問題描述
我有一行代碼用于不和諧機器人刪除特定命名角色并在特定時間內添加名為靜音"的角色.基本上,服務器只能有 3 個角色,一個可以發出命令,一個具有正常權限的普通"等級,然后是一個靜音"角色.我的代碼專門刪除了正常角色并添加了靜音角色,因此他們沒有任何權限.
I have a line of code for a discord bot to remove a specific named role and add a role named "muted" for a specific amount of time. Basically, the server can only have 3 roles, one that can issue the command, a "normal" rank with normal permissions, and then a "muted" role. and my code specifically removed the normal role and adds the muted role so they don't have any permissions.
好吧,我將我的機器人添加到了具有 3 個以上角色的另一臺服務器上,我決定給每個人一個正常的角色,同時也做一個靜音的角色.當我運行命令時,它可以工作,但由于人們有其他角色,因此即使靜音角色處于最高優先級,它也允許繼續發言.
Well I added my bot onto another server with more than 3 roles, I decided to give everyone the normal role and also make the muted role. when I run the command, it works, but since the people have other roles, it allows then to continue speaking even though the muted role is at the top priority.
我的問題是,是否有一些代碼可以刪除他們的所有角色并添加靜音角色.并且當靜音期結束時,靜音角色被刪除并恢復他們以前的角色?下面是我的代碼:
My questions is that is there some code where I can just remove all their roles and add the muted role. and when the muted period is over, the muted role is removed and their previous roles are restored? here is my code below:
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;
推薦答案
最簡單的方法是從用戶那里獲取角色列表,清除他們的角色,然后申請 Muted 角色.然后,您需要緩存他們以前的角色,甚至將數據存儲在數據庫中以防機器人出現故障,這樣一旦重新啟動,您就可以獲取數據并從中斷的地方繼續.
The simplest way to do this would be to get the list of roles from the user, clear their roles, and then apply for the Muted role. You'd then want to cache their previous roles or even store the data in a database in case the bot goes down, that way once restarted you could fetch the data and continue where you left off.
這是我即時編寫的一個簡單示例.您可能希望緩存角色的方式與我的方式有所不同,因為我的方法只是為了快速演示,我強烈建議將數據保存到數據庫甚至是 .json 文件之類的文件中.
Here's a quick example I wrote on the fly. You probably want to cache the roles a bit differently than the way I did since my method was just for a quick demo, and i highly recommend saving the data to a database or even something like a .json file.
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)
}
在你的情況下,它看起來像這樣:
In your case, it would look something like this:
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;
這篇關于如何在discord bot上刪除所有角色并添加一個角色,然后刪除添加的角色并恢復以前的角色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!