This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
157 lines (128 loc) · 4.64 KB
/
music.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
const ytdl = require('ytdl-core')
const Handles = new Map()
const yt = require('simple-youtube-api')
const youtube = require('yt-search')
const sf = require('snekfetch')
const Discord = require('discord.js')
process.on('unhandledRejection', function (err) {
console.log(err)
})
module.exports.create = (identificate, voiceChannel, message) => {
if (!Handles.get(identificate)) {
Handles.set(identificate, {
voiceChannel: voiceChannel,
queue: [],
volume: 50,
channel: message.channel,
playing: false,
dispatcher: false
})
}
}
module.exports.start = (identificate) => {
if (!Handles.get(identificate).playing) return this.startStream(identificate)
}
module.exports.next = (identificate) => {
this.getGuild(identificate).queue.shift()
if (this.getQueue(identificate)[0]) {
if (Handles.get(identificate).voiceChannel.members.filter(m => !m.user.bot).size == 0) {
let embed = new Discord.MessageEmbed()
.setColor(require('./config').color)
.addField('채널에 아무도 없습니다.', '저런 노래는 듣지도 않으면서 틀어놨군... 전기를 아껴주라구! \`#p\`커맨드로 대기열을 다시 불러오고 재생할 수 있어!',)
getGuild(identificate).channel.send(embed)
Handles.remove(identificate)
} else if(Handles.get(identificate).voiceChannel.members.filter(m => !m.user.bot).size != 0){
Handles.get(identificate).playing = false
this.startStream(identificate)
}
}
else{
this.endStream(identificate)
}
}
module.exports.getNP = getNP
function getNP(identificate) {
const latest = Handles.get(identificate).queue[0]
return latest
}
module.exports.getQueue = (identificate) => {
const latest = Handles.get(identificate)
if (latest) {
return latest.queue
} else {
return false
}
}
module.exports.getGuild = (identificate) => {
return Handles.get(identificate)
}
function getGuild(identificate) {
return Handles.get(identificate)
}
module.exports.addQueue = async (identificate, videoURL, message, info, thumb) => {
let q = {
url: videoURL,
author: message.author.id,
vote: 0,
time: new Date(),
info: info,
thumbnail : thumb
}
Handles.get(identificate).queue.push(q)
}
module.exports.removeQueue = (identificate, index) => {
if(index == 0) {
this.next(identificate)
}
else Handles.get(identificate).queue.splice(index, 1)
}
module.exports.resetQueue = (identificate) => {
Handles.get(identificate).queue.splice(0, Handles.get(identificate).queue.length)
this.endStream(identificate)
}
module.exports.startStream = (identificate) => {
if (Handles.get(identificate).playing) return
const stream = ytdl(this.getNP(identificate).url, {
quality: 'highest',
filter: 'audioonly'
}).on('error', error => {
console.log(error)
})
Handles.get(identificate).voiceChannel.join().then(connection => {
const dispatcher = connection.play(stream, { bitrate: 96 })
Handles.get(identificate).dispatcher = dispatcher
Handles.get(identificate).playing = true
dispatcher.setVolume(this.getGuild(identificate).volume / 100)
dispatcher.on('finish', () => {
if(!Handles.get(identificate)) return
Handles.get(identificate).playing = false
this.next(identificate)
})
dispatcher.on('start', () => {
let info = getNP(identificate).info.title
getGuild(identificate).channel.send('<a:playforpark:708621715571474482>음악을 재생합니다!' + random(require('./config').playmsg).replace('%song%', '`'+info+'`'))
})
})
}
module.exports.endStream = (identificate, message) => {
Handles.get(identificate).voiceChannel.leave()
if(!message) {
Handles.get(identificate).channel.send("더이상 플레이할 노래가 없어, 대기열을 초기화됬어! 그럼 난 이만👋")
}
else if(message){
message.channel.send("더이상 플레이할 노래가 없어 : 대기열을 초기화했어! 그럼 난 이만👋")
}
Handles.delete(identificate)
}
module.exports.pauseStream = (identificate) => {
Handles.get(identificate).dispatcher.pause()
}
module.exports.resumeStream = (identificate) => {
Handles.get(identificate).dispatcher.resume()
}
module.exports.changeStatus = (identificate, status) => {
Handles.get(identificate).playing = status
}
function random(items) {
return items[Math.floor(Math.random() * items.length)]
}