forked from xderxit/MyItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (159 loc) · 9.18 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
const Discord = require('discord.js');
const ytdl = require("ytdl-core");
const client = new Discord.Client();
var {prefix, token} = require("./config.json");
var queue = new Map();
client.once('ready', () => {
console.log('MyItems Bot online!\nBot made with love by x_derx_it!');
})
client.on('message', async message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
let serverQueue = queue.get(message.guild.id);
const args = message.content.slice(prefix.length).split(/ +/g);
const command = args.shift().toLowerCase();
switch (command) {
case 'ping':
message.channel.send('Calcolando il ping...').then((resultMessage) => {
const ping = resultMessage.createdTimestamp - message.createdTimestamp;
const messageEmbed = new Discord.MessageEmbed()
.setColor('#30e3ff')
.addField('Latenza Bot', `${ping}ms`)
.addField('Latenza API', `${client.ws.ping}ms`)
.setThumbnail('https://cdn4.telesco.pe/file/TNcTIdRrxhTQCEGqYOWDWKHW8XUlDlcLPrHy6MP2d3BK-ogLAs3JYVURkV-uV_4gZqsmMNoxe66LNF-fJaqBcSOWeCyTa9F5eYF4vLODM9cTfe9E6awOb7qbBj_k8IjK2tdcos6_IHuH8bDUdGfc-lGlW-btVAcPBYgAly1k1ujxMRsPO16nKwoWeq-aQFQSEv2YsjAvZvjfyEG_a3oSGwgaOfwTNP0toVrf7bU9DaXFE-ER0eJs88mP09ZqRm3MCYyv5cZflefjnQe2Ig9LG25pxIEADvWVX3aByeYMuprMi41LyQG6eIoIxhH_GIIzU-vxcblKTgpZ-MZmDzekgw.jpg')
.setFooter('Bot creato con amore da x_derx_it')
.setTimestamp()
message.channel.send(messageEmbed);
});
break;
case 'play':
if(!args[0]) return;
let url = args.join(" ")
if(!url.match(/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/)) return message.channel.send('Questo non è un link di **Youtube!**');
let vc = message.member.voice;
if(!vc) return message.channel.send('Non sei in un canale vocale!')
if(!vc.channel.permissionsFor(client.user).has('CONNECT') || !vc.channel.permissionsFor(client.user).has('CONNECT')) return message.channel.send('Non ho il permesso!');
let songinfo = await ytdl.getInfo(url);
let song = {
title: songinfo.title,
url: songinfo.video_url
}
if(!serverQueue) {
let queueConst = {
textChannel: message.channel,
voiceChannel: vc.channel,
connection: null,
songs: [],
volume: 5,
playing: true
};
queue.set(message.guild.id, queueConst);
queueConst.songs.push(song);
try {
let connection = await vc.channel.join();
queueConst.connection = connection
playSong(message.guild, queueConst.songs[0])
} catch (error) {
console.log(error)
queue.delete(message.guild.id);
var errore = new Discord.MessageEmbed()
.setColor('#30e3ff')
.addField('Ho avuto un errore!', 'Durante la connessione ad un canale vocale')
.addField('Errore', error)
.setThumbnail('https://cdn4.telesco.pe/file/TNcTIdRrxhTQCEGqYOWDWKHW8XUlDlcLPrHy6MP2d3BK-ogLAs3JYVURkV-uV_4gZqsmMNoxe66LNF-fJaqBcSOWeCyTa9F5eYF4vLODM9cTfe9E6awOb7qbBj_k8IjK2tdcos6_IHuH8bDUdGfc-lGlW-btVAcPBYgAly1k1ujxMRsPO16nKwoWeq-aQFQSEv2YsjAvZvjfyEG_a3oSGwgaOfwTNP0toVrf7bU9DaXFE-ER0eJs88mP09ZqRm3MCYyv5cZflefjnQe2Ig9LG25pxIEADvWVX3aByeYMuprMi41LyQG6eIoIxhH_GIIzU-vxcblKTgpZ-MZmDzekgw.jpg')
.setFooter('Bot creato con amore da x_derx_it')
.setTimestamp()
return message.channel.send(errore);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(`${song.title} è stata aggiunta alla coda!`);
}
break;
case 'stop':
if(!message.member.voice.channel) return message.channel.send("Devi essere in un canale vocale!");
if(!serverQueue) return message.channel.send("Non c'e' il bot nel canale vocale!");
serverQueue.songs = [0];
serverQueue.connection.dispatcher.end();
message.channel.send("Ho fermato la musica!");
break;
case 'skip':
if(!message.member.voice.channel) return message.channel.send("Devi essere in un canale vocale!");
if(!serverQueue) return message.channel.send("Non c'e' nessuna canzone in coda!");
queueConst.connection.dispatcher.end();
var canzone = new Discord.MessageEmbed()
.setColor('#30e3ff')
.addField('Ho skippato la canzone!')
.addField('Canzone skippata:', song.title)
.setThumbnail('https://cdn4.telesco.pe/file/TNcTIdRrxhTQCEGqYOWDWKHW8XUlDlcLPrHy6MP2d3BK-ogLAs3JYVURkV-uV_4gZqsmMNoxe66LNF-fJaqBcSOWeCyTa9F5eYF4vLODM9cTfe9E6awOb7qbBj_k8IjK2tdcos6_IHuH8bDUdGfc-lGlW-btVAcPBYgAly1k1ujxMRsPO16nKwoWeq-aQFQSEv2YsjAvZvjfyEG_a3oSGwgaOfwTNP0toVrf7bU9DaXFE-ER0eJs88mP09ZqRm3MCYyv5cZflefjnQe2Ig9LG25pxIEADvWVX3aByeYMuprMi41LyQG6eIoIxhH_GIIzU-vxcblKTgpZ-MZmDzekgw.jpg')
.setFooter('Bot creato con amore da x_derx_it')
.setTimestamp()
message.channel.send(canzone);
break;
case 'volume':
if(!message.member.voice.channel) return message.channel.send("Devi essere in un canale vocale!");
if(!serverQueue) return message.channel.send("Non c'e' il bot nel canale vocale!");
if(!args[0]) return message.channel.send(`Volume attuale: **${serverQueue.volume}**`);
if(isNaN(args[0])) return message.channel.send("Quello non è un numero valido!");
serverQueue.volume = args[1];
serverQueue.connection.dispatcher.setVolumeLogarithmic(args[0] / 5);
message.channel.send(`Ho cambiato il volume a: **${args[0]}**`);
break;
case 'np' || 'nowplaying':
if(!serverQueue) return message.channel.send("Non c'e' niente in ascolto!");
var nowPlaying = new Discord.MessageEmbed()
.setColor('#30e3ff')
.addField('Canzone attuale', song.title)
.setThumbnail('https://cdn4.telesco.pe/file/TNcTIdRrxhTQCEGqYOWDWKHW8XUlDlcLPrHy6MP2d3BK-ogLAs3JYVURkV-uV_4gZqsmMNoxe66LNF-fJaqBcSOWeCyTa9F5eYF4vLODM9cTfe9E6awOb7qbBj_k8IjK2tdcos6_IHuH8bDUdGfc-lGlW-btVAcPBYgAly1k1ujxMRsPO16nKwoWeq-aQFQSEv2YsjAvZvjfyEG_a3oSGwgaOfwTNP0toVrf7bU9DaXFE-ER0eJs88mP09ZqRm3MCYyv5cZflefjnQe2Ig9LG25pxIEADvWVX3aByeYMuprMi41LyQG6eIoIxhH_GIIzU-vxcblKTgpZ-MZmDzekgw.jpg')
.setFooter('Bot creato con amore da x_derx_it')
.setTimestamp()
message.channel.send(nowPlaying);
break;
case 'queue':
if(!serverQueue) return message.channel.send("Non c'e' niente in coda!");
message.channel.send(`
__**Canzoni in coda:**__
${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
**In ascolto adesso:** ${serverQueue.songs[0].title}
`, { split: true });
break;
case 'pause':
if(!message.member.voice.channel) return message.channel.send("Devi essere in un canale vocale per metteree in pausa!");
if(!serverQueue) return message.channel.send("Non c'e' niente in ascolto!");
if(!serverQueue.playing) return message.channel.send("La canzone è già in pausa!");
serverQueue.playing = false;
serverQueue.connection.dispatcher.pause();
message.channel.send("Ho messo in pausa la musica!");
break;
case 'resume':
if(!message.member.voice.channel) return message.channel.send("Devi essere in un canale vocale per far ripartire la musica!");
if(!serverQueue) return message.channel.send("Non c'e' niente in ascolto");
if(serverQueue.playing) return message.channel.send("La musica è già in ascolto!");
serverQueue.playing = true;
serverQueue.connection.dispatcher.resume();
message.channel.send("Ho fatto ripartire la musica!");
break;
}
});
/**
* |
* @param {Discord.Guild} guild |
* @param {Object} song
*
*/
async function playSong(guild, song) {
let serverQueue = queue.get(guild.id);
if(!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection.play(ytdl(song.url)).on('end', () => {
serverQueue.songs.shift();
playSong(guild, serverQueue.songs[0])
})
.on('error', () => {
console.log(error)
})
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}
client.login(token);