Discord.js 禁止和踢出命令无法正常工作

Discord.js ban and kick commands not working properly

这是 index.js 文件中的代码。此代码位于 client.on('ready') 等激活器中。

//Ban Command
    command(client, 'ban', (message) => {
        const { member, mentions } = message
        const tag = `<@${member.id}>`

        if (member.hasPermission('ADMINISTRATOR') || member.hasPermission('BAN_MEMBERS')
        ) {
            const target = mentions.users.first()
            if (target) {
                const targetMember = message.guild.members.cache.get(target.id)
                targetMember.ban()
                message.channel.send(`${tag} has been banned.`)
            } else {
                message.channel.send(`${tag} Please specify which user to ban.`)
            }
        } else {
            message.channel.send(`${tag} You do not have the permission to use this command.`)
        }
    })

    //Kick Command
    command(client, 'kick', (message) => {
        const { member, mentions } = message
        const tag = `<@${member.id}>`

        if (member.hasPermission('ADMINISTRATOR') || member.hasPermission('KICK_MEMBERS')
        ) {
            const target = mentions.users.first()
            if (target) {
                const targetMember = message.guild.members.cache.get(target.id)
                targetMember.kick()
                message.channel.send(`${tag} has been kicked.`)
            } else {
                message.channel.send(`${tag} Please specify which user to kick.`)
            }
        } else {
            message.channel.send(`${tag} You do not have the permission to use this command.`)
        }
    })

当我 运行 前几次代码时,当我只输入 .exekick 或 .exeban 时它工作正常但是当我输入 .exekick 并 ping 一个测试用户以禁止时,来自机器人的消息在 ${tag} 模板文字中 ping 我而不是被禁止的人。

我尝试通过执行 ${targetMember} 来编辑它,但没有用,当我返回当前代码时,新问题是它说 (`${tag} Please specify which user to kick.`) 消息是否我对某人执行了 ping 操作或不。而且代码本身没有 return 任何错误。

所以我现在就站在这个问题上。有什么建议吗?

Message.member代表邮件的作者。请改为提及。

 const { member, mentions } = message;
//mentions.members.first() will be the mentioned member

请记住,mentions.members.first() 不一定是 return 消息中提到的第一个 here

我自己找到了问题的答案!

共分两部分:

  1. 我按照 MrMythical 的建议将 mentions.users.first() 替换为 mentions.members.first()

  2. 我没有在消息中使用 ${tag} 模板文字告诉您哪个用户已被禁止,而是使用 ${targetMember} 返回正确的成员并 ping 正确的用户!