问题
I have a Discord bot. I am coding warn command but I need a code to compare two members' (role) rankings. Example: A person is higher than B person. If B person tries to warn A person, bot says 'You can not warn this person.'. If A person tries to warn B person, bot warns B person.
How to do that?
回答1:
Get each member's highest role and compare their calculated positions
By getting a GuidMember's Highest Role and getting that Role's Calculated Position, we can use this value to compare with other member's highest role's calculated positions to see who's higher up according to the server's role manager.
// I'll assume you already have the data for both members (memberA and memberB)
let rankingA = memberA.highestRole.calculatedPosition;
let rankingB = memberB.highestRole.calculatedPosition;
if (rankingA > rankingB) {
// apply warning
} else {
// reject warning
}
Notes:
.highestRole
will return the role object itself..calculatedPosition
will return an integer, higher value means higher ranking.
回答2:
So you'd initially need to setup a counting system to count their permissions, maybe it could be role based. You could store this in a simple SQL DB, or an array etc. Relatively simple. Say if I had x
role, I'd have x
points. This could be done with if (user.role === 'blah') points = x
or something simple like that.
If you didn't want to do the counting system, look into Role#calculatedPosition - that'll return the height in the list of roles on your Discord server. For example;
const roleX = message.role.calculatedPosition()
const otherRole = message.role.calculatedPosition()
You'd need to look for how to go about calling the role and its ID.
Once that's done, do something like
if (points.a > points.b) {
// do something here like allow/reject it
message.channel.send('warn stuff')
} else {
//reject it here, for example
message.channel.send('no')
return
}
Let me know how ya got on.
来源:https://stackoverflow.com/questions/58901755/is-there-a-way-to-compare-two-members-rankings