-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (91 loc) · 2.61 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
const { Client } = require("discord.js");
const dotenv = require("dotenv");
const APIs = require("apis");
const {
getVoiceConnection,
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
entersState,
VoiceConnectionStatus,
NoSubscriberBehavior,
} = require("@discordjs/voice");
/////////////////////////////////////////
// DOTENV
/////////////////////////////////////////
// Load .env file.
dotenv.config();
/////////////////////////////////////////
// GLOBAL
/////////////////////////////////////////
const player = createAudioPlayer({
behaviors: {
noSubscriber : NoSubscriberBehavior.Play,
maxMissedFrames : Math.round(5000 / 20),
}
});
const client = new Client({
intents: [
"GUILDS",
"GUILD_VOICE_STATES",
"GUILD_MESSAGES"
]
});
// Start music.
APIs.fetch(`GET`, `https://chai5she.cdn.dvmr.fr/bfmtv`, { stream: true }).then(stream => {
player.play(createAudioResource(stream));
});
/////////////////////////////////////////
// MUSIC
/////////////////////////////////////////
async function joinChannel(voiceChannel) {
let connection = joinVoiceChannel({
channelId : voiceChannel.id,
guildId : voiceChannel.guild.id,
adapterCreator : voiceChannel.guild.voiceAdapterCreator,
selfDeaf : false,
selfMute : false
});
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30000);
// Subscribe player.
connection.subscribe(player);
return connection;
} catch (err) {
connection.destroy();
throw (err);
}
}
/////////////////////////////////////////
// EVENTS
/////////////////////////////////////////
client.on('ready', function () {
console.log(`${client.user.tag} ready !`);
// Set presence.
client.user.setPresence({
activity: {
type : "LISTENING",
name : "BFMTV",
}
});
});
client.on('messageCreate', function (message) {
switch (message.content) {
case "bfmtv join":
// Delete message.
message.delete().catch(console.error);
// Join channel.
joinChannel(message.member.voice.channel);
break;
case "bfmtv quit":
// Delete message.
message.delete().catch(console.error);
// Leave channel.
getVoiceConnection(message.guild.id).destroy();
break;
}
});
/////////////////////////////////////////
// MAIN
/////////////////////////////////////////
client.login(process.env.DISCORD_BOT_TOKEN);