-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.js
326 lines (293 loc) · 7.67 KB
/
functions.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const
{
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
NoSubscriberBehavior,
getVoiceConnection
} = require("@discordjs/voice"),
audioPlayer = new Map(),
audioPlayerData = {
behaviors: {
maxMissedFrames: Math.round(5000 / 20),
noSubscriber: NoSubscriberBehavior.Pause
}
},
audioResourceData = {
inlineVolume: true
};
module.exports = {
/**
*
* @param {string[]} string_array An array of strings to encoding
* @returns {string} The encoding of the return value.
*/
toHash: function (string_array) {
const
crypto = require("crypto"),
hash = crypto.createHmac("sha256", string_array[0]);
for (let index = 1; index < string_array.length; index++)
hash.update(string_array[index])
return hash.digest("hex")
},
/**
*
* @param {object} object
* @returns {string}
*/
getAPI: function (object) {
object = JSON.stringify(object);
const
{ apiKey, apiSecret } = require("./config.json"),
host = "https://www.personalityforge.com/api/chat/",
hash = this.toHash([apiSecret, object]),
url = `${host}?apiKey=${apiKey}&hash=${hash}&message=${encodeURIComponent(object)}`;
return url;
},
/**
* @description This is player class can be use play online resource in discord voice channel.
* @example
* ```js
* // Load class.
* const player = new Player(interaction);
*
* // Play the url.
* player.play("url");
*
* // Get connection volume.
* console.log(player.volume); // output: <player.volume> like 100
*
* // Set connection volume.
* console.log(player.setVolume(50)); // output: 50
*
* // Pause the player.
* if(player.isPaused()) // If it's true it should be resumed.
* player.resume();
*
* else
* // This one sould be paused.
* player.pause();
*
* // Radio mode.
* player.radio(["url"]);
*
* // Stop the player.
* player.stop();
* ```
*/
Player: class Player {
/**
*
* @description To use this class with default settings you may to add "interaction" to it.
* @param {import("discord.js").CommandInteraction} interaction
* @returns {Player}
*/
constructor(interaction) {
if (interaction)
this.data = {
channelId: interaction.member.voice?.channel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
selfDeaf: true
};
return this;
}
/**
*
* @description Set player data's for using the class.
* @param {import("@discordjs/voice").JoinVoiceChannelOptions & import("@discordjs/voice").CreateVoiceConnectionOptions} param0
* @returns {Player}
*/
setData({ channelId, guildId, adapterCreator, selfDeaf = true }) {
this.data = {
channelId,
guildId,
adapterCreator,
selfDeaf
};
return this;
}
/**
*
* @description Set guild id of player data.
* @param {string} guildId
* @returns {Player}
*/
setGuildId(guildId) {
this.data.guildId = guildId;
return this;
}
/**
*
* @description Set adapterCreator of player data.
* @param {import("discord.js").InternalDiscordGatewayAdapterCreator} adapterCreator
* @returns {Player}
*/
setAdapterCreator(adapterCreator) {
this.data.adapterCreator = adapterCreator;
return this;
}
/**
*
* @description Set bot deaf in the voice channel.
* @param {boolean} selfDeaf
* @returns {Player}
*/
setSelfDeaf(selfDeaf) {
this.data.selfDeaf = selfDeaf;
return this;
}
/**
*
* @description Set voice channel id to join or play something there.
* @param {string} channelId
* @returns {Player}
*/
setVoiceChannelId(channelId) {
this.data.channelId = channelId;
return this;
}
/**
*
* @description Start playing resource in the voice.
* @param {string} resource
* @returns {import("@discordjs/voice").AudioPlayer}
*/
async play(resource) {
const connection = this.join();
const player = createAudioPlayer(audioPlayerData);
player.play(
createAudioResource(await this.#createStream(resource), audioResourceData)
);
connection.subscribe(player);
audioPlayer.set(this.data.guildId, player);
return player;
}
/**
*
* @description Join to voice channel.
* @param {string} guildId
* @returns {import("@discordjs/voice").VoiceConnection}
*/
join() {
if (this.isConnection(this.data.guildId))
return getVoiceConnection(this.data.guildId);
return joinVoiceChannel(this.data);
}
/**
*
* @description Find a voice connection and return it.
* @returns {import("@discordjs/voice").VoiceConnection}
*/
get connection() {
return getVoiceConnection(this.data.guildId);
}
/**
*
* @description Find a voice connection and return it to boolean.
* @param {string} guildId
* @returns {boolean}
*/
isConnection(guildId) {
if (getVoiceConnection(guildId))
return true;
return false;
}
/**
*
* @description Get player volume numver and return it.
* @returns {number}
*/
get volume() {
const player = audioPlayer.get(this.data.guildId);
return Number(player.state.resource.volume.volume * 100);
}
/**
*
* @description Find a voice connection and return it to boolean.
* @param {number} input
* @returns {number}
*/
setVolume(input) {
const connection = this.connection;
const player = audioPlayer.get(this.data.guildId);
if (input <= 200 && input >= 0)
player.state.resource.volume.volume = input / 100;
connection.subscribe(player);
return Number(this.volume);
}
/**
*
* @description Check is player paused or not and return boolean.
* @returns {boolean}
*/
isPaused() {
const player = audioPlayer.get(this.data.guildId);
if (player.state.status === "paused")
return true;
else return false;
}
/**
*
* @description Pausing the player.
* @returns {void}
*/
pause() {
const connection = this.connection;
const player = audioPlayer.get(this.data.guildId);
if (!this.isPaused())
player.pause();
connection.subscribe(player);
return this;
}
/**
*
* @description Resuming the player.
* @returns {void}
*/
resume() {
const connection = this.connection;
const player = audioPlayer.get(this.data.guildId);
if (this.isPaused())
player.unpause();
connection.subscribe(player);
return this;
}
/**
*
* @description Stop the player and break the connection.
* @returns {void}
*/
stop() {
const connection = this.connection;
const player = audioPlayer.get(this.data.guildId);
player.stop();
connection.disconnect();
audioPlayer.delete(this.data.guildId);
return this;
}
/**
*
* @param {string} url
* @returns {stream}
*/
async #createStream(url) {
try {
const response = await fetch(url);
url = response.url;
} catch {
url = url;
}
return url;
}
}
}
/**
* @copyright
* Coded by Sobhan-SRZA (mr.sinre) | https://github.com/Sobhan-SRZA
* @copyright
* Work for Persian Caesar | https://dsc.gg/persian-caesar
* @copyright
* Please Mention Us "Persian Caesar", When Have Problem With Using This Code!
* @copyright
*/