-
Notifications
You must be signed in to change notification settings - Fork 38
/
Bot.js
190 lines (161 loc) · 4.72 KB
/
Bot.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
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
let Discord = require('discord.js');
let Bot = new Discord.Client();
let Helper = require('./components/helper');
let Queue = require('./components/queue');
let TrackHelper = require('./components/trackhelper.js');
let WordService = require('./components/wordservice.js');
let WeatherService = require('./components/weatherservice.js');
let commandIdentificator = '!';
let 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'
}
};
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));
});
} else {
TrackHelper.getRandomTrack(args, 5).then(track => {
Queue.add(track, message);
}).catch(err => {
message.reply(Helper.wrap(err));
});
}
}
function getVideo(args, message) {
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) {
toReturn = 'Available commands:\n'
.concat(Object.keys(commands)
.filter(command => command !== 'help')
.map(command => {
const data = commands[command];
return `${commandIdentificator}${command}: ${data.description}${getAvailableCommandAsText(data)}\n`;
})
.join(""))
}
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 isBotCommand(message) {
return message.content.startsWith(commandIdentificator) && message.author.id != Bot.user.id;
}
function execute(content, message) {
var args = content.split(" ");
var command = commands[args[0].replace(commandIdentificator, '')];
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('command', ['id'])
.then(keys => {
commandIdentificator = keys.id;
console.log(`using ${commandIdentificator} as command identificator`);
})
.catch(console.log)
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']);
console.log("SupahBot started.");
})
.catch(console.error);
}
init();