-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
187 lines (169 loc) · 5.42 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
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
'use strict';
var fs = require('fs');
var config = require('./config.json');
var Discord = require('discord.js');
var bot = new Discord.Client({ autoReconnect: true });
var stats;
var commands = new Map();
var triggerPrefix = config.commandTrigger + config.botPrefix + ' ';
commands.set(new RegExp(triggerPrefix + 'help', 'i'), ['function', displayCommands]);
commands.set(new RegExp(triggerPrefix + 'random', 'i'), ['function', playRandomSound]);
commands.set(new RegExp(triggerPrefix + 'popular', 'i'), ['function', sendPopularCommands]);
commands.set(new RegExp(triggerPrefix + 'exit', 'i'), ['function', leaveVoiceChannel]);
if(config.demoMode) {
commands.set(/liftoff/i, ['text', 'Houston, we have liftoff!']);
commands.set(/!smallstep/i, ['sound', 'smallstep.mp3']);
}
// commands.set(//i, ['', '']);
function incrementSoundStats(command) {
if(stats[command]) {
stats[command]++;
} else {
stats[command] = 1;
}
fs.writeFile(config.statsFileName, JSON.stringify(stats));
}
function loadStatsFile() {
fs.readFile(config.statsFileName, 'utf-8', function(error, data) {
if(error) {
if(error.code === 'ENOENT') {
fs.writeFileSync(config.statsFileName, JSON.stringify({}));
stats = {};
} else {
console.log('Error: ', error);
}
} else {
try {
stats = JSON.parse(data);
} catch(parsingError) {
console.log('Error parsing JSON: ', parsingError);
}
}
});
}
function fileToCommand(file) {
return config.commandTrigger + file.split('.')[0].split('-').join(' ');
}
function regExpToCommand(command) {
return command.toString().split('/')[1];
}
function addSoundsTo(map, fromDirectoryPath) {
var soundFiles = fs.readdir(fromDirectoryPath, function(err, files) {
files.forEach(function(file) {
if(file[0] !== '.') {
var command = fileToCommand(file);
var commandRegExp = new RegExp(command, 'i');
map.set(commandRegExp, ['sound', file]);
}
});
});
}
function sendMessage(authorChannel, text) {
bot.sendMessage(authorChannel, text);
}
function leaveVoiceChannel(message) {
if(bot.voiceConnections.get('server', message.server)) {
bot.voiceConnections.get('server', message.server).destroy();
}
}
function playSound(authorChannel, authorVoiceChannel, command, sound) {
bot.joinVoiceChannel(authorVoiceChannel).then(function(connection, joinError) {
if(joinError) {
var joinErrorMessage = 'Error joining voice channel: ';
console.log(joinErrorMessage, joinError);
bot.sendMessage(authorChannel, joinErrorMessage + joinError);
}
connection.playFile(config.soundPath + sound).then(function(intent, playError) {
if(playError) {
var playErrorMessage = 'Error playing sound file: ';
console.log(playErrorMessage, playError);
bot.sendMessage(authorChannel, playErrorMessage + playError);
}
intent.on('error', function(streamError) {
var streamErrorMessage = 'Error streaming sound file: ';
console.log(streamErrorMessage, streamError);
bot.sendMessage(authorChannel, streamErrorMessage + streamError);
});
incrementSoundStats(command);
if(config.autoLeaveVoice) {
intent.on('end', function() {
connection.destroy();
});
}
});
});
}
function sendPopularCommands(message) {
var total = 0;
var statsArray = [];
var popularMessage = '';
for(var key in stats) {
if(stats.hasOwnProperty(key)) {
statsArray.push([key, stats[key]]);
total += stats[key];
}
}
statsArray.sort(function(a, b) {
return b[1] - a[1];
});
var i = 0;
while(i < statsArray.length && i < 5) {
popularMessage += statsArray[i][0] + ' — ' + Math.round((statsArray[i][1] / total) * 100) + '%\n';
i++;
}
bot.sendMessage(message.channel, popularMessage);
}
function playRandomSound(message) {
var keys = [...commands.keys()];
var randomKey;
var randomValue = ['', ''];
while(randomValue[0] !== 'sound') {
randomKey = keys[Math.round(keys.length * Math.random())];
randomValue = commands.get(randomKey);
}
playSound(message.channel, message.author.voiceChannel, regExpToCommand(randomKey), randomValue[1]);
}
function displayCommands(message) {
var helpMessage = '';
if(message.content.split(' ')[2]) {
var helpFilter = new RegExp(message.content.split(' ')[2], 'i');
commands.forEach(function(fileName, command){
if(command.toString().match(helpFilter)) {
helpMessage += regExpToCommand(command) + '\n';
}
});
} else {
commands.forEach(function(fileName, command){
helpMessage += regExpToCommand(command) + '\n';
});
}
bot.sendMessage(message.channel, helpMessage);
}
bot.on('message', function(message) {
if(message.author.username !== bot.user.username) {
commands.forEach(function (botReply, regexp) {
if(message.content.match(regexp)) {
switch(botReply[0]) {
case 'function':
botReply[1](message);
break;
case 'sound':
playSound(message.channel, message.author.voiceChannel, regExpToCommand(regexp), botReply[1]);
break;
case 'text':
sendMessage(message.channel, botReply[1]);
break;
default:
break;
}
}
});
}
});
(function init() {
bot.loginWithToken(config.botToken);
if(config.autoLoadSounds) {
addSoundsTo(commands, config.soundPath);
}
loadStatsFile();
})();