forked from kriller1612/datbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
282 lines (245 loc) · 9.57 KB
/
bot.js
File metadata and controls
282 lines (245 loc) · 9.57 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Here come DatBot - oh shit waddup!
*/
// Various imports
const Discord = require('discord.js'); // Import the discord.js module
const Playback = require('./playback.js'); // Import our own player
const config = require('./config.json'); // Import json files
const quotes = require('./quotes.json');
const embeds = require('./embeds.js');
// Instanciate classes
const client = new Discord.Client(); // Create an instance of a Discord client
const pb = new Playback(client); // Create an instance of our player
// The ready event is vital, it means that tbe bot will only start reacting to information
// from Discord _after_ ready is emitted
client.on('ready', () => {
console.log('I am ready!');
});
/**
* React to a given message with a given emote.
* If a limit greater than 1 is specified, previous messages
* in the thread will also recieve a reaction
* @param {*} message
* @param {*} emote
* @param {*This should be at least 1} limit
*/
function react (message, emote, limit = 1) {
message.channel.fetchMessages({limit: limit})
.then((result) => {
result.forEach(message => {
message.react(emote);
})
});
}
/**
* Determines if a member is connected to the
* same voice connection as the bot
* @param {*} member
*/
function inVoice(member) {
try {
const map = member.voiceChannel.members;
const array = Array.from(map.keys());
return array.includes(config.id);
}
catch (error) {
return false;
}
}
/**
* Do something if a message contains a substring
* of one of the specified triggers
* @param {*A message!} message
* @param {*An array!} trigger
* @param {*A function!} callback
*/
function scan(content, trigger, func) {
for (let t = 0; t < trigger.length; t++) {
if (content.indexOf(trigger[t]) !== -1) {
return func();
}
}
}
// Create an event listener for messages
client.on('message', message => {
// Defining emotes here
const ermin = client.emojis.find("name", "incest");
// Find words in the message
const words = message.content.split(" ");
// Switch between different cases
switch(words[0]) {
case '!ping':
// Pong the pinger!
message.reply('pong!');
message.channel.send(embeds.ping(client));
break; // Remember to break!
case '!help':
// Send a pm with a table of available commands
message.author.send(embeds.help(config.link));
break;
case '!code': // Nesting cases creates synonyms!
case '!github': //(Because there are no break statements)
case '!source':
// Send a link to the source code
message.channel.send(`Thats right, ${message.author} !\nYou\'re welcome to add features to the bot - ${config.link}`);
break;
case '!ermin':
// Quote the man himself
if (words[1] != undefined) {
// Is a specific quote being requested?
if (quotes.ermin[words[1]] != undefined) {
message.channel.send(`${ermin} ${quotes.ermin[words[1]]} ${ermin}`)
}
else {
// Handling invalid inputs
if (quotes.ermin.amount < words[1]) {
message.channel.send(`I do not know that many ermin quotes, ${message.author} !`);
}
else {
message.channel.send("You must specify an Integer value, " + message.author + " !\nTry: `!ermin { Integer }`");
}
}
}
else {
// If no specific quote is being requested, choose one at random
const random_choice = Math.ceil(Math.random()*quotes.ermin.amount)
message.channel.send(`${ermin} ${quotes.ermin[random_choice]} ${ermin}`)
}
break;
case '!react':
// React with Ermin's face on the last five messages posted
react(message, ermin, 5);
break;
case '!dropud':
// Help out a fellow in need
message.reply("www.dropud.nu/pizza");
break;
case '!play':
// Playback link passed as parameter
try {
if (message.member.voiceChannel != undefined) {
if (words[1] != undefined) {
// Pass parameter given to queue
const query = words.splice(1, words.length - 1).join(' ');
// Start playing
pb.queue(query, message);
}
else {
message.channel.send(`You must specify a valid YouTube link or search query, ${message.author} !`);
// Suggestion with formatting
}
}
else {
// handle incorrect input - no link specified
message.channel.send(`You must be in a voice channel as me to listen to music, ${message.author} !`);
}
}
catch (error) {
console.log(error);
message.channel.send(`I'm terribly sorry but I simply cannot do that, ${message.author} !`);
}
break;
case '!volume':
// Changes volume to specified value if integer value
// is specified, else return volume in db
if (pb.playing && !pb.paused) {
if (words[1]) {
if (inVoice(message.member)) {
if (words[1] < 11 && words[1] > 0) {
try {
pb.setVolume(words[1]);
}
catch (error) {
message.channel.send(`Invalid input, expected Integer, ${message.author}`);
}
}
else {
message.channel.send('The integer value specified must be between 1 and 10 inclusive, ' + message.author + " !");
}
}
else {
message.channel.send(`You must be in the same voice channel as me to issue voice commands, ${message.author} !`);
}
}
else {
message.channel.send("The current playback volume is " + pb.getVolume() + "/10. To change this, do `!volume { Integer }`, where the integer value is between 1 and 10 inclusive, " + message.author + " !");
}
}
else {
message.channel.send(`You must be listening to music in order to change the volume, ${message.author} !`);
}
break;
case '!pause':
// Pause playback
if (inVoice(message.member) && pb.playing && !pb.paused) {
pb.pause();
}
else {
message.reply("you must be in the same voice channel as me and the music must be playing!")
}
break;
case '!resume':
// Resume playback
if (inVoice(message.member) && pb.playing && pb.paused) {
pb.resume();
}
else {
message.reply("you must be in the same voice channel as me and the music must be paused!")
}
break;
case '!playlist':
case '!queue':
// Retrived all queued songs
// Format print: https://leovoel.github.io/embed-visualizer/
break;
case '!skip':
// Skip song currently playing and play next in queue
if (inVoice(message.member) && pb.playing) {
pb.skip();
}
else {
message.reply("nothing to skip!")
}
break;
case '!remaining':
// Retrieves the time remaining of
// the song currently playting
if (pb.playing) {
pb.remaining(message);
}
else {
message.channel.send(`There's no music playing, ${message.author} !`);
}
}
const content = message.content.toLowerCase();
scan(content,
["incest", "ermin", "søster"],
() => react(message, ermin));
scan(content,
["fuck", "shit", "lort", "røvhul"],
() => message.reply("tal ordenligt!"));
scan(content,
["?"],
() => message.channel.send(
`Hej ${message.author}\n`
+ "Du skal være velkommen til at sende dine spørgsmål ind til Ermin Rayquaza Obradovacs DevelopmentTeam på følgende e - mail: obradovacpower @gmail.com\n"
+ "\n"
+ "Din mail vil blive besvaret inden 24 timer\n"
+ "Vh Leader of Ermin Rayquaza Obradovac A/S"
))
// Add handlers for other mentions here
});
client.on('voiceStateUpdate', member => {
if (member.voiceChannel != undefined) {
const map = member.voiceChannel.members;
//const array = Array.from(map.keys());
if(pb.playing && map.size == 1 && inVoice(member)) {
// Disconnect from voice chat if no one's listening
pb.terminate();
}
}
})
// Log our bot in with
// the token of the bot -
// https://discordapp.com/developers/applications/me
client.login(config.token);