-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
195 lines (152 loc) · 4.93 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
const {
Client,
GatewayIntentBits,
InteractionType,
Collection,
EmbedBuilder,
ActivityType,
escapeMarkdown,
Partials,
AttachmentBuilder
} = require("discord.js");
const {
NoSubscriberBehavior,
StreamType,
createAudioPlayer,
createAudioResource,
createReadStream,
entersState,
AudioPlayerStatus,
VoiceConnectionStatus,
joinVoiceChannel
} = require('@discordjs/voice');
const config = require("./config.json");
const PlayDL = require("play-dl");
const queue = new Map();
const client = new Client({
intents: [
"Guilds",
"GuildMessages",
"GuildVoiceStates",
"GuildMessageTyping",
"GuildIntegrations",
"MessageContent",
"DirectMessageTyping",
"DirectMessages",
GatewayIntentBits.Guilds
]});
module.exports = client;
client.slashCommands = new Collection();
client.commands = new Collection();
client.on("interactionCreate", async (interaction) => {
if (!interaction.guild) return;
if (interaction.isCommand()){
const cmd = client.slashCommands.get(interaction.commandName);
if(!cmd)return;
cmd.run(client, interaction, handleVideo, queue, disconnectToChannel);
}
if (interaction.isContextMenuCommand()) {
await interaction.deferReply({ ephemeral: false });
const command = client.slashCommands.get(interaction.commandName);
if (command) command.run(client, interaction,handleVideo, queue, disconnectToChannel);
}
});
client.on('ready', () => {
console.log("Estou pronto Alunos, Vamos começar!");
});
require('./handler')(client);
async function connectToChannel(channel) {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
try {
await entersState(connection, VoiceConnectionStatus.Ready);
return connection;
} catch (error) {
connection.destroy();
throw error;
}
}
async function disconnectToChannel(channel) {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
connection.destroy();
}
async function handleVideo(video, msg, voiceChannel, playlist = false) {
const serverQueue = queue.get(msg.guild.id);
console.log(video) // ID de informações do video
const song = {
id: video.id,
title: escapeMarkdown(video.title),
url: `https://www.youtube.com/watch?v=${video.id}`
/*
Se vc quiser personalizar o bot completamente do seu jeito, adicione mais informações a este objeto
*/
};
if (!serverQueue) {
const queueConstruct = {
player: createAudioPlayer(),
textChannel: msg.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true,
loop: false,
stopLoop: false // Uma solução para interromper o loop no final da musica
};
queue.set(msg.guild.id, queueConstruct);
queueConstruct.songs.push(song);
try {
const connection = await connectToChannel(voiceChannel);
connection.subscribe(queueConstruct.player);
play(msg.guild, queueConstruct.songs[0]);
} catch (error) {
console.error(`Eu não consegui entrar no canal de voz: ${error}`);
queue.delete(msg.guild.id);
return msg.channel.send(`Eu não consegui entrar no canal de voz: ${error}`);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
if (playlist) return undefined;
else return msg.channel.send(`Agora **${song.title}** foi adicionado a lista!`);
}
return undefined;
}
async function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
disconnectToChannel(serverQueue.voiceChannel);
queue.delete(guild.id);
return;
}
serverQueue.textChannel.send(`Tocando para os Alunos: **${song.title}**`);
const sond = await PlayDL.stream(song.url,{
quality: 2, // quality: [ 0 = Baixo, 1 = Medio, 2 = Alto ]
});
const resource = createAudioResource(sond.stream, {
inputType: sond.type,
inlineVolume: true // Deixe verdadeiro se quiser alterar o volume da música
});
serverQueue.player.play(resource);
entersState(serverQueue.player, AudioPlayerStatus.Playing);
serverQueue.player.on(AudioPlayerStatus.Idle,async () => {
if(serverQueue.stopLoop){
clearTimeout(serverQueue.stopLoop);
serverQueue.stopLoop = setTimeout(() => serverQueue.stopLoop = false, 5000);
return;
}
serverQueue.stopLoop = setTimeout(() => serverQueue.stopLoop = false, 5000);
if(!serverQueue.loop) serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
});
resource.volume.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.connection = resource;
}
client.login(config.token);