您可以像处理禁用词一样执行相同的操作,并将每个人的用户 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();刷新缓存。