This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
/
app.js
91 lines (74 loc) · 2.96 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
const config = require("./config.json");
const fetch = require("node-fetch");
const { URLSearchParams } = require("url");
const { Client } = require("discord.js");
const { PlayerManager } = require("../src/index");
class MusicClient extends Client {
constructor(...args) {
super(...args);
this.player = null;
this.on("ready", () => {
this.player = new PlayerManager(client, config.nodes, {
user: client.user.id,
shards: 0
});
console.log("Bot is online!");
}).on("error", console.error).on("warn", console.warn);
}
}
const client = new MusicClient();
client.on("message", async msg => {
if (msg.author.bot || !msg.guild) return;
if (!msg.content.startsWith(config.prefix)) return;
const args = msg.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "play") {
if (!msg.member || !msg.member.voice.channel) return msg.reply("You must be in a voice channel for this command.");
const track = args.join(" ");
const [song] = await getSongs(`ytsearch: ${track}`);
if (!song) return msg.reply("No songs found. try again!");
const player = await client.player.join({
guild: msg.guild.id,
channel: msg.member.voice.channel.id,
host: client.player.nodes.first().host
}, { selfdeaf: true });
if (!player) return msg.reply("Could not join");
player.play(song.track);
player.once("error", console.error);
player.once("end", async data => {
if (data.reason === "REPLACED") return;
msg.channel.send("Song has ended...");
await client.player.leave(msg.guild.id);
});
return msg.reply(`Now playing: **${song.info.title}** by *${song.info.author}*`);
}
if (command === "leave") {
await client.player.leave(msg.guild.id);
return msg.reply("Successfully left the voice channel");
}
if (command === "pause") {
const player = client.player.get(msg.guild.id);
if (!player) return msg.reply("No lavalink player found");
await player.pause(true);
return msg.reply("Paused the music");
}
if (command === "resume") {
const player = client.player.get(msg.guild.id);
if (!player) return msg.reply("No lavalink player found");
await player.pause(false);
return msg.reply("Resumed the music");
}
});
function getSongs(search) {
const node = client.player.nodes.first();
const params = new URLSearchParams();
params.append("identifier", search);
return fetch(`http://${node.host}:${node.port}/loadtracks?${params.toString()}`, { headers: { Authorization: node.password } })
.then(res => res.json())
.then(data => data.tracks)
.catch(err => {
console.error(err);
return null;
});
}
client.login(config.token);