Skip to content

Commit

Permalink
🗃️ Division of Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nich87 committed Dec 3, 2023
1 parent 6fb09fb commit 02fc2f3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 92 deletions.
93 changes: 93 additions & 0 deletions src/classes/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 queue: Queue<string>;
public volume: number;
public isPlaying: boolean;
constructor(serverId: Snowflake, voiceChannel: VoiceBasedChannel) {
this.isPlaying = false;
this.serverId = serverId;
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<string>;
this.volume = 100 / 10;
this.player
.on('subscribe', () => {
this.isPlaying = true;
})
.on('unsubscribe', () => {
this.isPlaying = false;
})
.on(AudioPlayerStatus.Idle, () => this.playNextSong());
}

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
});
const resource = createAudioResource(stream, {
inputType: StreamType.WebmOpus,
inlineVolume: true
});
resource.volume?.setVolume(0.1);
this.connection.subscribe(this.player);
this.player.play(resource);
}

public pause(): void {
this.player.pause();
}

public resume(): void {
this.player.unpause();
}

public stop(): void {
this.player.stop();
this.connection.destroy();
queueManager.deleteQueue(this.serverId);
}

skip(): void {
this.player.stop();
this.playNextSong();
}

public changeVolume(volume: number): void {
this.volume = volume / 10;
}

private playNextSong(): 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 === 1) {
this.play();
return this.queue.removeSong(0);
}
return this.play();
}
}
if (this.queue.loop === 'queue') this.queue.loopQueue();
if (this.queue.loop === 'track') this.queue.loopTrack();
this.play();
}
}
92 changes: 1 addition & 91 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { joinVoiceChannel, createAudioPlayer } from '@discordjs/voice';
import { createAudioResource, StreamType, AudioPlayerStatus } from '@discordjs/voice';
import { Snowflake, VoiceBasedChannel } from 'discord.js';
import ytdl from 'ytdl-core';
import { Snowflake } from 'discord.js';

export class Queue<T> {
private _store: string[];
Expand Down Expand Up @@ -76,90 +73,3 @@ class QueueManager {
}

export const queueManager = new QueueManager();

export class YTPlayer {
private connection: import('@discordjs/voice').VoiceConnection;
public player: import('@discordjs/voice').AudioPlayer;
public serverId: Snowflake;
public queue: Queue<string>;
public volume: number;
public isPlaying: boolean;
constructor(serverId: Snowflake, voiceChannel: VoiceBasedChannel) {
this.isPlaying = false;
this.serverId = serverId;
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<string>;
this.volume = 100 / 10;
this.player
.on('subscribe', () => {
this.isPlaying = true;
})
.on('unsubscribe', () => {
this.isPlaying = false;
})
.on(AudioPlayerStatus.Idle, () => this.playNextSong());
}

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
});
const resource = createAudioResource(stream, {
inputType: StreamType.WebmOpus,
inlineVolume: true
});
resource.volume?.setVolume(0.1);
this.connection.subscribe(this.player);
this.player.play(resource);
}

public pause(): void {
this.player.pause();
}

public resume(): void {
this.player.unpause();
}

public stop(): void {
this.player.stop();
this.connection.destroy();
queueManager.deleteQueue(this.serverId);
}

skip(): void {
this.player.stop();
this.playNextSong();
}

public changeVolume(volume: number): void {
this.volume = volume / 10;
}

private playNextSong(): 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 === 1) {
this.play();
return this.queue.removeSong(0);
}
return this.play();
}
}
if (this.queue.loop === 'queue') this.queue.loopQueue();
if (this.queue.loop === 'track') this.queue.loopTrack();
this.play();
}
}
3 changes: 2 additions & 1 deletion src/commands/play.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Queue, queueManager, YTPlayer } from '../classes/queue';
import { YTPlayer } from '../classes/player';
import { Queue, queueManager } from '../classes/queue';
import { Message, EmbedBuilder, ChannelType, VoiceBasedChannel } from 'discord.js';
import ytdl from 'ytdl-core';

Expand Down

0 comments on commit 02fc2f3

Please sign in to comment.