-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoiceChannel.js
87 lines (78 loc) · 2.3 KB
/
VoiceChannel.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
const ytdl = require('ytdl-core')
class VoiceChannel {
constructor(channel, callback) {
this.id = channel.id
this.channel = channel
this.guild = this.channel.guild
this.queue = []
this.playing = false
this.callback = callback
this.volume = 100
this.repeat = false
}
add(data) {
return new Promise(resolve => {
this.queue.push(data)
resolve(this.queue)
if (!this.playing) this.loop()
// TODO: 最大件数とか
})
}
remove(index) {
return new Promise((resolve, reject) => {
if (typeof index !== 'number' || index < 1) return reject('INVAILD_VALUE')
this.queue.splice(index, 1)
resolve(this.queue)
})
}
loop() {
this.playing = true
console.log('playing', this.queue[0].id)
// this.setTitle(this.queue[0].title)
const stream = ytdl(this.queue[0].id, {filter: 'audioonly'})
this.channel.join().then(connection => {
this.dispatcher = connection.playStream(stream).on('end', () => {
// this.setTitle()
this.playing = false
if (this.repeat) this.queue.push(this.queue[0])
this.queue.shift()
this.callback(this.queue)
if (this.queue[0]) this.loop()
else this.channel.leave()
}).on('error', console.error)
this.dispatcher.setVolume(0.1)
}).catch(console.error)
}
setVolume(volume) {
return new Promise((resolve, reject) => {
if (this.volume === volume) return
if (!this.dispatcher) return reject('NOT_PLAYING_YET')
// FrontEnd 0 25 50 75 100
// Discord 0 0.5 1 1.5 2
// vol * 2 / 100 = vol / 50
this.dispatcher.setVolume(volume / 1000)
this.volume = volume
resolve(volume)
})
}
async setRepeat(repeat) {
this.repeat = repeat
console.log('repeat', repeat)
return repeat
}
skip() {
return new Promise((resolve, reject) => {
if (!this.dispatcher) return reject('NOT_PLAYING_YET')
this.dispatcher.end()
console.log('Skipped')
})
}
// setTitle(title) {
// const me = this.guild.me
// if (!me || !me.hasPermission('CHANGE_NICKNAME')) return
// if (!title) return me.setNickname(null)
// if (title.length > 28) title = title.slice(0, 25) + '...'
// me.setNickname(title + 'を再生中')
// }
}
module.exports = VoiceChannel