【问题标题】:Ban discord invite禁止不和谐邀请
【发布时间】:2022-01-21 14:56:11
【问题描述】:

我想在机器人上创建一个白名单,并且其中的人可以发送链接。

client.on(`message`, async message => {
   let msg = message
   const bannedWords = [`discord.gg`, `.gg/`, `.gg /`, `. gg /`, `. gg/`, `discord .gg /`, `discord.gg /`, `discord .gg/`, `discord .gg`, `discord . gg`, `discord. gg`, `discord gg`, `discordgg`, `discord gg /`]
   try {
       if (bannedWords.some(word => message.content.toLowerCase().includes(word))) {
           if (message.author.id === message.guild.ownerID) return;
           await message.delete();
           await message.channel.send(`<@`+message.author+`> **You cannot send invites to other Discord servers ????**`).then((m) => m.delete({ timeout: 3000 }))
       }
   } catch (e) {
       console.log(e);
   }
});

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    您可以像处理禁用词一样执行相同的操作,并将每个人的用户 ID 放入一个数组中。 (不推荐用于中/大型公会)或与角色一起工作。因此,假设您创建了一个角色并将其分配给允许添加链接的人员。然后检查他们是否有角色,如果没有,您可以发送警告并删除消息。

    使用用户 ID 的白名单数组示例:

    client.on(`message`, async message => {
        let msg = message;
    
        // Put all the user ID's in this array
        const allowedUsers = ["user_id_1","user_id_2","user_id_3"];
        const bannedWords = [`discord.gg`, `.gg/`, `.gg /`, `. gg /`, `. gg/`, `discord .gg /`, `discord.gg /`, `discord .gg/`, `discord .gg`, `discord . gg`, `discord. gg`, `discord gg`, `discordgg`, `discord gg /`]
        try {
            if (bannedWords.some(word => message.content.toLowerCase().includes(word))) {
                if (message.author.id === message.guild.ownerID) return;
                if(allowedUsers.indexOf(message.author.id) !== -1) return;
                
                await message.delete();
                await message.channel.send(`<@`+message.author+`> **You cannot send invites to other Discord servers ?**`).then((m) => m.delete({ timeout: 3000 }))
            }
        } catch (e) {
            console.log(e);
        }
    });
    

    或者你可以用角色来做。只需创建一个新角色,例如Links Allowed,复制此角色的 ID 并将其放入下面的代码中message.member.roles.cache.has("role_id_of_allowed")

    client.on(`message`, async message => {
        let msg = message;
    
        // Put all the user ID's in this array
        const bannedWords = [`discord.gg`, `.gg/`, `.gg /`, `. gg /`, `. gg/`, `discord .gg /`, `discord.gg /`, `discord .gg/`, `discord .gg`, `discord . gg`, `discord. gg`, `discord gg`, `discordgg`, `discord gg /`]
        try {
            if (bannedWords.some(word => message.content.toLowerCase().includes(word))) {
                if (message.author.id === message.guild.ownerID) return;
                if(message.member.roles.cache.has("role_id_of_allowed") === true) return;
                
                await message.delete();
                await message.channel.send(`<@`+message.author+`> **You cannot send invites to other Discord servers ?**`).then((m) => m.delete({ timeout: 3000 }))
            }
        } catch (e) {
            console.log(e);
        }
    });
    

    如果不能正常使用,也可以先获取用户的个人资料,使用await message.guild.members.fetch();刷新缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2020-12-02
      • 2020-10-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多