-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpermissions.js
91 lines (81 loc) · 3.3 KB
/
permissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Permissions } from 'discord.js';
/**
* @typedef {import('discord.js').User} User
* @typedef {import('discord.js').Guild} Guild
*/
const PERMISSION_NAMES = new Map([
[Permissions.FLAGS.ADMINISTRATOR, 'Administrator'],
[Permissions.FLAGS.CREATE_INSTANT_INVITE, 'Create Instant Invite'],
[Permissions.FLAGS.KICK_MEMBERS, 'Kick Members'],
[Permissions.FLAGS.BAN_MEMBERS, 'Ban Members'],
[Permissions.FLAGS.MANAGE_CHANNELS, 'Manage Channels'],
[Permissions.FLAGS.MANAGE_GUILD, 'Manage Server'],
[Permissions.FLAGS.ADD_REACTIONS, 'Add Reactions'],
[Permissions.FLAGS.VIEW_AUDIT_LOG, 'View Audit Log'],
[Permissions.FLAGS.PRIORITY_SPEAKER, 'Priority Speaker'],
[Permissions.FLAGS.VIEW_CHANNEL, 'View Channel'],
[Permissions.FLAGS.SEND_MESSAGES, 'Send Messages'],
[Permissions.FLAGS.SEND_TTS_MESSAGES, 'Send TTS Messages'],
[Permissions.FLAGS.MANAGE_MESSAGES, 'Manage Messages'],
[Permissions.FLAGS.EMBED_LINKS, 'Embed Links'],
[Permissions.FLAGS.ATTACH_FILES, 'Attach Files'],
[Permissions.FLAGS.READ_MESSAGE_HISTORY, 'Read Message History'],
[Permissions.FLAGS.MENTION_EVERYONE, 'Mention Everyone'],
[Permissions.FLAGS.USE_EXTERNAL_EMOJIS, 'Use External Emojis'],
[Permissions.FLAGS.CONNECT, 'Connect'],
[Permissions.FLAGS.SPEAK, 'Speak'],
[Permissions.FLAGS.MUTE_MEMBERS, 'Mute Members'],
[Permissions.FLAGS.DEAFEN_MEMBERS, 'Deafen Members'],
[Permissions.FLAGS.MOVE_MEMBERS, 'Move Members'],
[Permissions.FLAGS.USE_VAD, 'Use Voice Activity'],
[Permissions.FLAGS.CHANGE_NICKNAME, 'Change Nickname'],
[Permissions.FLAGS.MANAGE_NICKNAMES, 'Manage Nicknames'],
[Permissions.FLAGS.MANAGE_ROLES, 'Manage Roles'],
[Permissions.FLAGS.MANAGE_WEBHOOKS, 'Manage Webhooks'],
[Permissions.FLAGS.MANAGE_EMOJIS, 'Manage Emojis'],
]);
/**
* Checks to see if a User is a Guild Admin
* @param {Guild} guild - Guild to check admin status in
* @param {User} user - User to check admin status for
* @returns {boolean} Whether the user is a Guild Admin
*/
export function isGuildAdmin(guild, user) {
const member = guild.member(user);
if (!member) {
return false;
}
return member.hasPermission(Permissions.FLAGS.ADMINISTRATOR);
}
/**
* Converts a Permission Flag to its string representation
* @param {number} permission - Permission Flag
* @returns {string} The name of a Permission Flag
*/
export function getPermissionName(permission) {
if (PERMISSION_NAMES.has(permission)) {
return PERMISSION_NAMES.get(permission);
}
return 'Unknown';
}
/**
* Checks to see if the bot is granted a given list of permission flags and
* indicates which ones it does not have
* @param {Guild} guild - Guild to check permission values in
* @param {...number} permissions - Array of permission flags to check
* @returns {Object} check - Information about the bot's permission flags in the given guild
* @returns {boolean} check.valid - Whether the bot has all the permission flags granted
* @returns {number[]} check.missing - Missing permission flags
*/
export function checkBotPermissions(guild, ...permissions) {
const missingPermissions = new Set();
permissions.forEach((permission) => {
if (!guild.me.hasPermission(permission)) {
missingPermissions.add(permission);
}
});
return {
valid: missingPermissions.size === 0,
missing: Array.from(missingPermissions),
};
}