-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
284 lines (246 loc) · 7.36 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
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
283
284
const Discord = require('discord.js');
const client = new Discord.Client();
const channelName = 'frostbolt';
const useSpecificChannel = false;
const prefix = "%";
const requirePrefix = false;
const initMessage = 'Frostbolt initialized.';
const sendInitMessage = false;
let regCommands = [];
let connectedChannels = [];
let ytdl = require('ytdl-core');
let request = require('request');
let fs = require('fs');
let isPlaying = false;
const tokenData = require('./token.json');
const token = tokenData.token;
client.login(token);
client.on('ready', () =>
{
console.log('Connected as ' + client.user.tag);
client.user.setActivity('hentai', { type: 'WATCHING' });
//For each server, log that it has connected
client.guilds.cache.forEach((guild) =>
{
console.log('Connected to ' + guild.name);
//For each server with the designated channel name, add it to the connectedChannels array
for (let channel of guild.channels.cache)
{
if (channel.name == channelName)
{
let botChannel = client.channels.cache.get(channel.id);
connectedChannels.push(botChannel);
console.log('Connected to ' + channelName + ' in ' + guild.name);
}
}
});
//Load all commands from json file
loadCommands();
//If the bot is using Initlization messages, send it
if (sendInitMessage)
{
for (let channel of connectedChannels)
{
channel.send(initMessage);
}
}
});
client.on('message', (message) =>
{
//If message is bot or not designated channel, return
if (message.author == client.user || message.channel.name != channelName && useSpecificChannel)
{
return;
}
if (message.content.charAt(0) != prefix && requirePrefix)
{
return;
}
if (message.content.substring(0, 4).includes('play') && !isPlaying)
{
console.log('music');
isPlaying = true;
let link = message.content.substring(5, message.content.length);
console.log(link);
message.member.voice.channel.join().then((connection) =>
{
const dispatcher = connection.play(ytdl(link, { filter: "audioonly" }));
});
return;
}
let addInit = 0;
let addFinit = 3;
if (requirePrefix)
{
addInit = 1;
addFinit = 4;
}
//If is add message, and has a command after add prefix
if (message.content.substring(addInit, addFinit) == 'add' && message.content.length >= 5)
{
//If message has mp3 attachment, download it
if (message.attachments.first())
{
newCmd = message.content.substring(4, message.content.length).trim();
newCmd = removeSpecialChar(newCmd);
//Check if command already exists
for (let cmd of regCommands)
{
if (cmd.command == newCmd)
{
console.log('Command ' + newCmd + ' already exists' + ' (' + message.member.user.tag + ')');
message.reply("Command already exists");
return;
}
}
fn = message.attachments.first().name; //file name
fType = fn.substring(fn.length - 3, fn.length); //file type
if (fType == 'mp3')
{
console.log('Command: ' + newCmd + ', File: ' + newCmd + '.mp3');
console.log('Downloading mp3, requested by, ' + message.member.user.tag);
message.reply('Command added as: ' + "'" + newCmd + "'");
downloadMp3(message.attachments.first().url, newCmd);
addCommand(newCmd, newCmd + '.mp3');
return;
}
}
}
//Check each command for a match
for (let cmd of regCommands)
{
if (message.content.includes(cmd.command))
{
const vc = message.member.voice.channel;
if (vc == null)
{
return;
}
//If multiple files, pick a random one
let fileToPlay;
if (isArray(cmd.file))
{
let randomNum = Math.floor(Math.random() * cmd.file.length);
fileToPlay = cmd.file[randomNum];
} else
{
fileToPlay = cmd.file;
}
//Join channel then play file
vc.join().then((connection) =>
{
const dispatcher = connection.play(stringToSound(fileToPlay));
console.log(
'Sending command: ' +
cmd.command +
', requested by ' +
message.member.user.tag
);
dispatcher.on('finish', (finish) =>
{
vc.leave();
});
});
}
}
//Return commands list
let getCmdMessage = 'commands';
if (requirePrefix)
{
getCmdMessage = prefix + getCmdMessage;
}
if (message.content == getCmdMessage)
{
message.reply(getCommandList());
}
});
//Remove all special characters
function removeSpecialChar(str)
{
return str.replace(/[^a-zA-Z ]/g, '');
}
//Downloads and mp3 from a message
function downloadMp3(url, name)
{
console.log("Downloading " + url);
request.get(url).on('error', console.error).pipe(fs.createWriteStream('sounds/' + newCmd + '.mp3'));
}
//Load all commands from json file
function loadCommands()
{
regCommands = [];
//Load json file
const data = require('./commands.json');
//Add all json objects to commands array
for (i = 0; i < data.length; i++)
{
regCommands.push(data[i]);
}
//Notify for each command registered
for (let cmd of regCommands)
{
console.log('Registered command: ' + cmd.command);
}
}
//Adds new command to regCommands array, and re-writes json commands
function addCommand(command, file)
{
let newData = { "command": command, "file": file }
console.log("Added " + command + " at " + file);
regCommands.push(newData);
let jsonObj = JSON.stringify(regCommands);
fs.writeFile('./commands.json', jsonObj, 'utf8', function (err)
{
if (err)
{
console.log("Error occured writing to json file");
return;
}
})
}
//Returns a list of all available commands
function getCommandList()
{
let commandList = 'commands: ';
for (let cmd of regCommands)
{
commandList += cmd.command + ', ';
}
return commandList;
}
//Return if the object is an array
function isArray(obj)
{
return Object.prototype.toString.call(obj) === '[object Array]';
}
//Return a string with path to the sound folder
function stringToSound(str)
{
return 'sounds/' + str;
}
//Return a string with the path to the image folder
function stringToImage(str)
{
return 'images/' + str;
}
//Return a string with path and file name supplied (kinda pointless?)
function stringToFile(strPath, str)
{
return strPath + '/' + str;
}
//Return a string with every other letter uppercase
function stringToAlt(str)
{
let newStr = [];
for (i = 0; i < str.length; i++)
{
if (i % 2 == 0)
{
newStr.push(str.charAt(i).toUpperCase());
} else
{
newStr.push(str.charAt(i).toLowerCase());
}
}
return newStr.join('');
}