【问题标题】:Add a "reason" to a ban/kick in discord.js在 discord.js 中为禁令/踢球添加“原因”
【发布时间】:2021-04-03 22:49:00
【问题描述】:

每当有人禁止用户时,我想添加一个原因,这是我的代码:

bot.on("message", message => {
    if(message.author.bot) return;
    if (message.content.startsWith(prefix + "ban")) {
        if (message.member.hasPermission("BAN_MEMBERS")) {
            let ban_member = message.mentions.members.first();
            
            if (ban_member != undefined) {
               ban_member.ban();  
               message.channel.send(ban_member.displayName + " a bien été banni !");
            } else {
                message.reply("Cet utilisateur ne peut être banni!")
            } 
        }
    } 
});

我到处搜索,但找不到任何人可以解释这一点 感谢您未来的回答!

【问题讨论】:

  • this 回答你的问题了吗?
  • 不,你出现的是预定义的原因,我想要的是用户指定的原因!像这样:!ban @someone reason.
  • 这在我提供的答案中有解释。

标签: javascript discord.js


【解决方案1】:

所有答案都不允许通过禁令命令输入原因。

您需要像这样在命令中定义参数:

let args = message.content.slice(prefix.length).trim().split(/ +/);
var reason = args[2];

实施

ban_member.ban({reason: `${reason}`});

代码:

    bot.on("message", message => {
        if(message.author.bot) return;
        if (message.content.startsWith(prefix + "ban")) {
    let args = message.content.slice(prefix.length).trim().split(/ +/);
    var reason = args[2];
            if (message.member.hasPermission("BAN_MEMBERS")) {
                let ban_member = message.mentions.members.first();
                
                if (ban_member != undefined) {
                   ban_member.ban({reason: `${reason}`});  
                   message.channel.send(ban_member.displayName + " a bien été banni !");
                } else {
                    message.reply("Cet utilisateur ne peut être banni!")
                } 
            }
        } 
    });

希望对你有所帮助;)

【讨论】:

  • “所有答案都不允许通过禁令命令输入原因。” 你确定我昨天的回答不会完全那样吗? :)
  • 我一定是瞎了,因为我没看到LOL
  • 没关系,但很奇怪:D
  • 只有当你有一个参数的原因时它才有效,它只会占用第一个单词,所以如果我想说:“test 1”的原因,它只会返回“test”而不是 1 (抱歉法国时间延迟;))
【解决方案2】:

如果你想通过 Discord 的命令提供被禁止的原因,你可以从消息字符串中获取它并将其传递给member.ban() 方法。

如果您用空格分割消息并删除前两个元素(!ban@username),则消息的其余部分将是原因。您可以再次按空格加入数组。检查下面的这个sn-p:

const message = {
  content: '!ban @john_doe for being a real tool and telling lies!'
}

const args = message.content.trim().split(/ +/);
const command = args[0];
const reason = args.slice(2).join(' ');

console.log({
  command,
  reason
})

您可以像这样更新您的代码:

bot.on('message', (message) => {
  if (message.author.bot || !message.content.startsWith(prefix)) {
    return;
  }
  const args = message.content.trim().split(/ +/);
  const command = args[0];

  // if the command is !ban but author can't ban, just exit
  if (command === '!ban' && !message.member.hasPermission('BAN_MEMBERS')) {
    return;
  }

  const memberToBan = message.mentions.members.first();
  // remove the command and the mentioned user and join the words
  const reason = args.slice(2).join(' ');

  if (memberToBan) {
    // pass the reason
    memberToBan.ban({ reason });
    message.channel.send(`${memberToBan.displayName} a bien été banni!`);
  } else {
    message.reply('Cet utilisateur ne peut être banni!');
  }
});

【讨论】:

  • 这就像我说的那样,只有当原因由 2 个单词组成时,是否有一种方法可以在整数之后获取所有数组,比如在 args[2] 之后从数组中获取所有值例子?
  • 不确定我是否理解正确。在我的示例中,如果消息是"!ban @john_doe for being a real tool and telling lies!",原因将是"for being a real tool and telling lies!"。这不是你想要的吗?
  • 问题是代码只接受第二个参数(在这种情况下),而不是整个句子,这就是我问的原因
  • 您能告诉我示例消息的预期输出是什么吗? "!ban @john_doe for being a real tool and telling lies!"
  • 输出将打印:@john_doe has been bans for the reason : for
【解决方案3】:
member.ban({reason: "your reason here"});

【讨论】:

  • 不,你出现的是预定义的原因,我想要的是用户指定的原因!像这样:!ban @someone reason.
  • @XRHTech 最新的回答是对的,不过把原因定义换成args.slice(2).join(" ");
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 2020-11-18
  • 2020-10-20
  • 2021-06-01
  • 2021-06-01
  • 1970-01-01
  • 2021-08-22
  • 2021-11-13
相关资源
最近更新 更多