将命令限制在某些频道

Limiting commands to certain channels

我正在尝试使用 Commando 构建一个机器人,但我想不出一种方法让机器人忽略未在定义的通道中发送的命令(或者更好的是,删除并忽略)命令。例如,如果您不在#botchat 中发送命令,消息将被删除。

我知道我可以拒绝其他频道中机器人的读取权限,但我还有其他模块 运行ning,它们需要读取频道。

我可以在每个 运行 函数的开头添加一个检查,但这不会影响默认命令。

有没有办法在实际 运行 函数开始之前为每个命令编写一个 运行 的检查函数? (也许使用命令 class?)

你的命令是在 IF/ELSE IF 树中吗?

如果是这样,我会做的是在它甚至检查命令之前(但在它在服务器上的每条消息上运行的部分下)有一个变量声明为通道消息。像;

var ChannelID = message.channel.id

或者如果您不想按名称进行操作

var ChannelName = message.channel.name

然后,当您的命令依赖于频道时,执行如下操作:

if(command === "ChannelDependentCommand"){
    if(ChannelID !== "AllowedChannel"){
        message.delete();
    }ELSE {
        //**whatever you want the command to do**
    };
};

希望对您有所帮助?显然,如果你做的事情不同,语法可能需要调整,免责声明,这根本没有经过测试,但它应该解释其背后的逻辑和思想。

此外,为了将来参考,如果您提供一些您已经获得的代码,则可以更轻松地给出相关答案:)

我发现:您可以使用 Inhibitors。感谢 Discord.js 公会

中的 Gawdl3y#4269
//in main file
client.dispatcher.addInhibitor(msg => {
  return (msg.channel.name == "blockme"); //you return whether the command should be blocked
})

您只需:

var Channel = message.channel.name

if (message.content === "command") {
    if(Channel != "Channel name here") {
        message.channel.send('Cannot use command here, ' + message.author);
    } else {
        // Insert command code here
    }
}

希望对您有所帮助!谢谢