我有一行代码供不和谐的bot删除特定的命名角色,并在特定时间内添加一个名为“ muted”的角色。基本上,服务器只能具有3个角色,一个可以发出命令,一个具有正常权限的“普通”等级,然后是“静音”角色。我的代码专门删除了普通角色,并添加了静音角色,因此他们没有任何权限。
好吧,我将我的机器人添加到了具有3个以上角色的另一台服务器上,我决定给每个人正常的角色,同时也将其设置为静音角色。当我运行该命令时,它可以工作,但是由于人们还有其他角色,因此即使将静音角色放在首位,它也可以继续讲话。
我的问题是,这里有一些代码可以删除所有角色并添加静音角色。静音期结束后,静音角色将被删除并恢复其先前的角色吗?这是我的代码如下:
case 'mute':
if(!message.member.roles.cache.find(r => r.name === "Admin")) return message.channel.send('You dont have permissions to do that you clown')
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("User Doesn't Exist");
let mainrole = message.guild.roles.cache.find(role => role.name == "normal");
let muterole = message.guild.roles.cache.find(role => role.name == "muted");
if(!muterole) return message.reply("Role Doesn't Exist");
let time = args[2];
if(!time){
return message.reply("How Long?");
}
person.roles.remove(mainrole.id);
person.roles.add(muterole.id);
message.channel.send(`@${person.user.tag} has now been muted for ${ms(ms(time))}`);
setTimeout(function(){
person.roles.add(mainrole.id);
person.roles.remove(muterole.id);
message.channel.send(`@${person.user.tag} has now been unmuted`)
}, ms(time));
break;
case 'help':
const embed2 = new MessageEmbed()
.setTitle("How To Get Commands")
.setColor(0x00fff7)
.setDescription("Do /commands to get the list of commands");
message.author.send(embed2);
break;
答案 0 :(得分:2)
最简单的方法是从用户那里获取角色列表,清除他们的角色,然后申请Muted角色。 然后,您想缓存他们以前的角色,甚至将数据存储在数据库中,以防机器人崩溃,这样一来,一旦重新启动,您就可以获取数据并继续进行操作。
这是我即时编写的一个简单示例。由于我的方法只是用于快速演示,因此您可能希望缓存角色的方式与我的方式有所不同,因此我强烈建议将数据保存到数据库或什至是.json文件之类。
let cachedUserRoles = {};
function addMutedRole(guildId, userId, roleId) {
//Get the guild the user is apart of
let guild = client.guilds.get(guildId);
//Specify the user from the guild
let guildMember = guild.members.get(userId);
//Cache the users existing roles so we can restore them later
cachedUserRoles[userId] = guildMember.roles.cache
//Remove all roles from user
guildMember.roles.set([])
//Add the muted role after all roles have been removed with an array of the single role ID
.then(member => member.roles.add([roleId]))
//Catch and report any errors that might happen
.catch(console.error)
}
function restoreRoles(guildId, userId) {
//Get the guild the user is apart of
let guild = client.guilds.get(guildId);
//Specify the user from the guild
let guildMember = guild.members.get(userId);
//Get and set the user's previouse roles from cachedUserRoles and error log if anything goes wrong
guildMember.roles.set(cachedUserRoles[userId]).catch(console.error)
}
对于您来说,它看起来像这样:
case 'mute':
if(!message.member.roles.cache.find(r => r.name === "Admin")) return message.channel.send('You dont have permissions to do that you clown')
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("User Doesn't Exist");
let muterole = message.guild.roles.cache.find(role => role.name == "muted");
if(!muterole) return message.reply("Role Doesn't Exist");
let time = args[2];
if(!time){
return message.reply("How Long?");
}
//Cache their already existing roles
cachedUserRoles[person.id] = person.roles.cache;
//Set their roles to an empty array to clear them, then add the muted role once all roles were removed successfully
person.roles.set([]).then(member => member.roles.add(muterole)).catch(console.error);
message.channel.send(`@${person.user.tag} has now been muted for ${ms(ms(time))}`);
setTimeout(function(){
//Grab their existing roles and set them back to the user, we wont need to remove the muted role since setting the roles would override all existing ones already
person.roles.set(cachedUserRoles[person.id]).catch(console.error)
message.channel.send(`@${person.user.tag} has now been unmuted`)
}, ms(time));
break;
case 'help':
const embed2 = new MessageEmbed()
.setTitle("How To Get Commands")
.setColor(0x00fff7)
.setDescription("Do /commands to get the list of commands");
message.author.send(embed2);
break;
答案 1 :(得分:0)
我不确定是否有更有效的方法来执行此操作,但是您可以尝试以下操作:
浏览所有角色,并将其保存到文件甚至数据库中,一旦时间用完,请删除静音的角色,并循环浏览已保存角色的列表,以将其重新添加。