-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
322 lines (312 loc) · 12.8 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { Client, Intents, MessageAttachment } = require("discord.js")
const { createAudioPlayer } = require("@discordjs/voice")
const playMusic = require("./utils/playMusic")
const getMusic = require("./src/getMusic")
const createEmbed = require("./utils/createEmbed")
const isYoutubeLink = require("./utils/isYoutubeLink")
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_MESSAGES,
]
})
const defaultPrefix = "~"
require("dotenv").config()
const TOKEN = process.env.DISCORD_TOKEN
const MUSIC_TOKEN = process.env.YOUTUBE_KEY
let isPlaying = false
let isPaused = false
const player = createAudioPlayer()
const queue = new Map()
function isMsgForBot(msg) {
if (!msg.content.startsWith(defaultPrefix) || msg.author.bot) return false
let msgArray = msg.content.split(" ")
if (msgArray[0] === "~") return false
return true
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`)
})
client.on("messageCreate", async (msg) => {
if (isMsgForBot(msg)) {
const args = msg.content.slice(defaultPrefix.length).trim().split(' ')
const command = args.shift().toLowerCase()
const userVoiceChannel = msg.member.voice.channel
const userGuild = msg.guild
let textChannel = msg.channel
if (command === "ping" && args.length === 0)
await msg.reply(`🏓 Latency: ${Date.now() - msg.createdTimestamp}ms`)
else if (command === "help" && args.length === 0) {
let iconLink = new MessageAttachment("./images/botIcon.png")
let embed = createEmbed({
title: "Help command!!",
pingCommand: "ping",
playCommand: "play",
pauseCommand: "pause",
resumeCommand: "resume",
skipCommand: "skip",
nowPlayingCommand: "np",
queueCommand: "q",
removeCommand: "remove",
leaveCommand: "dc",
helpCommand: "help",
footer: `Requested By: ${msg.author.username}`
})
embed.setThumbnail("attachment://botIcon.png")
await textChannel.send({ embeds: [embed], files: [iconLink] })
}
else if (command === "play") {
let serverQueue = queue.get(userGuild.id)
if (!userVoiceChannel) {
await textChannel.send("You need to be in a Voice Channel, to execute this command")
}
if (args.length === 0) {
await textChannel.send("Please pass the arguments")
return
}
if (!userVoiceChannel.joinable) {
await textChannel.send("Not have permission to join your Voice Channel")
return
}
let nameByUser = args.join(" ")
let songData = {}
try {
let re = /https:\/\//
let isLink = nameByUser.match(re)
if (isLink) {
if (!isYoutubeLink(nameByUser)) {
await textChannel.send("Sorry, currently we are not playing media from external links. :(")
return
}
let songId = isYoutubeLink(nameByUser)
songData = {
id: songId,
title: "Youtube Video",
authorChannel: "External Youtube Link",
source: "YouTube",
thumbnailHigh: "https://cdn.discordapp.com/attachments/933824091482374166/991319195604222102/unknown.png",
requestedBy: msg.author
}
}
else {
songData = await getMusic(nameByUser, MUSIC_TOKEN, msg.author)
}
if (!songData) {
await textChannel.send("Sorry, could not find the media you've been looking")
return
}
if (!serverQueue) {
let thisServerQueue = {
textChannel: textChannel,
voiceChannel: userVoiceChannel,
guild: userGuild,
adapter: userGuild.voiceAdapterCreator,
connection: null,
songs: []
}
queue.set(userGuild.id, thisServerQueue)
thisServerQueue.songs.push(songData)
try {
await playMusic(player, thisServerQueue)
let embed = createEmbed({
title: `Now Playing`,
description: thisServerQueue.songs[0].title,
thumbnail: thisServerQueue.songs[0].thumbnailHigh,
extra: thisServerQueue.songs[0].authorChannel,
footer: thisServerQueue.songs[0].source,
songAddedBy: thisServerQueue.songs[0].requestedBy.toString()
})
await textChannel.send({ embeds: [embed] })
isPlaying = true
}
catch (e) {
console.log("error in recieving data")
console.log(e)
}
}
else {
serverQueue.songs.push(songData)
let embed = createEmbed({
title: "Adding To Queue",
description: songData.title,
extra: songData.authorChannel,
footer: songData.source,
songAddedBy: songData.requestedBy.toString()
})
await textChannel.send({ embeds: [embed] })
}
}
catch (e) {
console.log("error while fetching song data")
console.log(e)
}
}
else if (command === "np" || command === "nowPlaying" && args.length === 0) {
let serverQueue = queue.get(userGuild.id)
if (!serverQueue) {
let embed = createEmbed({ title: "No Music Playing" })
await textChannel.send({ embeds: [embed] })
return
}
let embed = createEmbed({
title: "Now Playing",
description: serverQueue.songs[0].title,
thumbnail: serverQueue.songs[0].thumbnailHigh,
extra: serverQueue.songs[0].authorChannel,
footer: serverQueue.songs[0].source,
songAddedBy: serverQueue.songs[0].requestedBy.toString()
})
await textChannel.send({ embeds: [embed] })
}
else if (command === "pause" && args.length === 0) {
if (!isPlaying) {
let embed = createEmbed({ title: "No Music Playing" })
await textChannel.send({ embeds: [embed] })
return
}
player.pause(true)
isPlaying = false
isPaused = true
let embed = createEmbed({ title: "Paused!!" })
await textChannel.send({ embeds: [embed] })
}
else if (command === "resume" && args.length === 0) {
if (!isPaused) {
let embed = createEmbed({ title: "No Music Paused" })
await textChannel.send({ embeds: [embed] })
return
}
player.unpause()
isPlaying = true
isPaused = false
let embed = createEmbed({ title: "Resumed!!" })
await textChannel.send({ embeds: [embed] })
}
else if (command === "skip" && args.length === 0) {
let serverQueue = queue.get(userGuild.id)
if (!serverQueue) {
let embed = createEmbed({ title: "No Music Playing" })
await textChannel.send({ embeds: [embed] })
return
}
player.stop()
}
else if (command === "q" || command === "queue" && args.length === 0) {
let serverQueue = queue.get(userGuild.id)
if (!serverQueue) {
let embed = createEmbed({ title: "There is nothing playing in server!!" })
await textChannel.send({ embeds: [embed] })
return
}
let q = serverQueue.songs.slice(1)
let queueStr = ""
let i = 2
for (let song of q) {
queueStr = queueStr + `**${i}).** ` + `${song.title} **by** ${song.authorChannel} [ ${song.requestedBy.toString()}]\n`
i++
}
let nowPlayingSong = `${serverQueue.songs[0].title} **by** ${serverQueue.songs[0].authorChannel} [ ${serverQueue.songs[0].requestedBy.toString()}]`
let queueIconLink = new MessageAttachment("./images/queueIcon.png")
let embed = createEmbed({
title: "Queue",
description: "Here are the songs you've added",
comingUpSongs: queueStr,
nowPlayingField: nowPlayingSong,
footer: `Requested By: ${msg.author.username}`
})
embed.setThumbnail("attachment://queueIcon.png")
await textChannel.send({ embeds: [embed], files: [queueIconLink] })
}
else if (command === "remove") {
if (args.length === 0) {
await textChannel.send("Please provide the argument.")
return
}
let argument = args.join(" ")
if (isNaN(argument)) {
await textChannel.send("Please enter a valid argument")
return
}
let serverQueue = queue.get(userGuild.id)
if (!serverQueue) {
await textChannel.send("No music playing, to remove")
return
}
let songNum = parseInt(argument)
if (songNum <= 0 || songNum > serverQueue.songs.length) {
await textChannel.send("Invalid choice")
return
}
if (songNum - 1 === 0) {
player.stop()
let embed = createEmbed({
title: "Removed Song :(",
description: serverQueue.songs[0].title
})
await textChannel.send({ embeds: [embed] })
return
}
let deletedSong = serverQueue.songs.splice(songNum - 1, 1)
let embed = createEmbed({
title: "Removed Song :(",
description: deletedSong[0].title
})
await textChannel.send({ embeds: [embed] })
}
else if (command === "dc" || command === "quit" || command === "disconnect" && args.length === 0) {
let serverQueue = queue.get(userGuild.id)
if (!serverQueue) {
await textChannel.send("I'm not in any Voice Channels. :(")
return
}
let currConnection = serverQueue.connection
player.stop()
currConnection.disconnect()
queue.delete(userGuild.id)
isPlaying = false
isPaused = false
await textChannel.send("Disconnected :(")
return
}
else {
await textChannel.send("Unknown command!!")
}
}
})
player.on("stateChange", async (oldState, newState) => {
if (newState.status === "idle" && oldState.status === "playing") {
try {
let guild_id = player.subscribers[0].connection.joinConfig.guildId
let serverQueue = queue.get(guild_id)
serverQueue.songs.shift()
if (serverQueue.songs.length === 0) {
let embed = createEmbed({ title: "End of Queue :(" })
await serverQueue.textChannel.send({ embeds: [embed] })
serverQueue.connection.disconnect()
queue.delete(guild_id)
return
}
else {
await playMusic(player, serverQueue)
let embed = createEmbed({
title: "Now Playing",
description: serverQueue.songs[0].title,
thumbnail: serverQueue.songs[0].thumbnailHigh,
extra: serverQueue.songs[0].authorChannel,
footer: serverQueue.songs[0].source
})
await serverQueue.textChannel.send({ embeds: [embed] })
}
}
catch (e) {
console.log("error while switching songs in queue")
console.log(e)
}
}
})
player.on("error", (error) => {
console.log("error occurred in player")
console.log(error)
})
client.login(TOKEN)