-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
39 lines (31 loc) · 1.21 KB
/
utils.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
const { DMChannel, GuildChannel, NewsChannel, TextChannel, Permissions } = require('discord.js');
class ClientUtils {
static async findNotifyChannel(guild) {
// Prefer the system channel
let systemChannel = await guild.channels.fetch(guild.systemChannelId);
if (systemChannel && PermissionUtils.canSend(systemChannel, true)) { return systemChannel; }
// Otherwise look for a text/news channel
return (await guild.channels.fetch()).find(
channel =>
(channel instanceof TextChannel || channel instanceof NewsChannel) &&
PermissionUtils.canSend(channel, true)
);
}
}
class PermissionUtils {
static canSend(channel, embedLinks = false) {
if (channel instanceof DMChannel) {
return true;
} else if (channel instanceof GuildChannel) {
let channelPerms = channel.permissionsFor(channel.client.user);
return channelPerms.has([
Permissions.FLAGS.VIEW_CHANNEL, // Needed to view the channel
Permissions.FLAGS.SEND_MESSAGES, // Needed to send messages
...embedLinks ? [Permissions.FLAGS.EMBED_LINKS] : [], // Needed to send embedded links
]);
} else {
return false;
}
}
}
module.exports = { ClientUtils, PermissionUtils };