-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MoonLinkPlayer.js
213 lines (212 loc) · 5.64 KB
/
MoonLinkPlayer.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
"use strict";
const event = require('events')
const eventos = new event()
let { utils } = require('../structures/MoonLinkUtils.js')
let { MoonQueue } = require('../structures/MoonLinkQueue.js')
var request = function (x) {
utils.node.get()
.ws.send(JSON.stringify(x))
}
let map = utils.map
let player = map.get('players') || {}
let db = utils.db
class MoonPlayer {
constructor(infos) {
this.infos = infos
this.playing = this.infos.playing || null
this.connected = this.infos.connected || null
this.current = utils.track.current()
this.queue = new MoonQueue({ guildId: infos.guildId })
}
connect(setDeaf, setMute) {
let sendDs = utils.sendDs()
sendDs(this.infos.guildId, JSON.stringify({
op: 4
, d: {
guild_id: this.infos.guildId
, channel_id: this.infos.voiceChannel
, self_mute: setMute || false
, self_deaf: setDeaf || true
}
}))
this.connected = true
}
disconnect() {
console.log(this.infos)
if (!player[this.infos.guildId].connected) {
throw new TypeError(`[MOONLINK]: player not connected in voice channel`)
}
let sendDs = utils.sendDs()
sendDs(this.infos.guildId, JSON.stringify({
op: 4
, d: {
guild_id: this.infos.guildId
, channel_id: undefined
, self_mute: false
, self_deaf: false
}
}))
this.connected = false
player[this.infos.guildId] = {
...player[this.infos.guildId]
, playing: false
, connected: false
}
delete map.get('players')[this.infos.guildId]
map.set('players', player)
}
play() {
let queue = db.get('queue.' + this.infos.guildId)
let sendDs = utils.sendDs()
if (!queue) throw new Error('[ MoonLink.Js ]: the queue is empty, verify your play command.')
else {
let track = queue.shift();
if (!track) throw new Error(`[ MoonLink.Js ]: intel error`)
if (track) {
utils.track.editCurrent(track)
this.current = utils.track.current()
.track
db.set(`queue.${this.infos.guildId}`, queue)
let req = request({
op: 'play'
, guildId: this.infos.guildId
, channelId: this.infos.voiceChannel
, track: this.current
, volume: 80
, noReplace: false
, pause: false
})
player[this.infos.guildId] = {
...player[this.infos.guildId]
, playing: true
, connected: true
}
}
}
}
pause(u = true) {
if (typeof u !== 'boolean') {
throw new TypeError(`[ MOOONLINK ]: pause require boolean for pause player!`)
}
if (player[this.infos.guildId].pause) {
throw new TypeError(`[MOONLINK]: Player already is paused.`)
}
if(!u) {
player[this.infos.guildId] = {
...player[this.infos.guildId]
, paused: u
, playing: false
, connected: true
}
request({ op: 'pause', guildId: this.infos.guildId, pause: u })
} else {
player[this.infos.guildId] = {
...player[this.infos.guildId]
, paused: u
, playing: true
, connected: true
}
request({ op: 'pause', guildId: this.infos.guildId, pause: u })
}
}
volume(porcent) {
if (typeof porcent !== 'string' && typeof porcent !== 'number') {
throw new TypeError(`[ MOONLINK ]: a porcentage of volume needs to be a number!`)
}
request({ op: 'volume', guildId: this.infos.guildId, volume: porcent })
}
stop() {
if (!map.get('players')[this.infos.guildId]) {
throw new TypeError("[MOONLINK]: I can't stop a song when there's no player")
}
request({ op: 'stop', guildId: this.infos.guildId })
player[this.infos.guildId] = { ...player[this.infos.guildId], playing: false, connected: true }
}
destroy() {
let sendDs = utils.sendDs()
sendDs(this.infos.guildId, JSON.stringify({
op: 4
, d: {
guild_id: this.infos.guildId
, channel_id: null
, self_mute: false
, self_deaf: false
}
}))
request({ op: 'destroy', guildId: this.infos.guildId })
delete map.get(`players`)[this.infos.guildId]
db.delete(`queue.${this.infos.guildId}`)
}
skip() {
let queue = db.get(`queue.${this.infos.guildId}`)
if (!queue) {
throw new TypeError(`[ Moonlink.Js ] queue is empty!`)
} else if (queue[0]) {
if (player[this.infos.guildId].loop == 2) {
db.push(`queue.${this.infos.guildId}`, queue.shift())
utils.track.editCurrent(queue[0])
}
if (!queue[0]) {
this.destroy()
return false
}
let actualtrack = queue.shift()
utils.track.editCurrent(actualtrack)
db.set(`queue.${this.infos.guildId}`, queue)
utils.track.skipEdit(true)
let a = request({
op: 'play'
, channelId: this.infos.voiceChannel
, guildId: this.infos.guildId
, track: utils.track.current()
.track
})
}
}
replay() {
let queue = db.get(`queue.${this.infos.guildId}`)
if (!utils.track.current()) {
throw new TypeError(`[MoonlinkJs]: no has a current track to replay.`)
}
if (queue && !queue[0]) return;
utils.track.skipEdit(true)
request({
op: 'play'
, channelId: this.infos.voiceChannel
, guildId: this.infos.guildId
, track: utils.track.current()
.track
});
}
loop(r) {
if (!r) {
throw new TypeError(`[ Moonlink.Js ] loop parameter is empty.`)
}
if (typeof r !== 'string' && typeof r !== 'number') {
throw new TypeError(`[ Moonlink.Js ] loop parameter need to be string and number!`)
}
if (r > 3) {
throw new TypeError(`[ Moonlink.Js ] ${r} is not a option parameter required by loop, try this:
1 - for loop song
2 - for loop queue
3 - for no loop`)
}
if (r == 1) {
player[this.infos.guildId] = {
...player[this.infos.guildId]
, loop: 1
}
} else if (r == 2) {
player[this.infos.guildId] = {
...player[this.infos.guildId]
, loop: 2
}
} else if (r == 3) {
player[this.infos.guildId] = {
...player[this.infos.guildId]
, loop: false
}
}
}
}
module.exports.MoonPlayer = MoonPlayer;