forked from Fostecks/pepospin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (224 loc) · 8.76 KB
/
Copy pathindex.js
File metadata and controls
260 lines (224 loc) · 8.76 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const Commando = require("discord.js-commando");
const {token, BOT_CLIENT_ID} = require('./config.json')
const Trie = require('./trie');
const bot = new Commando.Client({
disableEveryone: true,
unknownCommandResponse: false,
});
const PRIMARY_DISCORD_GUILD_NAME = "Underground Radio";
const BOT_CHANNEL_NAME = "bot";
const textChannelBlacklist = ["rules", "general", "monstercat-album-art"];
const LINK_REGEX = /https:\/\/(www\.)?(youtube.com|youtu.be)[a-zA-Z.0-9\/?=&-_]+/g;
let radioMap = {};
let radioTrie;
let latestBotMessage;
let latestCommandMessage;
bot.login(token);
/*******************************
* EVENT HANDLERS *
* responsible for radioMap *
******************************/
/************
* ON READY *
***********/
bot.on('ready', async () => {
await constructRadioMap();
radioTrie = new Trie(Object.keys(radioMap));
bot.birthdate = Date.now();
bot.registry.registerGroup("cmd", "Commands");
bot.registry.registerGroup("debug", "Debug");
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + '/commands');
console.log("bot ready");
//pin message with bot commands if there isn't one already
let botChannel = bot.primaryDiscordGuild.channels.find(channel => channel.name === BOT_CHANNEL_NAME);
botChannel.fetchPinnedMessages().then(messages => {
if(!messages || !messages.size) {
pinHelpMessage(botChannel);
}
})
});
/******************
* ON NEW MESSAGE *
*****************/
bot.on('message', async (message) => {
if(!message.guild || message.guild.name !== PRIMARY_DISCORD_GUILD_NAME) return;
//is message in bot channel
if(message.channel.name === BOT_CHANNEL_NAME) {
//keep latest command message
if(message.content.startsWith("!")) {
latestCommandMessage = message;
}
//keep latest bot reply
else if(message.author.id === BOT_CLIENT_ID) {
latestBotMessage = message;
}
//delete every other message
message.channel.fetchMessages().then(async (messages) => {
let messagesToDelete = messages.array().filter(x => {
let isLastCommandMessage = latestCommandMessage && x.id === latestCommandMessage.id;
let isLastBotMessage = latestBotMessage && x.id === latestBotMessage.id;
return !isLastCommandMessage && !isLastBotMessage && !x.pinned;
});
message.channel.bulkDelete(messagesToDelete);
})
}
//else if message is a new song in a radio channel
else {
if(!textChannelBlacklist.includes(message.channel.name)) {
let links = message.content.match(LINK_REGEX);
if(links) {
if(message.channel.name in radioMap) {
console.log("Message added in " + message.channel.name + ": Adding link(s): " + links);
radioMap[message.channel.name] = radioMap[message.channel.name].concat(links);
}
else {
console.log("Message added in NEW channel " + message.channel.name + ": Adding link(s): " + links);
radioMap[message.channel.name] = links;
radioTrie.add(message.channel.name);
}
}
}
}
});
/*********************
* ON MESSAGE UPDATE *
********************/
bot.on('messageUpdate', (oldMessage, newMessage) => {
if(!oldMessage.guild || oldMessage.guild.name !== PRIMARY_DISCORD_GUILD_NAME) return;
if(oldMessage.channel.name === BOT_CHANNEL_NAME) return;
// delete old links
if(!oldMessage) return;
let oldLinks = oldMessage.content.match(LINK_REGEX);
if (oldLinks) {
for(oldLink of oldLinks) {
let oldLinkIndex = radioMap[oldMessage.channel.name].indexOf(oldLink);
if(oldLinkIndex > -1 ) {
radioMap[oldMessage.channel.name].splice(oldLinkIndex, 1);
console.log("Message updated in " + oldMessage.channel.name + ": Deleted a link: " + oldLink);
}
}
}
//add new links
let newLinks = newMessage.content.match(LINK_REGEX);
if(newLinks) {
radioMap[newMessage.channel.name] = radioMap[newMessage.channel.name].concat(newLinks);
console.log("Message updated in " + newMessage.channel.name + ": Adding new link(s): " + newLinks);
}
});
/*********************
* ON CHANNEL UPDATE *
********************/
bot.on('channelUpdate', (oldChannel, newChannel) => {
if(oldChannel.guild.name !== PRIMARY_DISCORD_GUILD_NAME) return;
if(oldChannel.name !== newChannel.name) {
if(radioMap[newChannel.name] !== undefined) {
console.log("WARNING: Duplicate channel: " + newChannel.name + ". RadioMap has become out of sync. A purge is required.");
return;
};
radioMap[newChannel.name] = radioMap[oldChannel.name];
delete radioMap[oldChannel.name];
radioTrie.remove(oldChannel.name);
radioTrie.add(newChannel.name);
console.log("Updated radioMap channel: " + oldChannel.name + " with new name: " + newChannel.name);
}
});
/*********************
* ON MESSAGE DELETE *
********************/
bot.on('messageDelete', deletedMessage => {
if(!deletedMessage.guild || deletedMessage.guild.name !== PRIMARY_DISCORD_GUILD_NAME) return;
if(deletedMessage.channel.name === BOT_CHANNEL_NAME) return;
// delete old links
let oldLinks = deletedMessage.content.match(LINK_REGEX);
if (oldLinks) {
for(oldLink of oldLinks) {
let oldLinkIndex = radioMap[deletedMessage.channel.name].indexOf(oldLink);
if(oldLinkIndex > -1 ) {
radioMap[deletedMessage.channel.name].splice(oldLinkIndex, 1);
console.log("Message deleted in " + deletedMessage.channel.name + ": Deleted a link: " + oldLink);
}
}
}
});
/*********************
* ON CHANNEL DELETE *
********************/
bot.on('channelDelete', deletedChannel => {
if(deletedChannel.guild.name !== PRIMARY_DISCORD_GUILD_NAME) return;
if(radioMap[deletedChannel.name] === undefined) return;
delete radioMap[deletedChannel.name];
radioTrie.remove(deletedChannel.name);
console.log("Deleted channel from radioMap: " + deletedChannel.name);
});
/********************************
* PRIVATE HELPERS *
*******************************/
function constructRadioMap() {
let time1 = Date.now();
console.log("Constructing radio map...")
radioMap = {};
bot.primaryDiscordGuild = bot.guilds.find(guild => guild.name === PRIMARY_DISCORD_GUILD_NAME);
let textChannels = bot.primaryDiscordGuild.channels.filter(channel => channel.type === "text");
const constructRadioMapPromise = Promise.all(textChannels
.filter(textChannel => !textChannelBlacklist.includes(textChannel.name))
.map(textChannel => textChannel.fetchMessages())
).then(allMessages => {
allMessages.forEach(channelMessages => {
let messageArray = channelMessages.array();
if(messageArray.length === 0) return;
let linkCollection = collectLinks(messageArray);
let channelName = messageArray[0].channel.name;
if(linkCollection && linkCollection.length > 0) {
radioMap[channelName] = linkCollection;
}
})
let time2 = Date.now();
let time = time2 - time1;
console.log("Completed constructing radio map in " + time + " ms.")
});
return constructRadioMapPromise;
}
function pinHelpMessage(channel) {
let helpMessage = "```css\n" +
"[COMMANDS]\n" +
"1. !join : Joins your voice channel\n" +
"2. !play <channel> : plays Youtube links from <channel>\n" +
"3. !random : plays a random <channel>\n" +
"4. !playall : plays links from every channel\n" +
"5. !skip : skips currently playing link and plays next link\n" +
"6. !repeat : play currently playing link again when it ends\n" +
"7. !leave: end audio stream and leave voice channel\n" +
"```";
channel.send(helpMessage).then(message => {
message.pin();
});
}
/**
* Given an array containing discord.js Messages, harvests links
* and returns an array of links.
* @param {Message[]} messageArray
*/
function collectLinks(messageArray) {
let linkCollection = [];
for(let i = 0; i < messageArray.length; i++) {
let message = messageArray[i].content;
let links = message.match(LINK_REGEX);
if(links) {
linkCollection = linkCollection.concat(links);
}
}
return linkCollection;
}
function getMap() {
return radioMap;
}
function getTrie() {
return radioTrie;
}
module.exports = {
"bot": bot,
"getMap": getMap,
"getTrie": getTrie,
"constructRadioMap": constructRadioMap
}