我正在为 discord bot 编写一些代码,当我在执行取消静音和静音时,我试图让它发送消息,如果它们已经静音或未静音,但我无法正确处理任何帮助都会非常有用有帮助。 谢谢
module.exports = {
name: 'unmute',
description: "This unmutes a member",
execute(message, args){
if (message.member.hasPermission('MUTE_MEMBERS')) {
if (member.roles.cache.has(mainRoleId)){
return message.channel.send('Provided member isnt muted');
}else{
const target = message.mentions.users.first();
if(target){
let mainRole = message.guild.roles.cache.find(role => role.name === 'Verified');
let muteRole = message.guild.roles.cache.find(role => role.name === 'MUTED');
let memberTarget= message.guild.members.cache.get(target.id);
memberTarget.roles.remove(muteRole.id);
memberTarget.roles.add(mainRole.id);
message.channel.send(`<@${memberTarget.user.id}> has been unmuted`);
} else{
message.channel.send('Cant find that member!');
}
}else {
message.reply('you do not have permissions to use this command');
}
}
}
答案 0 :(得分:0)
if (member.roles.cache.has(mainRoleId)){
return message.channel.send('Provided member isn't muted.');
mainRoleId
和成员被静音之间有什么关系?如果我正确理解你的推理,你应该在 let muteRole = message.guild.roles.cache.find(role => role.name === 'MUTED');
语句的开头声明 if
,然后使用
if (!member.roles.cache.has(muteRoleId)){
return message.channel.send('Provided member isn't muted.');
const target = message.mentions.users.first();
一旦有人忘记传递参数,您的机器人就会崩溃。记得在命令的开头做一些万无一失的检查,就像这样(假设你想在 args 中传递未静音的人,这是一个必需的参数):
if (!args[0]) return message.reply(`not enough arguments!`);
if (message.mentions.users.first() == undefined) return message.reply(`user not found!`);
现在,至于标题中的问题,鉴于我的回复并拥有函数 if (member.roles.cache.has(muteRoleId))
,您应该可以自己弄清楚。