这是来自 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} 模板文字而不是被禁止的人。
我试图通过执行 ${targetMember} 来编辑它,但它不起作用,当我回到当前代码时,新的问题是无论我是否 ping 某人,它都会显示 (`${tag} Please specify which user to kick.`)
消息.并且代码本身不会返回任何错误。
所以我现在支持这个问题。有什么建议吗?
答案 0 :(得分:1)
Message.member 代表消息的作者。改用提及。
const { member, mentions } = message;
//mentions.members.first() will be the mentioned member
请记住,mentions.members.first()
不一定会返回消息中的第一次提及,如 here
答案 1 :(得分:0)
我自己找到了问题的答案!
它占据了两部分:
我按照 MrMythical 的建议将 mentions.users.first()
替换为 mentions.members.first()
。
我没有在消息中使用 ${tag}
模板文字告诉您哪个用户已被禁止,而是使用了 ${targetMember}
,它返回了正确的成员并 ping 了正确的用户!