Discord.js-commando: 如果不在特定频道中则停止所有命令
Discord.js-commando: Stopping all commands if not in a specific channel
为了测试,我试图停止所有命令,除非在某个频道中。我知道如何具体针对每个命令执行此操作,但我试图在主机器人文件中捕获它,并 return 一条消息。到目前为止,我尝试了两种方法:
bot.on('command', async m => { (Also tried 'commandmessage')
console.log('COMMAND');
if (m.channel != 'bot-testing') {
return m.channel.send('You can\'t use commands here!');
}
});
这根本不起作用。然后我试了这个:
bot.on('message', async m => {
m.isDM = (m.guild ? false : true);
if (m.content[0] != bot.commandPrefix) {
return;
} else {
if (m.channel != 'bot-testing') {
m.channel.send('You can\'t use commands here!');
}
}
});
哪种有效,但不会停止命令。
看起来你非常接近 - 你只需要在第二个 if-statement
中查看 m.channel.name
(使用方法 #2):
bot.on('message', async m => {
// ...
if (m.content[0] != bot.commandPrefix) {
return;
} else {
// [NEW: add .name prop here]
if (m.channel.name != 'bot-testing') {
m.channel.send('You can\'t use commands here!');
}
}
});
为了测试,我试图停止所有命令,除非在某个频道中。我知道如何具体针对每个命令执行此操作,但我试图在主机器人文件中捕获它,并 return 一条消息。到目前为止,我尝试了两种方法:
bot.on('command', async m => { (Also tried 'commandmessage')
console.log('COMMAND');
if (m.channel != 'bot-testing') {
return m.channel.send('You can\'t use commands here!');
}
});
这根本不起作用。然后我试了这个:
bot.on('message', async m => {
m.isDM = (m.guild ? false : true);
if (m.content[0] != bot.commandPrefix) {
return;
} else {
if (m.channel != 'bot-testing') {
m.channel.send('You can\'t use commands here!');
}
}
});
哪种有效,但不会停止命令。
看起来你非常接近 - 你只需要在第二个 if-statement
中查看 m.channel.name
(使用方法 #2):
bot.on('message', async m => {
// ...
if (m.content[0] != bot.commandPrefix) {
return;
} else {
// [NEW: add .name prop here]
if (m.channel.name != 'bot-testing') {
m.channel.send('You can\'t use commands here!');
}
}
});