-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.js
More file actions
206 lines (172 loc) · 5.19 KB
/
Bot.js
File metadata and controls
206 lines (172 loc) · 5.19 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
var Discord = require('discord.js');
var Bot = new Discord.Client();
var Helper = require('./components/helper.js');
var Queue = require('./components/queue.js');
var TrackHelper = require('./components/trackhelper.js');
var WordService = require('./components/wordservice.js');
var WeatherService = require('./components/weatherservice.js');
var killsearch = false
const blacklist = require("./tagblacklist.json");
var commands = {
'!video': {
execute: getVideo,
description: 'get a youtube video by search word'
},
'!weather': {
execute: getWeather,
description: 'get current weather for the given city, defaults to Stockholm'
},
'!roll': {
execute: roll,
description: 'roll from 1-100'
},
'!help': {
execute: showHelp
},
'!words': {
execute: countWordsByUser,
description: 'get the most popular words for user of the given username, defaults to your username'
},
'!queue': {
execute: doQueue,
description: 'queue your song'
},
'!voteskip': {
execute: voteSkip,
description: 'vote to skip the current song'
},
'!song': {
execute: showSong,
description: 'get the current song'
},
'!dev': {
execute: devList,
description: 'Print the smelly developer of Traza'
}
};
Bot.on('message', message => {
WordService.registerMessage(message);
if (isBotCommand(message)) {
execute(message.content, message);
}
});
function showSong(args, message) {
Queue.showSong(message);
}
function voteSkip(args, message) {
Queue.voteSkip(message);
}
function doQueue(args, message) {
if (args.length <= 0) {
return message.reply(Helper.wrap('Type of music need to be specified.'));
}
if (Queue.isFull()) {
return message.reply(Helper.wrap('Queue is full.'));
}
if (args.startsWith('http')) {
TrackHelper.getVideoFromUrl(args).then(track => {
Queue.add(track, message);
}).catch(err => {
message.reply(Helper.wrap(err));
});
TrackHelper.getRandomTrack(args, 5).then(track => {
Queue.add(track, message);
}).catch(err => {
message.reply(Helper.wrap(err));
});
}}
function getVideo(args, message) {
for (var i = 0; i < args.length; i++) {
for(var j = 0; j < blacklist.nsfwtags.length; j++)
{
if(blacklist.nsfwtags[j].indexOf(args[i]) != -1)
{
var killsearch = true;
}
}
}
if (killsearch == true){
message.reply("Sorry, I can't do that")
} else {
TrackHelper.getRandomTrack(args, 5).then(track => {
message.reply(track.url);
}).catch(err => {
message.reply(Helper.wrap(err));
});
}}
function countWordsByUser(args, message) {
WordService.countWordsByUser(args, message);
}
function getWeather(args, message) {
WeatherService.getWeather(args, message);
}
function showHelp(args, message) {
var toReturn = 'No commands to run!';
if (Object.keys(commands).length > 1) {
var toReturn = 'Available commands:\n';
for (var command in commands) {
if (command != '!help') {
data = commands[command];
toReturn += command + ': ' + data.description + getAvailableCommandAsText(data) + '\n';
}
}
}
message.reply(Helper.wrap(toReturn));
}
function getAvailableCommandAsText(command) {
if (!Helper.commandIsAvailable(command)) return ' (not available)';
return '';
}
function roll(content, message) {
message.reply(Helper.wrap('You rolled ' + getRandomNumber(1, 100) + ' (1-100)'));
}
function devList(content, message) {
message.reply('Made by the esteemed <@135821957844172800>');
}
function isBotCommand(message) {
if (message.content.startsWith('!') && message.author.id != Bot.user.id) {
return true;
}
return false;
}
function execute(content, message) {
var args = content.split(" ");
var command = commands[args[0]];
if (command) executeCommand(command, message, args);
}
function executeCommand(command, message, args) {
if (!Helper.commandIsAvailable(command)) {
return message.reply(Helper.wrap('Command is not available.'));
}
command.execute(getCommandArguments(args), message);
}
function getCommandArguments(args) {
var withoutCommand = args.slice(1);
return withoutCommand.join(" ");
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function registerService(service, affectedCommands) {
service = new service();
if (affectedCommands) {
affectedCommands.forEach(command => {
var c = commands[command];
if (c) {
if (!c.services) c.services = [];
c.services.push(service);
}
});
}
return service;
}
function init() {
Helper.keys('apikeys', ['discord']).then(keys => {
Bot.login(keys.discord);
Queue = registerService(Queue, ['!queue', '!voteskip', '!song']);
TrackHelper = registerService(TrackHelper, ['!queue', '!video']);
WordService = registerService(WordService, ['!words']);
WeatherService = registerService(WeatherService, ['!weather']);
}).catch(console.error);
}
init();