如何使用 Commando 框架将机器人命令限制为仅一个通道,

时间:2021-01-31 03:07:11

标签: discord.js commando

我曾经使用此代码扫描我的机器人收到的每条消息。但是现在我正在使用 Commando 框架对我的机器人进行编程,我没有使用 client.on('message', async (message) => { ...}); 拦截消息、检查它们是否是命令并运行它们的方式。相反,我使用的是内置命令处理程序。我需要某种方法在所有命令被突击队处理之前拦截所有命令,以检查它们是否在正确的频道中发送。我可以在每个命令文件中都做这个检查,但那是多余的。

srv 是保存消息被发送到的公会的所有 MongoDB 属性的对象。
defaultChannel 是一个允许管理员为公会设置默认频道的模块。

这是我在切换到突击队之前使用的代码:

        //check if a message is not sent in the default channel, check if there is a default channel set
        //if there is one, deny the message and tell the user to use this command in that channel
        //else tell the user that the server owner needs to set a default channel first
        //before the bot can be used
        if (message.channel.id != srv.GUILD_DEFAULT_CHANNEL) {
            if (srv.GUILD_DEFAULT_CHANNEL === null) {
                message.channel.send("The server owner has not set a default channel yet.\n If you are the server owner please use `&defaultChannel (CHANNEL ID)`");
                defaultChannel.defaultChannelCMD(message, args);
                return; //exit the loop and don't parce the command
            } else {
                //ping the user in the default channel
                bot.channels.cache.get(srv.GUILD_DEFAULT_CHANNEL).send(`<@${message.author.id}> Please use me in this channel`)
                return;
            }
        }

0 个答案:

没有答案