@AkimotoRyou/

vire-bot_js

Nodejs

No description

fork
loading
Files
  • index.js
  • config.json
  • keep_alive.js
  • package-lock.json
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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//----------------------------------------------------------------------------------------------

console.log('Loading Dependencies');
const Discord = require('discord.js');
const Util = require('discord.js');
const YouTube = require('simple-youtube-api');
const ffmpeg = require('ffmpeg-static')
const keep_alive = require('./keep_alive.js');
const config = require('./config.json');
const ytdl = require('ytdl-core-discord');

console.log('Loading Const');
const youtube = new YouTube(process.env.YOUTUBE_API);
const client = new Discord.Client();
const queue = new Map();
const information = '#add8e6';
const warning = '#ffff00';
const success = '#ff0000';

//----------------------------------------------------------------------------------------------

console.log('Loading Functions');
function getEmbed(color, title, description){
  var embed = new Discord.RichEmbed()
    .setColor(color)
    .setTitle(title)
    .setDescription(description)
    .setFooter(`${client.user.tag} by Asterisk*#6944`)
    .setTimestamp();
  return embed;
}
function getDMEmbed(color, title, field1, field2){
  var temp1 = field1.split(",");
  var temp2 = field2.split(",");
  var embed = new Discord.RichEmbed()
    .setColor(color)
    .setTitle(title)
    .addField(temp1[0], temp1[1])
    .addField(temp2[0], temp2[1])
    .setFooter(`${client.user.tag} by Asterisk*#6944`)
    .setTimestamp();
  return embed;
}
async function handleVideo(video, message, voiceChannel, playlist = false){
  const serverQueue = queue.get(message.guild.id);
  console.log(video);
  const song = {
      id: video.id,
      title: Util.escapeMarkdown(video.title),
      url: `https://www.youtube.com/watch?v=${video.id}`
  };
  if(!serverQueue){
      const queueConstruct = {
          textChannel: message.channel,
          voiceChannel: voiceChannel,
          connection: null,
          songs: [],
          volume: 5,
          playing: true
      };
      queue.set(message.guild.id, queueConstruct);

      queueConstruct.songs.push(song);

      try{
          var connection = await voiceChannel.join();
          queueConstruct.connection = connection;
          play(message.guild, queueConstruct.songs[0]);
      } catch(error){
          console.error(`Can't join voice channel.`);
          queue.delete(message.guild.id);
          var embed = getEmbed(success, "Can't join voice channel", `I can't join the voice channel : ${error}`);
          return message.channel.send(embed);
      }
  } else {
      serverQueue.songs.push(song);
      console.log(serverQueue.songs);
      if(playlist) return undefined;
      else return message.channel.send(`**${song.title} has been added to the queue!`);
  }
  return undefined;
}
async function play(guild, song){
  const serverQueue = queue.get(guild.id);
  if(!song){
      serverQueue.voiceChannel.leave();
      queue.delete(guild.id);
      return;
  }
  console.log(serverQueue.songs);
  const dispatcher = serverQueue.connection.playOpusStream(await ytdl(song.url))
      .on('end', reason => {
        if(reason == 'Stream is not generating quickly enough.') console.log('Song ended.')
        else console.log(reason)
        serverQueue.songs.shift()
        play(guild, serverQueue.songs[0])
      })
      .on('error', error => console.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}

//----------------------------------------------------------------------------------------------

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  client.user.setActivity('repl.it | vb help', {type: "WATCHING"});
});

//----------------------------------------------------------------------------------------------

client.on('guildMemberAdd', (member) => {
  if(member.user.bot){
    member.addRole(member.guild.roles.find(role => role.name === 'Bots').id).catch(console.error);
  } else {
    member.addRole(member.guild.roles.find(role => role.name === 'Guest').id).catch(console.error);
    var embed = getEmbed(information, `Welcome to ${member.guild.name} server!`, "Please follow the rules at #rules channel to make a nice environment for all of us :)\n\nRegards, \n\nDiscord Admin");
    member.send(embed)};
});

//----------------------------------------------------------------------------------------------

client.on('message', async message => {
  if(message.author.bot) return;
  if(message.content.indexOf(config.prefix) !== 0 && message.content.indexOf("<@541413292900352005>") !== 0) {
		const censor = "[Censored]";
		var edit = message.content;
		for (var i=0; i<= config.blacklist.length; i++) {
			if (message.content.toLowerCase().includes(config.blacklist[i])) {
				edit = edit.replace(new RegExp(config.blacklist[i], 'gi'), censor);
			}
		}
		if(edit !== message.content){
			message.delete();
			message.channel.send(`${message.author.username} : ${edit}`);
		}
	} else{
			const command = message.content.toLowerCase().split(" ", 2).slice(1).join("");
			const args = message.content.split(" ").slice(2).join(" ");
			const serverQueue = queue.get(message.guild.id);
			const searchString = args;
			const djrole = message.guild.roles.find(role => role.name === "DJ")
			var tempArgs = args.split(' ');
			const url = tempArgs[0] ? tempArgs[0].replace(/<(.+)>/g, '$1') : '';

			var notInVC = getEmbed(warning, "Not in voice channel", "You're not in a voice channel, join one to use command!")
			var noDJ = getEmbed(warning, "DJ role not found", `${message.guild.name} doesn't have \`DJ\` role. Create one to use this command.`)
			var notDJ = getEmbed(warning, "Insufficient permission", `You need ${djrole} role to use this command.`)

			if(!message.member.hasPermission("ADMINISTRATOR") && message.channel.id !== bot_channel[0] && message.channel.id !== bot_channel[1] && message.guild.name === "Victorious Return"){
					var embed = getEmbed(warning, "Channel disabled.", `Commands usage has been disabled in this channel.\n\nUse command in <#${bot_channel[0]}> or <#${bot_channel[1]}> instead.`)
					message.channel.send(embed)
			} else{
					try{
							if(command === "ping") {
									const m = await message.channel.send("Ping?");
									m.edit(`\`Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms\``);
							} else if(command === 'help'){
									var embed = getEmbed(information, 'Commands list', "**Prefix : v[space]**\n\n" +
									"**Help     : **Show this information.\n"+
									"**Guild    : **Giving clan information.\n"+
									"**Selfrole : **Get selfrole list.\n"+
									"**Getrole  : **Get a role from selfrole list.\n"+
									"**Play     : **Play or search music from YouTube.\n"+
									"**Skip     : **Skip currently playing music.\n"+
									"**Stop     : **Stop playing a music.\n"+
									"**Volume   : **Change music volume.\n"+
									"**NP       : **Show currently playing music.\n"+
									"**Queue    : **Show song queue.\n"+
									"**Pause    : **Pause music.\n"+
									"**Resume   : **Resume music.\n"+
									"**Warn     : **Warn a member.\n"+
									"**Kick     : **Kick a member.\n"+
									"**Ban      : **Ban a member.\n"+
									"**Prune    : **Delete up to 100 messages at once.");
									message.channel.send(embed);
							} else if(command === 'guild'){
									var embed = getEmbed(information, `${message.guild.name}`, "**Clan Media**\n"+
									"Discord  : <http://discord.gg/meu46Vt>\n"+
									"Guilded  : <http://guilded.gg/ViRe>\n"+
									"Facebook : <http://www.facebook.com/VictoriousReturn/?ref=br_rs>\n"+
									"YouTube  : <http://m.youtube.com/channel/UCz6h1Xcj3zZ2Oq8sSoirppQ>\n");
									message.channel.send(embed);
							} else if(command === 'warn'){
									const member = message.mentions.members.first()
									//for user without permission
									var missingPermissionEmbed = getEmbed(warning, 'Insufficient Permissions!', 'You need the `manage_messages` permission to use this command!');
									//wrong syntax
									var missingArgsEmbed = getEmbed(warning, 'Missing Arguments!', 'Usage : `warn [@User] [Reason]`');
									if(!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send(missingPermissionEmbed);

									let mentioned = message.mentions.users.first();
									if(!mentioned) return message.channel.send(missingArgsEmbed);

									let reason = args.split(" ").slice(1).join(' ')
									if(!reason) return message.channel.send(missingArgsEmbed);

									var warningEmbed = getDMEmbed(warning, `You've been warned in ${message.guild.name}`, `Warned by , ${message.author.tag}`, `Reason , ${reason}`);
									mentioned.send(warningEmbed).catch(error => message.channel.send(`Cannot send warning to ${member.user.tag}`));

									var warnSuccessfulEmbed = getDMEmbed(success, `${member.user.tag} has been warned.`, `Warned by , ${message.author.tag}`, `Reason , ${reason}`)
									message.channel.send(warnSuccessfulEmbed);
							} else if(command === 'kick'){
									const member = message.mentions.members.first()
									//for user without permission
									var missingPermissionEmbed = getEmbed(warning, 'Insufficient Permissions!', 'You need the `kick_members` permission to use this command!')
									//wrong syntax
									var missingArgsEmbed = getEmbed(warning, 'Missing Arguments!', 'Usage : `kick [@User] [Reason]`')
									var higherRoleEmbed = getEmbed(warning, `Can't Kick User!`, 'This user have higher role than me.')
									if(!message.member.hasPermission('KICK_MEMBERS')) return message.channel.send(missingPermissionEmbed);

									let mentioned = message.mentions.users.first();
									if(!mentioned) return message.channel.send(missingArgsEmbed);

									let reason = args.split(" ").slice(1).join(' ')
									if(!reason) return message.channel.send(missingArgsEmbed);
									if(!member.kickable) return message.channel.send(higherRoleEmbed);

									var kickEmbed = getDMEmbed(success, `You've been kicked from ${message.guild.name}`, `Kicked by , ${message.author.tag}`, `Reason , ${reason}`)
									mentioned.send(kickEmbed).catch(error => message.channel.send(`Cannot send kick reason to ${member.user.tag}`));

									var warnSuccessfulEmbed = getDMEmbed(success, `${member.user.tag} has been kicked.`, `Kicked by , ${message.author.tag}`, `Reason , ${reason}`)
									member.kick().then(() => message.channel.send(warnSuccessfulEmbed)).catch(error => message.reply(`Sorry, an error occured.`))
							} else if(command === 'ban'){
									const member = message.mentions.members.first()
									//for user without permission
									var missingPermissionEmbed = getEmbed(warning, 'Insufficient Permissions!', 'You need the `ban_members` permission to use this command!')
									//wrong syntax
									var missingArgsEmbed = getEmbed(warning, 'Missing Arguments!', 'Usage : `ban [@User] [Reason]`')
									var higherRoleEmbed = getEmbed(warning, `Can't Ban User!`, 'This user have higher role than me.')
									if(!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send(missingPermissionEmbed);

									let mentioned = message.mentions.users.first();
									if(!mentioned) return message.channel.send(missingArgsEmbed);

									let reason = args.split(" ").slice(1).join(' ')
									if(!reason) return message.channel.send(missingArgsEmbed);
									if(!member.kickable) return message.channel.send(higherRoleEmbed);

									var banEmbed = getDMEmbed(success, `You've been banned from ${message.guild.name}`, `Banned by , ${message.author.tag}`, `Reason , ${reason}`)
									mentioned.send(banEmbed).catch(error => message.channel.send(`Cannot send ban reason to ${member.user.tag}`));

									var banSuccessfulEmbed = getDMEmbed(success, `${member.user.tag} has been banned.`, `Banned by , ${message.author.tag}`, `Reason , ${reason}`)
									member.kick().then(() => message.channel.send(banSuccessfulEmbed)).catch(error => message.reply(`Sorry, an error occured.`))
							} else if(command === 'prune'){
									const amount = parseInt(args[0])+1
									//for user without permission
									var missingPermissionEmbed = getEmbed(warning, 'Insufficient Permissions!', 'You need the `manage_messages` permission to use this command!')
									//wrong syntax
									var notNumberEmbed = getEmbed(warning, 'Wrong Syntax!', 'That doesn\'t seem to be a valid number.\nUsage : `prune [amount]`')
									var beyondNumberEmbed = getEmbed(warning, 'Number Exceed Limit!', 'Input a number between 1 and 100.\nUsage : `prune [amount]`')
									if(!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send(missingPermissionEmbed);

									if(isNaN(amount)){
											return message.channel.send(notNumberEmbed)
									} else if(amount <= 1 || amount > 100){
											return message.reply(beyondNumberEmbed)
									}

									var errorEmbed = getEmbed(error, 'An error occured!', 'There was an error when trying to prune messages.')
									message.channel.bulkDelete(amount).catch(err => {
											console.console.error(err);
											message.channel.send(errorEmbed)
									});
							} else if(command === 'selfrole'){
									var roles = ""
									for (var i = 0; i < selfrole.length; i++) {
											roles = roles + selfrole[i] + "\n";
									}
									var embed = getEmbed(information, 'Selfrole list', roles + "\nUsage : `getrole [Role]`")
									message.channel.send(embed)
							} else if(command === 'getrole') {
									var warningEmbed = getEmbed(warning, 'Role not found', `Roles either not in the server or not added as selfrole. Use 'v selfrole' to get a list of available roles.`)

									var i
									for (i = 0; i <= selfrole.length;) {
										if(i === selfrole.length){
												break;
										} else if(args.toLowerCase() === selfrole[i].toLowerCase()){
												break;
										} else {
												i++;
										}
									}

									var role = message.guild.roles.find(role => role.name === selfrole[i]);
									if (!role) {
														message.channel.send(warningEmbed)
									} else {
											if (message.member.roles.has(role.id)) {
													var embed = getEmbed(success, 'Role Removed', `User already have a ${role} role.\n\nRemoving ${role} role from ${message.author.username}.`)
													message.member.removeRole(role.id).catch(console.error)
													message.channel.send(embed)
											} else {
													var embed = getEmbed(success, 'Role added!', `Add ${message.author.username} a ${role} role.`)
													message.member.addRole(role.id).catch(console.error)
													message.channel.send(embed)
											}
									}
							} else if(command === 'play'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											const voiceChannel = message.member.voiceChannel;
											var cantConnect = getEmbed(warning, "Can't connect to channel", `I can't connect to that channel.`)
											var cantSpeak = getEmbed(warning, "Can't speak in channel", `I can't speak in that channel.`)
											
											if(!voiceChannel) return message.channel.send(notInVC);
											const permissions = voiceChannel.permissionsFor(message.client.user);
											if(!permissions.has('CONNECT')){
													return message.channel.send(cantConnect)
											}
											if(!permissions.has('SPEAK')){
													return message.channel.send(cantSpeak)
											}
											if(url.match(/^https?:\/\/(www.youtube.com|youtube.com|m.youtube.com)\/playlist(.*)$/)){
													const playlist = await youtube.getPlaylist(url)
													const videos = await playlist.getVideos()
													for(const video of Object.values(videos)){
															const video2 = await youtube.getVideoByID(video.id)
															await handleVideo(video2, message, voiceChannel, true)
													}
													return message.channel.send(`Playlist : **${playlist.title}** has been added to the queue!`)
											} else {
													try {
															var video = await youtube.getVideo(url)
													} catch (error){
															try{
																	var videos = await youtube.searchVideos(searchString, 10)
																	let index = 0
																	message.channel.send(`
\`\`\`Song selection :

${videos.map(video2 => `${++index}. ${video2.title}`).join('\n')}

Please provide a value to select one of the search results range from 1-10.\`\`\`
																	`);
																	try{
																			var response = await message.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
																				maxMatches: 1,
																				time: 30000,
																				errors: ['time']
																			});
																	} catch (err){
																			console.error(err)
																			var errorEmbed = getEmbed(success, "Can't proceed", "There's either no response or the value is invalid, cancelling video selection.")
																			return message.channel.send(errorEmbed)
																	}
																	const videoIndex = parseInt(response.first().content)
																	var video = await youtube.getVideoByID(videos[videoIndex - 1].id)
															} catch (err){
																	console.error(err)
																	var errorEmbed = getEmbed(success, "No videos found!", "I can't obtain any search results.")
																	message.channel.send(errorEmbed)
															}
													}
												return handleVideo(video, message, voiceChannel);
											}
									}
							} else if(command === 'skip'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(!message.member.voiceChannel) return message.channel.send(notInVC)
											if(!serverQueue) return message.channel.send("There's nothing playing that I could skip for you.")
											serverQueue.connection.dispatcher.end('Skip command has been used!')
											return undefined;
									}
							} else if(command === 'stop'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(!message.member.voiceChannel) return message.channel.send(notInVC)
											if(!serverQueue) return message.channel.send("There's nothing playing that I could stop for you.")
											serverQueue.songs = []
											serverQueue.connection.dispatcher.end('Stop command has been used!')
											return undefined;
									}
							} else if(command === 'volume'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(!message.member.voiceChannel) return message.channel.send(notInVC)
											if(!serverQueue) return message.channel.send("There's nothing playing right now.")
											if(!tempArgs[0]) return message.channel.send(`The current volume is: **${serverQueue.volume}**`);
											serverQueue.volume = tempArgs[0]
											serverQueue.connection.dispatcher.setVolumeLogarithmic(tempArgs[0] / 5)
											return message.channel.send(`Volume has been set to: **${tempArgs[0]}**`)
									}
							} else if(command === 'np'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(!serverQueue) return message.channel.send("There's nothing playing right now.")
											return message.channel.send(`Now playing: **${serverQueue.songs[0].title}**`)
									}
							} else if(command === 'queue'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(!serverQueue) return message.channel.send("There's nothing playing right now.")
											return message.channel.send(`
\`\`\`Song queue:

${serverQueue.songs.map(song => `- ${song.title}`).join('\n')}

Now playing: ${serverQueue.songs[0].title}\`\`\`
											`);
									}
							} else if(command === 'pause'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(serverQueue && serverQueue.playing){
													serverQueue.playing = false;
													serverQueue.connection.dispatcher.pause();
													return message.channel.send('Music paused!')
											}
										return message.channel.send("There's nothing playing right now.")
									}
							} else if(command === 'resume'){
									if(!djrole){
											message.channel.send(noDJ)
									} else if(!message.member.roles.has(djrole.id)){
											message.channel.send(notDJ)
									} else {
											if(serverQueue && !serverQueue.playing){
													serverQueue.playing = true;
													serverQueue.connection.dispatcher.resume();
													return message.channel.send('Music resumed!')
											}
										return message.channel.send("There's nothing playing right now.")
									}
							} else {
									var embed = getEmbed(warning, 'Command not found!',`There's no ` + command + ` command. Use 'v help' to get list of command.`)
									message.channel.send(embed)
							}
					} catch (error){
						console.error(error)
						message.reply('there was an error while trying to execute that command.')
					}
			}
	}
});

//----------------------------------------------------------------------------------------------

client.login(process.env.BOT_TOKEN)
node v10.16.0