77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { AudioPlayer, AudioPlayerStatus } from "@discordjs/voice";
|
|
import { player } from "./main";
|
|
import { CacheType, ChatInputCommandInteraction } from "discord.js";
|
|
import { getAudioFile } from "./util/downloader";
|
|
import { playSong } from "./util/helpers";
|
|
|
|
const queue: Queue = {
|
|
songList: [],
|
|
current: null
|
|
}
|
|
|
|
export async function requestSong(
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
url: string) {
|
|
|
|
const path = await getAudioFile(url)
|
|
queue.songList.push({ path, url })
|
|
updatePlayer()
|
|
|
|
}
|
|
|
|
export async function forceRequestSong(
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
url: string) {
|
|
|
|
const path = await getAudioFile(url)
|
|
const audio = { path, url }
|
|
queue.songList.push(audio)
|
|
queue.current = audio
|
|
playSong(player, path);
|
|
}
|
|
|
|
|
|
export async function updatePlayer() {
|
|
if (player.state.status === AudioPlayerStatus.Idle) {
|
|
const nextSong = queue.songList.shift()
|
|
if (!nextSong) {
|
|
queue.current = null
|
|
return
|
|
};
|
|
queue.current = nextSong;
|
|
playSong(player, nextSong.path);
|
|
}
|
|
}
|
|
|
|
export function skip_song(): AudioFile | null {
|
|
if (player.state.status === AudioPlayerStatus.Playing) {
|
|
const skipped = queue.current
|
|
const nextSong = queue.songList.shift()
|
|
if (!nextSong) {
|
|
queue.current = null
|
|
player.stop()
|
|
return null
|
|
};
|
|
queue.current = nextSong;
|
|
playSong(player, nextSong.path);
|
|
return skipped
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function pause_playback(player: AudioPlayer) {
|
|
player.pause()
|
|
}
|
|
|
|
export function stop_playback(player: AudioPlayer) {
|
|
player.stop()
|
|
}
|
|
|
|
export function resume_playback(player: AudioPlayer) {
|
|
player.unpause()
|
|
}
|
|
|
|
export function getQueue(): Queue {
|
|
return queue
|
|
}
|