forked from MotiCAT/TuneNekoSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.ts
105 lines (96 loc) · 3.09 KB
/
player.ts
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
import { getSongInfo } from '../Utils/songResolver';
import { client } from '../index';
import type { Queue } from './queue';
import { queueManager } from './queue';
import { joinVoiceChannel, createAudioPlayer } from '@discordjs/voice';
import { createAudioResource, StreamType, AudioPlayerStatus } from '@discordjs/voice';
import { Snowflake, VoiceBasedChannel } from 'discord.js';
import ytdl from 'ytdl-core';
export class YTPlayer {
private connection: import('@discordjs/voice').VoiceConnection;
public player: import('@discordjs/voice').AudioPlayer;
public serverId: Snowflake;
public messageChannelId: Snowflake;
public queue: Queue;
public volume: number;
public isPlaying: boolean;
public resource: import('@discordjs/voice').AudioResource | null;
constructor(serverId: Snowflake, voiceChannel: VoiceBasedChannel, messageChannelId: Snowflake) {
this.isPlaying = false;
this.serverId = serverId;
this.messageChannelId = messageChannelId;
this.connection = joinVoiceChannel({
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
channelId: voiceChannel.id,
guildId: serverId,
selfDeaf: true,
selfMute: false
});
this.player = createAudioPlayer();
this.queue = queueManager.getQueue(serverId) as Queue;
this.volume = 0.1;
this.player
.on('subscribe', () => {
this.isPlaying = true;
})
.on('unsubscribe', () => {
this.isPlaying = false;
})
.on(AudioPlayerStatus.Idle, () => this.playNextSong());
this.resource = null;
}
public play() {
const queue = queueManager.getQueue(this.serverId);
const stream = ytdl(ytdl.getURLVideoID(queue?.currentSong as string), {
filter: (format) => format.audioCodec === 'opus' && format.container === 'webm',
quality: 'highest',
highWaterMark: 32 * 1024 * 1024
});
this.resource = createAudioResource(stream, {
inputType: StreamType.WebmOpus,
inlineVolume: true
});
this.resource.volume?.setVolume(this.volume);
this.connection.subscribe(this.player);
this.player.play(this.resource);
}
public pause(): void {
this.player.pause();
}
public resume(): void {
this.player.unpause();
}
public stop(): void {
this.connection.destroy();
queueManager.deleteQueue(this.serverId);
client.player = undefined;
}
public skip(): void {
this.playNextSong();
}
public changeVolume(volume: number): void {
if (!this.resource) return;
this.volume = volume;
this.resource.volume?.setVolume(volume / 10);
}
private async playNextSong(): Promise<void> {
if (this.queue.loop === 'none') {
if (!this.queue.store.length) return;
else {
if (this.queue.store.length >= 1) this.queue.removeSong(0);
if (!this.queue.store.length) return this.stop();
await this.fetchSongData();
return this.play();
}
}
if (this.queue.loop === 'queue') this.queue.loopQueue();
if (this.queue.loop === 'track') this.queue.loopTrack();
this.play();
await this.fetchSongData();
}
private async fetchSongData() {
const channel = client.channels.cache.get(this.messageChannelId);
if (!channel) return;
if (channel.isTextBased()) channel.send(await getSongInfo(this.queue.currentSong!));
}
}