This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
151 lines (129 loc) · 6.33 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
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
const { CommandoClient } = require('discord.js-commando');
const { Structures } = require('discord.js');
const { MessageEmbed } = require('discord.js');
const winston = require('winston');
const ytdl = require('ytdl-core');
const path = require('path');
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'logs.log' }),
],
format: winston.format.combine(
winston.format.printf(log => `${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()} - [${log.level.toUpperCase()}] - ${log.message}`),
)
});
Structures.extend('Guild', Guild => {
class MusicGuild extends Guild {
constructor(client, data) {
super(client, data);
this.music = {
queue: [],
isPlaying: false,
nowPlaying: null,
volume: 1,
dispatcher: null,
paused: false,
seek: null,
sfx: {
earrape: false
}
};
}
play = async (queue, message) => {
try {
if (!message.guild.music.seek && queue.length === 0) throw new Error('Unknwon');
const voiceChannel = !message.guild.music.seek ? queue[0].voiceChannel : message.guild.music.nowPlaying.voiceChannel;
const connection = await voiceChannel.join();
message.guild.music.paused = false;
let dispatcher;
if (message.guild.music.seek) {
switch (message.guild.music.nowPlaying.type) {
case 'youtube': { dispatcher = connection.play(ytdl(message.guild.music.nowPlaying.link, { quality: 'highestaudio' }), { seek: message.guild.music.seek }); break; }
case 'facebook': { dispatcher = connection.play(message.guild.music.nowPlaying.link, { seek: message.guild.music.seek }); break; }
case 'other': { dispatcher = connection.play(message.guild.music.nowPlaying.link, { seek: message.guild.music.seek }); break; }
}
} else {
switch (queue[0].type) {
case 'youtube': { dispatcher = connection.play(ytdl(queue[0].link, { quality: 'highestaudio' })); break; }
case 'facebook': { dispatcher = connection.play(queue[0].link); break; }
case 'other': { dispatcher = connection.play(queue[0].link); break; }
}
}
dispatcher.on('start', async () => {
message.guild.music.nowPlaying = !message.guild.music.seek ? queue[0] : message.guild.music.nowPlaying;
message.guild.music.dispatcher = dispatcher;
dispatcher.setVolume(message.guild.music.volume);
if (!message.guild.music.seek) {
const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: Play`).addField('Now playing', queue[0].title);
if (queue[0].type === 'youtube') embed.setThumbnail(queue[0].thumbnail);
if (queue[0].type !== 'other') embed.addField('By', queue[0].by);
embed.addField('Duration', queue[0].duration);
await message.say({ embed });
return queue.shift();
}
});
dispatcher.on('finish', async () => {
if (queue.length >= 1) {
message.guild.music.seek = null;
return this.play(queue, message);
}
message.guild.music.isPlaying = false;
message.guild.music.nowPlaying = null;
message.guild.music.dispatcher = null;
message.guild.music.seek = null;
voiceChannel.leave();
const embed = new MessageEmbed().setColor('#000099').setTitle(':musical_note: Queue ended');
return await message.say({ embed });
});
dispatcher.on('error', err => {
message.guild.music.queue = [];
message.guild.music.isPlaying = false;
message.guild.music.nowPlaying = false;
message.guild.music.dispatcher = null;
message.guild.music.seek = null;
voiceChannel.leave();
throw err;
});
} catch(err) {
logger.log('error', err);
const embed = new MessageEmbed().setColor('#ff0000').setTitle(`:x: Error occured: ${err.message}`);
return message.say({ embed });
}
}
formatDurationString = (hours, minutes, seconds) => {
return `${hours < 10 ? '0' + hours : hours ? hours : '00'}:${minutes < 10 ? '0' + minutes : minutes ? minutes : '00'}:${seconds < 10 ? '0' + seconds : seconds ? seconds : '00'}`;
}
formatDurationSeconds = durationString => {
if (!durationString.match(/(\d+:)?(\d{2}:)?\d{2}/)) return;
const time = durationString.split(':').map(time => parseInt(time));
switch (time.length) {
case 3: return time[0] * 3600 + time[1] * 60 + time[2];
case 2: return time[0] * 60 + time[1];
case 1: return time[0];
}
}
}
return MusicGuild;
});
const client = new CommandoClient({
commandPrefix: process.env.PREFIX,
owner: process.env.OWNER_ID
});
client.registry
.registerDefaultTypes()
.registerGroups([
['music', 'Music Commands']
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, 'commands'));
client.once('ready', () => {
client.user.setActivity(process.env.STATUS || 'amsa7 lak7el', { type: process.env.STATUS_TYPE });
});
client.on('debug', msg => logger.log('debug', msg));
client.on('warn', msg => logger.log('warn', msg));
client.on('error', msg => logger.log('error', msg));
process.on('unhandledRejection', err => logger.log('error', err));
client.login(process.env.BOT_TOKEN);