This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPermissions.ts
70 lines (64 loc) · 2.42 KB
/
Permissions.ts
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
import { User, Message, GuildMember, Snowflake } from 'discord.js';
import { Command, TypedSnowflake } from './Types';
const admins = <Array<TypedSnowflake>>[
'261908587943690242', // Sam for now
'118455061222260736', // Muirrum
'262245801416327169', // Brooke
];
export const hasPermission = (message: Message, command: Command) => {
const required = command.requiredPerms;
if (!required) {
// If the command doesn't specify, default to public
return true;
}
// It's more specific...
switch (required) {
case 'public':
return true;
case 'admin':
case 'whitelist': // Whitelist inherits all the admins as well
if (message.guild) {
const guildUser = message.guild.member(message.author);
if (required == 'admin') {
return inTypedWhitelist(guildUser, admins);
}
let localWhitelist = admins;
if (command.whitelist) {
localWhitelist = admins.concat(command.whitelist);
}
return inTypedWhitelist(guildUser, localWhitelist);
} else {
return inTypedWhitelist(message.author, admins);
}
}
};
export const inRole = (guildUser: GuildMember, role: Snowflake) =>
guildUser.roles.cache.find((roleData, roleID) => role == roleID)
? true
: false; // Look through the GuildMember's roles and see if this role is anywhere there
export const inTypedSnowflake = (
user: User | GuildMember,
snowflake: TypedSnowflake
) => {
if (snowflake.match(/^&\d+$/) && isGuildMember(user)) {
// Is a role
return inRole(user, snowflake.substr(1));
} else if (snowflake.match(/^\d+$/)) {
// Is a user
return snowflake == user.id;
} else {
return false;
}
};
export const inTypedWhitelist = (
user: User | GuildMember,
whitelist: Array<TypedSnowflake>
) => {
// Loop through the whitelist and look for anything this user is included in. It returns whether or not they are included in anything
return (
whitelist.find((snowflake) => inTypedSnowflake(user, snowflake)) !=
undefined
); // Ughh this whole chain is super messy but it works. I don't want to touch this again
};
const isGuildMember = (tbd: User | GuildMember): tbd is GuildMember =>
(tbd as GuildMember).guild ? true : false;