-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobalfunctions.js
341 lines (225 loc) · 9.35 KB
/
globalfunctions.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const imports = require('./imports');
const globalVars = require('./globalvars');
const vars = require('./globalvars');
var user_timeout = new Map();
const axios = require('axios');
var mysql = require('mysql-await');
const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice')
require('dotenv').config();
module.exports = {
GetSingleMentionAfterCommandUsage: function GetSingleMentionAfterCommandUsage(msg) {
var msgcontent = msg.content;
if (msgcontent.startsWith(`<@!${imports.bot.user.id}>` + " ")) {
var indexOfFirstSpace = msgcontent.indexOf(" ");
if (indexOfFirstSpace != -1) {
msgcontent = msgcontent.substr(indexOfFirstSpace + 1);
}
}
var userMentionedForAvatar = null;
var indexOfFirstSpace = msgcontent.indexOf(" ");
if (indexOfFirstSpace != -1) {
userMentionedForAvatar = msgcontent.substr(indexOfFirstSpace + 1);
var indexOfSecondSpace = userMentionedForAvatar.indexOf(" ");
if (indexOfSecondSpace != -1) {
userMentionedForAvatar = userMentionedForAvatar.substr(0, indexOfSecondSpace);
}
}
return userMentionedForAvatar;
},
CommandHandler: function CommandHandler(msg, commands) {
var prefix = ">>"; // Should be later get from the database to change the prefix.
const message = msg.content.toLowerCase();
var messageString = message;
var indexOfFirstSpace = messageString.indexOf(" ");
if (commands !== undefined) {
for (var i = 0; i < commands.length; i++) {
var commandWithPrefixAlt = prefix + commands[i];
var commandWithBotMentionAlt = `<@!${imports.bot.user.id}>` + " " + commands[i];
if (messageString.startsWith(commandWithBotMentionAlt)) {
if (indexOfFirstSpace != -1) {
messageString = messageString.substr(indexOfFirstSpace + 1);
var indexOfSecondSpace = messageString.indexOf(" ");
if (indexOfSecondSpace != -1) {
messageString = messageString.substr(0, indexOfSecondSpace);
}
}
}
else if (messageString.startsWith(commandWithPrefixAlt)) {
if (indexOfFirstSpace != -1) {
messageString = messageString.substr(0, indexOfFirstSpace);
var indexOfSecondSpace = messageString.indexOf(" ");
if (indexOfSecondSpace != -1) {
messageString = messageString.substr(0, indexOfSecondSpace);
}
}
}
else {
}
if (messageString == commandWithPrefixAlt || messageString == commands[i]) {
if (module.exports.CommandTimeout(msg)) return true;
}
}
}
return false;
},
CommandTimeout: function CommandTimeout(msg) {
//TODO: Fix the memory leak I just created...
//TODO: Check if user is premium, will be from the database.
//var timeoutTime = 5000; // This will be production timeouts for a user for fun commands. Some command will need to be not limited by timeout, such as moderation ones.
var timeoutTime = 0;
if(user_timeout.has(msg.author.id)){
if(user_timeout.get(msg.author.id) > Date.now()){
msg.channel.send("Please wait 5 seconds before running a command again\nTo bypass this you can buy premium.");
return false;
} else{
user_timeout.set(msg.author.id, Date.now() + timeoutTime);
return true;
}
} else {
user_timeout.set(msg.author.id, Date.now() + timeoutTime);
return true;
}
},
// Checks if this is production or not, held in the .evn file that is being git ignored.
CheckProductionMode: function CheckProductionMode() {
const ISTHISPRODUCTION = process.env.ISTHISPRODUCTION;
if (ISTHISPRODUCTION == "TRUE") {
return true;
}
return false;
},
// You can make it force sleep (wait) if you need to.
// Only use this in async functions, otherwise you're gonna make whatever you are doing lag hard.
// Sometimes the bot might respond too fast lol
ForceSleep: function ForceSleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
},
GetAllFilesFromPath: function GetAllFilesFromPath(dirPath, existingArrayOfFiles) {
files = require("fs").readdirSync(dirPath);
var arrayOfFiles = existingArrayOfFiles || []; // if there is an existing array of files we can use that, or just create a new array
files.forEach(function (file) {
if (require("fs").statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = GetAllFilesFromPath(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(require("path").join(dirPath, "/", file));
}
})
return arrayOfFiles;
},
PlaySong: async function play(guild, song, loop = 0, start = 0, msg = null) {
const serverQueue = globalVars.MusicQueue.get(guild.id);
if (!song || song === null) {
//serverQueue.voiceChannel.leave();
//imports.MusicQueue.delete(guild.id);
serverQueue.textChannel.send("No songs in queue, Waiting for a song.");
return;
}
//globalVars.distube.playVoiceChannel(serverQueue.voiceChannel, song.url);
/*
if(song.type == 0){
serverQueue.textChannel.send("Connecting to `https://enthix.net/yt2source.php`...");
await axios.get("https://enthix.net/yt2source.php?key=UIAHDFJSDFHSJKDSHFKHKJFEWIUYEUIWYEWUYISJBBJBNMCXIYUOWEUUDSJHKKJSLALAOPPOFJBJHEHEHHHHEEXNBNBNBCNXBNBXXXX&yt=" + song.url).then(function (response) {
if(response.data == "404"){
serverQueue.textChannel.send(":underage: **Video age-restricted.**\nWe can't play this video because its age restricted.\nYou need to connect your YouTube account to play age-restricted content.\nhttps://enthix.net/connect2music");
} else {
serverQueue.textChannel.send("decoded source returned `" + response.data + "`");
vars.audioResource = createAudioResource(response.data, {
inlineVolume: true
});
}
}).catch(function (error) {
serverQueue.textChannel.send("Error while decoding source ```" + error + "```");
console.log(error);
return;
})
//const stream = await ytdl(song.url, { highWaterMark: 1 << 25 });
} else if(song.type == 1 || song.type == 2 || song.type == 3){
*/
vars.audioResource = createAudioResource(song.url, {
inlineVolume: true
});
//}
vars.audioPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play
}
});
vars.audioResource.volume.setVolume(serverQueue.volume / 100);
vars.audioPlayer.play(vars.audioResource, { seek: start });
serverQueue.connection.subscribe(vars.audioPlayer);
// --------- Events
vars.audioPlayer.on(AudioPlayerStatus.Playing, () => {
//module.exports.musicTimeLeft(song);
if(loop == 0 && start == 0 && msg == null) serverQueue.textChannel.send(":arrow_forward: Start playing `" + song.title + "`");
if(loop == 0 && start == 0 && msg !== null) msg.edit(":arrow_forward: Start playing `" + song.title + "`");
});
vars.audioPlayer.on(AudioPlayerStatus.Idle, () => {
next_song();
});
vars.audioPlayer.on('error', error => {
console.error(error);
});
// ----------------
function next_song(){
if(globalVars.MusicLoop){
song.timeleft = 0;
module.exports.PlaySong(guild, serverQueue.songs[0], 1);
} else {
serverQueue.songs.shift();
vars.vote_skip_song = [];
module.exports.PlaySong(guild, serverQueue.songs[0], 0);
//vars.audioPlayer.destroy();
}
}
/*
console.log(song.url);
if(song.type == 0) vars.dispatcher = serverQueue.connection.play(await ytdl.stream(song.url).stream, { seek: start });
if(song.type == 1 || song.type == 2 || song.type == 3) vars.dispatcher = serverQueue.connection.play(song.url, { seek: start });
module.exports.musicTimeLeft(song);
//vars.dispatcher = serverQueue.connection.play(play_api, { seek: start }); //, quality: 'highestaudio', highWaterMark: 1 << 25
//serverQueue.connection.subscribe(serverQueue.player);
vars.dispatcher.on("finish", () => {
serverQueue.textChannel.send("SONG FINISHED.");
next_song();
});
vars.dispatcher.on("end", () => {
serverQueue.textChannel.send("SONG HARD END.");
next_song();
});
vars.dispatcher.on("error", error => serverQueue.textChannel.send("An error accured, please inform MVDW: \n```" + error + "```"));
//console.log(serverQueue.volume);
vars.dispatcher.setVolumeLogarithmic(serverQueue.volume / 100);
if(loop == 0 && start == 0) serverQueue.textChannel.send(":arrow_forward: Start playing ``" + song.title + "``\nNote: Because we are buffering the stream now for performance it may stutter at the start.");
*/
},
toHHMMSS: function (sec) {
var sec_num = parseInt(sec, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
},
toHex: function(data) {
if(!data) return;
var arr1 = [];
for (var n = 0, l = data.length; n < l; n ++) {
var hex = Number(data.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join('');
},
con: mysql.createConnection({
host: process.env.MYSQLHOST,
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASS,
database: process.env.MYSQLDB,
socketPath: "/var/run/mysqld/mysqld.sock"
}),
musicTimeLeft: function(song){
}
};