-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (90 loc) · 2.88 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*jshint esversion: 6 */
// discord.js module
const Discord = require('discord.js');
// Discord Token, located in the Discord application console - https://discordapp.com/developers/applications/me/
const { token, prefix } = require('./config.json');
//Maximum users to tag
const maxUsers = 50;
// Instance of Discord to control the bot
const bot = new Discord.Client();
// Gets called when the bot is successfully logged in and connected
bot.on('ready', function() {
var promises = [];
//Cache users at startup.
bot.guilds.forEach(guild => {
promises.push(guild.fetchMembers());
});
Promise.all(promises).then(() => {
bot.on('message', handleMessage);
});
});
function handleMessage(message) {
var author = message.author,
textChannel, voiceChannel, max;
if (message.author.bot) return;
// Check if the message starts with the prefix trigger
if (message.content.indexOf(prefix) === 0) {
// Get the user's message excluding the prefix
var args = message.content.substring(1);
args = args.split(' ');
if (args[0] === 'notifyrole') {
notifyRole(message, args).then(resp => {
if (typeof resp === 'string') {
message.channel.send(resp);
}
}).catch(err => {
console.log(err);
});
}
}
}
/**
* Will tag (mention) online users in a given role.
*
* @param {Message} message
* @param {Array} args
*/
function notifyRole(message, args) {
return new Promise((resolve, reject) => {
let guild,
user,
role,
onlineUsers,
mentions = [],
subject;
if (!args.length) {
resolve('No role supplied');
return;
}
user = message.author;
//Check to see if the user is allowed to use
guild = message.channel.guild;
role = guild.roles.find(role => {
return role.name === args[1];
});
if (!role) {
resolve('Role does not exist');
return;
}
//Perhaps do some checks to see if its one of your self/bot assignable roles.
onlineUsers = role.members.filter(member => {
return member.presence.status === 'online';
});
//So its not pinging huge amounts of people... I dunno #lied2Agen
if (onlineUsers.size > maxUsers) {
resolve('Too many users');
return;
}
if (onlineUsers.size === 0) {
resolve('No users are online');
return;
}
onlineUsers.forEach(user => {
mentions.push(user.toString());
});
subject = 'Hey, online \'' + role.name + '\' members ^ ';
subject = subject + '\n\n' + mentions.join(' ');
resolve(subject);
});
}
bot.login(token);