通过提及discordjs识别管理员 [英] identifying admins from mentions discordjs
问题描述
我想使用 if(message.mentions.users.first())
查找正在执行的消息中是否有提及,但现在我也想过滤所有提及提及管理团队,而不是社区成员.如何实现?
I want to find out if there are mentions in a message which I am doing using if(message.mentions.users.first())
but now among all the mentions I also want to filter out mentions of the admin team and not those of the community members. How to achieve that?
我尝试根据这样的角色过滤用户
I tried filtering users based on roles like this
let r = [];
message.mentions.users.forEach( user => {
console.log('user' + user)
user.roles.forEach(role => {
console.log('role' + role)
r.push(role);
})
console.log('roles' + r);
var member = false;
for (i in r) {
if (i == 'admin') {
member = true;
}
}
})
这似乎不起作用.
推荐答案
const adminName = ['<role name>'];
const adminId = ['<role ID>'];
client.on('message', (msg) => {
let admins = msg.mentions.members.filter( (user) => {
return adminId.some(id => user.roles.has(id)) || user.roles.some(r => adminName.includes(r.name));
});
console.log(admins.map(d => d.user.username));
});
在那里,我创建了2个数组,一个数组具有角色ID,另一个数组具有角色名称.您可以使用两个,也可以只使用一个(我更喜欢使用id,因为您可以更改角色名称,但是不能更改id,除非删除角色).
There, I created 2 arrays, one with the role ID and another with the role name. You can use both, or only one (I prefer to use the id, because you can change a role name, but you can't change an id, except if you delete the role).
然后,我遍历 邮件中提到的成员
.为什么是 members
而不是 users
?因为 GuildMember
是链接到公会的类型,公会是公会中用户的实例,因此具有用户的角色.
Then, I iterate through the members
in the mentions of the messages. Why the members
and not the users
? Because the GuildMember
is a type linked to a guild, which is an instance of an user in a guild, and so it has the role of the user.
然后,我检查管理员角色的id之一是否在用户中,或者,如果角色名称之一在我的 adminName
中.
Then I check if one of the id of my admins roles is in the roles of the user or if one of the roles name is inside my adminName
.
变量 admins
是 GuildMember
的集合.
您可以更改 some
内部的功能,以匹配使某人成为您管理员的任何条件.
You can change the function inside the some
to match whatever condition make someone an admin for you.
这篇关于通过提及discordjs识别管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!