静音角色在被静音用户离开服务器时保存到他们

时间:2021-04-10 13:08:24

标签: javascript discord discord.js quick.db

这是我的代码,但是当某人离开服务器并重新加入它时,我赋予了静音角色,该角色消失了,当我使用 quick.db 时,它不起作用我应该怎么做才能使其工作< /strong>

const ms = require('ms')
const timer = new Map();
client.on('message', async nxt => {
  if (nxt.content.startsWith(prefix + "role")){
    var args = nxt.content.slice(prefix.length).trim().split(/ +/g);
        if(!nxt.member.hasPermission('MANAGE_ROLES')) return nxt.channel.send("You don't have permissions")
        var member = nxt.mentions.members.first();
        var time = args[3]
        var role = nxt.mentions.roles.first();
        if(!role) return nxt.channel.send("I Can't find this role")
        if(!member) return nxt.channel.send("Please Mention Someone")
        if(!time) return nxt.channel.send("Please specify a time")
        if (!time.endsWith("d")) {
        if (!time.endsWith("h")) {
        if (!time.endsWith("m")) {
        if (!time.endsWith("s")) {
          return nxt.channel.send('Please specify a time')
        }}}}
    timer.set(nxt.author.id,{
      author: {
        tag: nxt.author.tag,
      },
      time: ms(time),
    });
        await member.roles.add(role)
        nxt.react(`✅`)
        setTimeout(async () => {
            await member.roles.remove(role)
           timer.delete(nxt.author.id);
        }, ms(time))
 }});```

How can I make it to give the mute role back ?

1 个答案:

答案 0 :(得分:0)

首先在代码中添加这一行

//using quick.db
let muteTime = Date.now() + time;
db.set(`muted_${message.guild.id}_${target.id}`, muteTime);

然后当一个成员加入时这样做

client.on('guildMemberAdd', member => {
    let MuteStatus = db.get(`muted_${member.guild.id}_${member.id}`);
    if(!MuteStatus){
        //This member wasn't muted
        return(0);
    }
    if(MuteStatus > Date.now()){
        //He was muted and rejoined
        //Give the role back
       let TimeLeft = MuteStatus - Date.now();
       await member.roles.add(MuteRole) //Give the muted role
       setTimeout(async () => {
            await member.roles.remove(MuteRole);
        },TimeLeft);
    }
});