[英]How to remove all roles and add one role on discord bot, and then remove the added role and restore previous roles
我有一行代碼供不和諧機器人刪除特定命名角色並在特定時間內添加名為“靜音”的角色。 基本上,服務器只能有 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;
最簡單的方法是從用戶那里獲取角色列表,清除他們的角色,然后申請靜音角色。 然后,您希望緩存他們以前的角色,甚至將數據存儲在數據庫中,以防機器人出現故障,這樣一旦重新啟動,您就可以獲取數據並從上次中斷的地方繼續。
這是我即時編寫的一個快速示例。 您可能希望緩存角色的方式與我的方式略有不同,因為我的方法只是用於快速演示,我強烈建議將數據保存到數據庫,甚至是 .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;
我不確定是否有更有效的方法來執行此操作,但您可以嘗試以下操作:
循環遍歷他們的所有角色並將它們保存到文件甚至數據庫中,一旦時間用完,刪除靜音角色並循環瀏覽保存的角色列表以將它們添加回來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.