Multible服务器上的Discord.js切换命令 [英] Discord.js Toggle Commands On Multible Servers

查看:72
本文介绍了Multible服务器上的Discord.js切换命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要一个不和谐的机器人,您可以在写入命令的服务器上切换命令吗?仅作为示例:

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.

推荐答案

请跟踪公会以及是否在公会上或关闭。

Keep track of guilds and if they are on or off.

首先要初始化所有公会

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
   }
});

切换命令也非常容易。基本上只需要做 guildToggles.toggles [cmd] =!guildToggles.toggles [cmd]

Toggling commands is super easy as well. Basically just have to do guildToggles.toggles[cmd] = !guildToggles.toggles[cmd]

这篇关于Multible服务器上的Discord.js切换命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆