如何在命令中添加“原因”

时间:2019-04-12 15:26:44

标签: node.js bots discord discord.js

我正在node.js(不是master)中编写一个不和谐的bot,并且正在执行kick and ban命令。我试图使BOT写入用户的禁止日志。像+ ban @用户原因。我确实禁止了@user,但是我无法解释原因。

  if (!message.member.hasPermission("BAN_MEMBERS")) return;

  if (message.content.startsWith('+ban')) {
    const user = message.mentions.users.first();
    if (user) {
      const member = message.guild.member(user);
      if (member) {
        member.ban({
          reason: 'reason',
        }).then(() => {
          message.channel.send(`${user.tag} BAN!`);
        }).catch(err => {
          message.channel.send('Bu çar çok güçlü, banlayamıyorum! ');
          console.error(err);
        });
      } else {
        message.channel.send('Kullanıcı sunucuda değil.');
      }
    } else {
      message.channel.send('Adını ver banlayayım, sahip.');
    }
  }
});```

1 个答案:

答案 0 :(得分:0)

只需使用member.ban('reason here')。如果您需要删除以前的消息,请使用一个对象,并提供原因,例如:

member.ban({days: 2, reason: 'bad'});

现在,只需根据用户的原因使用此设置。出于这个原因,请使用一个变量作为arguments数组的分割版本,并加上空格。

编辑:显示上下文...

if (message.content.toLowerCase().startsWith('+ban')) { // changed to case insensitive command
  const member = message.mentions.members.first(); // keep in mind it isn't the best practice to use message.mentions to retrieve an argument
  if (!member) return message.channel.send('no member mentioned');
  let reason = args.slice(2).join(' '); // arguments should already be defined
  member.ban(reason)
  .then(message.channel.send('success'))
  .catch(err => {
    message.channel.send('something went wrong');
    console.error();
  });
}