Reputation: 6806
I'm trying to build a bot with Commando, but I can't figure out a way to make the bot ignore commands (or better, delete and ignore) commands that are not sent in a defined channel. For example, if you don't send the command in #botchat the message gets deleted.
I know that I could deny the read permission to the bot in the other channels, but I have other modules running, and they require reading channels.
I could add a check at the beginning of every run function, but this won't affect the default commands.
Is there a way to write a check function that is run for every command before the actual run function begins? (Maybe using the Command class?)
Upvotes: 0
Views: 7006
Reputation: 11
All you need to do is:
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
}
}
Hope I helped! Thanks.
Upvotes: 1
Reputation: 6806
I found out: you can use Inhibitors. Thanks to Gawdl3y#4269 in the Discord.js guild
//in main file
client.dispatcher.addInhibitor(msg => {
return (msg.channel.name == "blockme"); //you return whether the command should be blocked
})
Upvotes: 2
Reputation: 89
are your commands inside an IF/ELSE IF tree?
If so, what I would do is as the very begining of that before it even checks for a command (but under the part where it runs on every message on the server) have a variable declared to be the channel of the message. Something like;
var ChannelID = message.channel.id
or if you wan't to do it by name
var ChannelName = message.channel.name
then, when your command is dependent on the channel do something like this:
if(command === "ChannelDependentCommand"){
if(ChannelID !== "AllowedChannel"){
message.delete();
}ELSE {
//**whatever you want the command to do**
};
};
I hope this helps? Obviously if you are doing things differently the syntax may need tweaking, and disclaimer, this hasn't been tested at all but it should explain the logic and thinking behind it.
Also, for future reference, if you provide some of your code that you have already got it makes it easier to give a relevant answer :)
Upvotes: 0