【发布时间】:2018-09-18 08:18:30
【问题描述】:
我正在创建一个不和谐的机器人,将来我会添加更多命令,我只是 不明白参数的使用以及如果有像“说一般(频道)嗨伙计们我是文本(要发送的文本)”这样的空格它将如何工作。它只会向将军发送“hi”,而不是其他任何东西,这是聊天命令的缺点。
const bot = new Discord.Client({disableEveryone: true});
bot.on("ready", async () => {
console.log(`Bot is ready! ${bot.user.username}`);
try {
let link = await bot.generateInvite("[ADMINISTRATOR]");
console.log(link);
} catch(e) {
console.log(e.stack);
}
bot.user.setGame('nope nope nope');
});
bot.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
let command = message.content.split(" ")[0];
command = command.slice(prefix.length);
const args = message.content.slice(prefix.length).trim().split(/ +/g);
if (command === "ping") {
message.channel.send(`Pong! Time took: ${Date.now() - message.createdTimestamp} ms`);
} else
if (command === "say") {
message.delete()
let thetext = args[2];
let thechannel = args[1];
bot.channels.find("name",`${thechannel}`).sendMessage(`${thetext}`)
}
});
【问题讨论】:
-
您需要从索引 2 开始为您的文本获取所有字符串,并用空格将它们连接起来。
标签: javascript command discord