问题
So i want a discord bot were you can toggle commands on the server the command was writen in. Just for an example:
if(cmd === "me"){
message.channel.send(`You <@${message.author.id}>!`);
}
To toggle the command on/of on any server you would need to write "me on" or "me off ". "me on" Will allow to execute the command on that server and "me off " will not allow the command to be executed on that server. I would like this to work on many servers so no allow on guild etc.
回答1:
Keep track of guilds and if they are on or off.
First start by initializing all guilds
var guilds = [];
var commands = ['me', 'test']; //List of all commands that can be toggled
bot.on('ready', () => {
guilds = bot.guilds.map(guild => { //For each guild create an object of bools based on the commands
var obj = {};
commands.forEach(command => obj[command] = true);
return {
toggles: obj,
id: guild.id
};
});
});
we now have a data structure that looks like
[
{
id: (guildId)
toggles: {me: true, test: true}
}
]
Now whenever we get a message its super simple to check if the command is active
bot.on('message', msg => {
var cmd = ;//get command from msg
var guildToggles = guilds.find(guild => guild.id == msg.guild.id);
if(guildToggles.toggles[cmd]){
//Command is active. Run
}else{
//Command is not active. Dont run
}
});
Toggling commands is super easy as well. Basically just have to do guildToggles.toggles[cmd] = !guildToggles.toggles[cmd]
来源:https://stackoverflow.com/questions/59141448/discord-js-toggle-commands-on-multible-servers