問題描述
我有一行代碼用于不和諧機(jī)器人刪除特定命名角色并在特定時(shí)間內(nèi)添加名為靜音"的角色.基本上,服務(wù)器只能有 3 個(gè)角色,一個(gè)可以發(fā)出命令,一個(gè)具有正常權(quán)限的普通"等級(jí),然后是一個(gè)靜音"角色.我的代碼專門刪除了正常角色并添加了靜音角色,因此他們沒有任何權(quán)限.
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.
好吧,我將我的機(jī)器人添加到了具有 3 個(gè)以上角色的另一臺(tái)服務(wù)器上,我決定給每個(gè)人一個(gè)正常的角色,同時(shí)也做一個(gè)靜音的角色.當(dāng)我運(yùn)行命令時(shí),它可以工作,但由于人們有其他角色,因此即使靜音角色處于最高優(yōu)先級(jí),它也允許繼續(xù)發(fā)言.
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.
我的問題是,是否有一些代碼可以刪除他們的所有角色并添加靜音角色.并且當(dāng)靜音期結(jié)束時(shí),靜音角色被刪除并恢復(fù)他們以前的角色?下面是我的代碼:
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;
推薦答案
最簡(jiǎn)單的方法是從用戶那里獲取角色列表,清除他們的角色,然后申請(qǐng) Muted 角色.然后,您需要緩存他們以前的角色,甚至將數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫(kù)中以防機(jī)器人出現(xiàn)故障,這樣一旦重新啟動(dòng),您就可以獲取數(shù)據(jù)并從中斷的地方繼續(xù).
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.
這是我即時(shí)編寫的一個(gè)簡(jiǎn)單示例.您可能希望緩存角色的方式與我的方式有所不同,因?yàn)槲业姆椒ㄖ皇菫榱丝焖傺菔荆覐?qiáng)烈建議將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)甚至是 .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;
這篇關(guān)于如何在discord bot上刪除所有角色并添加一個(gè)角色,然后刪除添加的角色并恢復(fù)以前的角色的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!