有没有一种方法可以使用命令切换事件?

时间:2019-01-18 07:56:44

标签: discord.js

是否可以通过命令使事件可切换?
我正在尝试进行欢迎/告别活动,但我不希望它在默认情况下处于活动状态。

这是我的活动现在的样子:

client.on("guildMemberAdd", (member) => {
  const guild = member.guild;
  let memberTag = member.user.tag;
  guild.channels.sort(function(chan1, chan2) {
    if (chan1.type !== `text`) return 1;
    if (!chan1.permissionsFor(guild.me).has(`SEND_MESSAGES`)) return -1;
    return chan1.position < chan2.position ? -1 : 1;
  }).first().send(memberTag + " just joined <:floshed:533687801741443082>");
});

1 个答案:

答案 0 :(得分:2)

以下是我的评论示例:

  

一种方法是将行会变量存储在某个数据库中,该变量的值为true或false。然后,您抓取该变量,并检查该行会是否已打开或关闭该选项

client.on("guildMemberAdd", (member) => {
  const guild = member.guild;
  let memberTag = member.user.tag;

  // Code here to get the guild from database, this is just a non-working example
  let dbGuild = database.get('Guild', guild.id);

  // Check if the guild has the welcome command disabled
  if (dbGuild.enableWelcomeCmd === false) {
    // Breaks the function, no further message will be send
    return;
  }


  guild.channels.sort(function(chan1,chan2){
      if(chan1.type!==`text`) return 1;
      if(!chan1.permissionsFor(guild.me).has(`SEND_MESSAGES`)) return -1;
      return chan1.position < chan2.position ? -1 : 1;
  }).first().send(memberTag + " just joined <:floshed:533687801741443082>");
});

client.on("message", async message => {
  // Check if the msg has been send by a bot
  if(message.author.bot) return;
  // Check if message has correct prefix
  if(message.content.indexOf(config.prefix) !== 0) return;

  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  // Code for actually changing the guild variable
  if (command === 'toggleWelcome') {
    // Code here to get the guild from database, this is just a non-working example
    let dbGuild = database.get('Guild', message.guild.id);

    dbGuild.enableWelcomeCmd = !dbGuild.enableWelcomeCmd;

    // Save the new variable for the guild (also a non-working example)
    database.save('Guild', message.guild.id, dbGuild);
  }
});

您将不得不研究数据库,而您自己也有很多(免费)选项,它们都有不同的语法。这部分是您需要弄清楚的东西,但我希望这可以给您一个总体思路。

相关问题