-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.js
177 lines (159 loc) · 7.1 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: "To search songs :D",
usage: "<song_name>",
aliases: ["sc"],
},
run: async function (client, message, args) {
let channel = message.member.voice.channel;
if (!channel)return sendError("I'm sorry but you need to be in a voice channel to play music!", 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("You didn't poivide want i want to search", 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("Looks like i was unable to find the song on YouTube", message.channel);
let index = 0;
let embedPlay = new MessageEmbed()
.setColor("BLUE")
.setAuthor(`Results for \"${args.join(" ")}\"`, message.author.displayAvatarURL())
.setDescription(`${searched.map(video2 => `**\`${++index}\` |** [\`${video2.title}\`](${video2.url}) - \`${video2.durationFormatted}\``).join("\n")}`)
.setFooter("Type the number of the song to add it to the playlist");
// 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: "Nothing has been selected within 20 seconds, the request has been canceled."
}
});
}
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: "🆘 **|** I could not obtain any search results"
}
});
}
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("Song has been added to queue", "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setThumbnail(song.img)
.setColor("YELLOW")
.addField("Name", song.title, true)
.addField("Duration", song.duration, true)
.addField("Requested by", song.req.tag, true)
.setFooter(`Views: ${song.views} | ${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("Leaving the voice channel because I think there are no songs in the queue. If you like the bot stay 24/7 in voice channel run `!afk`\n\nThank you for using my code! [GitHub](https://github.com/SudhanPlayz/Discord-MusicBot)", 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("Started Playing Music!", "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setThumbnail(song.img)
.setColor("BLUE")
.addField("Name", song.title, true)
.addField("Duration", song.duration, true)
.addField("Requested by", song.req.tag, true)
.setFooter(`Views: ${song.views} | ${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(`I could not join the voice channel: ${error}`, message.channel);
}
},
};