-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
177 lines (159 loc) · 6.76 KB
/
search.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
const { Util, MessageEmbed } = require("discord.js");
const ytdl = require("ytdl-core");
const yts = require("yt-search");
const ytdlDiscord = require("ytdl-core-discord");
const YouTube = require("youtube-sr");
const sendError = require("../util/error")
const fs = require('fs');
module.exports = {
info: {
name: "search",
description: "Para prucurar uma música",
usage: "<song_name>",
aliases: ["sc", "procurar"],
},
run: async function (client, message, args) {
let channel = message.member.voice.channel;
if (!channel)return sendError("Você precisa está em um canal de voz para executar este comando", message.channel);
const permissions = channel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT"))return sendError("I cannot connect to your voice channel, make sure I have the proper permissions!", message.channel);
if (!permissions.has("SPEAK"))return sendError("I cannot speak in this voice channel, make sure I have the proper permissions!", message.channel);
var searchString = args.join(" ");
if (!searchString)return sendError("Você não me forneceu nada para procurar", message.channel);
var serverQueue = message.client.queue.get(message.guild.id);
try {
var searched = await YouTube.search(searchString, { limit: 10 });
if (searched[0] == undefined)return sendError("Música não encontrada")
let index = 0;
let embedPlay = new MessageEmbed()
.setColor("PURPLE")
.setAuthor(`Resultados para \"${args.join(" ")}\"`, message.author.displayAvatarURL())
.setDescription(`${searched.map(video2 => `**\`${++index}\` |** [\`${video2.title}\`](${video2.url}) - \`${video2.durationFormatted}\``).join("\n")}`)
.setFooter("Digite o número do resultado para adicionar a lista");
// eslint-disable-next-line max-depth
message.channel.send(embedPlay).then(m => m.delete({
timeout: 15000
}))
try {
var response = await message.channel.awaitMessages(message2 => message2.content > 0 && message2.content < 11, {
max: 1,
time: 20000,
errors: ["time"]
});
} catch (err) {
console.error(err);
return message.channel.send({
embed: {
color: "RED",
description: "O tempo de seleção terminou"
}
});
}
const videoIndex = parseInt(response.first().content);
var video = await (searched[videoIndex - 1])
} catch (err) {
console.error(err);
return message.channel.send({
embed: {
color: "RED",
description: "Eu não consegui obter resultados para a busca"
}
});
}
response.delete();
var songInfo = video
const song = {
id: songInfo.id,
title: Util.escapeMarkdown(songInfo.title),
views: String(songInfo.views).padStart(10, ' '),
ago: songInfo.uploadedAt,
duration: songInfo.durationFormatted,
url: `https://www.youtube.com/watch?v=${songInfo.id}`,
img: songInfo.thumbnail.url,
req: message.author
};
if (serverQueue) {
serverQueue.songs.push(song);
let thing = new MessageEmbed()
.setAuthor("A música foi adicionada a lista")
.setThumbnail(song.img)
.setColor("PURPLE")
.addField("Nome", song.title, true)
.addField("Duração", song.duration, true)
.addField("Adicionado por", song.req.tag, true)
.setFooter(`${song.views} Visualizações | ${song.ago}`)
return message.channel.send(thing);
}
const queueConstruct = {
textChannel: message.channel,
voiceChannel: channel,
connection: null,
songs: [],
volume: 80,
playing: true,
loop: false
};
message.client.queue.set(message.guild.id, queueConstruct);
queueConstruct.songs.push(song);
const play = async (song) => {
const queue = message.client.queue.get(message.guild.id);
let afk = JSON.parse(fs.readFileSync("./afk.json", "utf8"));
if (!afk[message.guild.id]) afk[message.guild.id] = {
afk: false,
};
var online = afk[message.guild.id]
if (!song){
if (!online.afk) {
sendError("Como a lista de músicas está vazia e não há uma música tocando, eu sai do canal de voz", message.channel)
message.guild.me.voice.channel.leave();//If you want your bot stay in vc 24/7 remove this line :D
message.client.queue.delete(message.guild.id);
}
return message.client.queue.delete(message.guild.id);
}
let stream = null;
if (song.url.includes("youtube.com")) {
stream = await ytdl(song.url);
stream.on('error', function(er) {
if (er) {
if (queue) {
queue.songs.shift();
play(queue.songs[0]);
return sendError(`An unexpected error has occurred.\nPossible type \`${er}\``, message.channel)
}
}
});
}
queue.connection.on("disconnect", () => message.client.queue.delete(message.guild.id));
const dispatcher = queue.connection
.play(ytdl(song.url, {quality: 'highestaudio', highWaterMark: 1 << 25 ,type: "opus"}))
.on("finish", () => {
const shiffed = queue.songs.shift();
if (queue.loop === true) {
queue.songs.push(shiffed);
};
play(queue.songs[0]);
})
dispatcher.setVolumeLogarithmic(queue.volume / 100);
let thing = new MessageEmbed()
.setAuthor("Tocando agora")
.setThumbnail(song.img)
.setColor("PURPLE")
.addField("Nome", song.title, true)
.addField("Duração", song.duration, true)
.addField("Adicionado por", song.req.tag, true)
.setFooter(`${song.views} Visualizações | ${song.ago}`)
queue.textChannel.send(thing);
};
try {
const connection = await channel.join();
queueConstruct.connection = connection;
channel.guild.voice.setSelfDeaf(true)
play(queueConstruct.songs[0]);
} catch (error) {
console.error(`I could not join the voice channel: ${error}`);
message.client.queue.delete(message.guild.id);
await channel.leave();
return sendError(`Ocorreu um arro ao acessar o canal: ${error}`, message.channel);
}
},
};