-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdiscord.js
276 lines (203 loc) · 7.09 KB
/
discord.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
const { Client } = require('discord.js');
const ytdl = require('ytdl-core');
const { token } = require('./token.json');
const { prefix } = require('./config.json');
const client = new Client();
// 建立一個類別來管理 Property 及 Method
class Music {
constructor() {
/**
* 下面的物件都是以 Discord guild id 當 key,例如:
* this.isPlaying = {
* 724145832802385970: false
* }
*/
/**
* 機器人是否正在播放音樂
* this.isPlaying = {
* 724145832802385970: false
* }
*/
this.isPlaying = {};
/**
* 等待播放的音樂隊列,例如:
* this.queue = {
* 724145832802385970: [{
* name: 'G.E.M.鄧紫棋【好想好想你 Missing You】Official Music Video',
* url: 'https://www.youtube.com/watch?v=P6QXo88IG2c&ab_channel=GEM%E9%84%A7%E7%B4%AB%E6%A3%8B'
* }]
* }
*/
this.queue = {};
// https://discord.js.org/#/docs/main/stable/class/VoiceConnection
this.connection = {};
// https://discord.js.org/#/docs/main/stable/class/StreamDispatcher
this.dispatcher = {};
}
async join(msg) {
// 如果使用者正在頻道中
if (msg.member.voice.channel !== null) {
// Bot 加入語音頻道
this.connection[msg.guild.id] = await msg.member.voice.channel.join();
} else {
msg.channel.send('請先進入語音頻道');
}
}
async play(msg) {
// 語音群的 ID
const guildID = msg.guild.id;
// 如果 Bot 還沒加入該語音群的語音頻道
if (!this.connection[guildID]) {
msg.channel.send('請先將機器人 `!!join` 加入頻道');
return;
}
// 如果 Bot leave 後又未加入語音頻道
if (this.connection[guildID].status === 4) {
msg.channel.send('請先將機器人 `!!join` 重新加入頻道');
return;
}
// 處理字串,將 !!play 字串拿掉,只留下 YouTube 網址
const musicURL = msg.content.replace(`${prefix}play`, '').trim();
try {
// 取得 YouTube 影片資訊
const res = await ytdl.getInfo(musicURL);
const info = res.videoDetails;
// 將歌曲資訊加入隊列
if (!this.queue[guildID]) {
this.queue[guildID] = [];
}
this.queue[guildID].push({
name: info.title,
url: musicURL
});
// 如果目前正在播放歌曲就加入隊列,反之則播放歌曲
if (this.isPlaying[guildID]) {
msg.channel.send(`歌曲加入隊列:${info.title}`);
} else {
this.isPlaying[guildID] = true;
this.playMusic(msg, guildID, this.queue[guildID][0]);
}
} catch(e) {
console.log(e);
}
}
playMusic(msg, guildID, musicInfo) {
// 提示播放音樂
msg.channel.send(`播放音樂:${musicInfo.name}`);
// 播放音樂
this.dispatcher[guildID] = this.connection[guildID].play(ytdl(musicInfo.url, { filter: 'audioonly' }));
// 把音量降 50%,不然第一次容易被機器人的音量嚇到 QQ
this.dispatcher[guildID].setVolume(0.5);
// 移除 queue 中目前播放的歌曲
this.queue[guildID].shift();
// 歌曲播放結束時的事件
this.dispatcher[guildID].on('finish', () => {
// 如果隊列中有歌曲
if (this.queue[guildID].length > 0) {
this.playMusic(msg, guildID, this.queue[guildID][0]);
} else {
this.isPlaying[guildID] = false;
msg.channel.send('目前沒有音樂了,請加入音樂 :D');
}
});
}
resume(msg) {
if (this.dispatcher[msg.guild.id]) {
msg.channel.send('恢復播放');
// 恢復播放
this.dispatcher[msg.guild.id].resume();
}
}
pause(msg) {
if (this.dispatcher[msg.guild.id]) {
msg.channel.send('暫停播放');
// 暫停播放
this.dispatcher[msg.guild.id].pause();
}
}
skip(msg) {
if (this.dispatcher[msg.guild.id]) {
msg.channel.send('跳過目前歌曲');
// 跳過歌曲
this.dispatcher[msg.guild.id].end();
}
}
nowQueue(msg) {
// 如果隊列中有歌曲就顯示
if (this.queue[msg.guild.id] && this.queue[msg.guild.id].length > 0) {
// 字串處理,將 Object 組成字串
const queueString = this.queue[msg.guild.id].map((item, index) => `[${index+1}] ${item.name}`).join();
msg.channel.send(queueString);
} else {
msg.channel.send('目前隊列中沒有歌曲');
}
}
leave(msg) {
// 如果機器人在頻道中
if (this.connection[msg.guild.id] && this.connection[msg.guild.id].status === 0) {
// 如果機器人有播放過歌曲
if (this.queue.hasOwnProperty(msg.guild.id)) {
// 清空播放列表
delete this.queue[msg.guild.id];
// 改變 isPlaying 狀態為 false
this.isPlaying[msg.guild.id] = false;
}
// 離開頻道
this.connection[msg.guild.id].disconnect();
} else {
msg.channel.send('機器人未加入任何頻道');
}
}
}
const music = new Music();
// 當 Bot 接收到訊息時的事件
client.on('message', async (msg) => {
// 如果發送訊息的地方不是語音群(可能是私人),就 return
if (!msg.guild) return;
// !!join
if (msg.content === `${prefix}join`) {
// 機器人加入語音頻道
music.join(msg);
}
// 如果使用者輸入的內容中包含 !!play
if (msg.content.indexOf(`${prefix}play`) > -1) {
// 如果使用者在語音頻道中
if (msg.member.voice.channel) {
// 播放音樂
await music.play(msg);
} else {
// 如果使用者不在任何一個語音頻道
msg.reply('你必須先加入語音頻道');
}
}
// !!resume
if (msg.content === `${prefix}resume`) {
// 恢復音樂
music.resume(msg);
}
// !!pause
if (msg.content === `${prefix}pause`) {
// 暫停音樂
music.pause(msg);
}
// !!skip
if (msg.content === `${prefix}skip`) {
// 跳過音樂
music.skip(msg);
}
// !!queue
if (msg.content === `${prefix}queue`) {
// 查看隊列
music.nowQueue(msg);
}
// !!leave
if (msg.content === `${prefix}leave`) {
// 機器人離開頻道
music.leave(msg);
}
});
// 連上線時的事件
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login(token);