gpt4 book ai didi

javascript - 如何在discord bot上删除所有 Angular 色并添加一个 Angular 色,然后删除添加的 Angular 色并恢复以前的 Angular 色

转载 作者:行者123 更新时间:2023-12-02 21:15:50 29 4
gpt4 key购买 nike

我有一行不和谐机器人的代码,用于删除特定的命名 Angular 色并添加一个名为“静音”的 Angular 色一段特定的时间。基本上,服务器只能有 3 个 Angular 色,一个可以发出命令,一个具有正常权限的“普通” Angular 色,然后是一个“静音” Angular 色。我的代码专门删除了正常 Angular 色并添加了静音 Angular 色,因此他们没有任何权限。

好吧,我将我的机器人添加到另一台具有超过 3 个 Angular 色的服务器上,我决定为每个人提供正常 Angular 色,并设置静音 Angular 色。当我运行该命令时,它起作用了,但由于人们有其他 Angular 色,因此即使静音 Angular 色处于最高优先级,它也允许继续讲话。

我的问题是,是否有一些代码可以删除他们的所有 Angular 色并添加静音 Angular 色。当禁言期结束后,禁言 Angular 色被解除,恢复原来的 Angular 色?下面是我的代码:

 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;

最佳答案

最简单的方法是从用户处获取 Angular 色列表,清除其 Angular 色,然后申请静音 Angular 色。然后,您需要缓存他们以前的 Angular 色,甚至将数据存储在数据库中,以防机器人出现故障,这样一旦重新启动,您就可以获取数据并从上次中断的地方继续。

这是我即时编写的一个简单示例。您可能希望缓存 Angular 色的方式与我的方式略有不同,因为我的方法只是为了快速演示,我强烈建议将数据保存到数据库,甚至保存到 .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;

关于javascript - 如何在discord bot上删除所有 Angular 色并添加一个 Angular 色,然后删除添加的 Angular 色并恢复以前的 Angular 色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60974984/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com