From 20a36703e6c563010be08e59f21413548b11c78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kore=C5=84?= Date: Thu, 12 Feb 2026 17:51:08 +0100 Subject: [PATCH] added loop --- src/commands/index.ts | 2 ++ src/commands/loop.ts | 34 ++++++++++++++++++++++++++++++++++ src/global.d.ts | 9 +++++---- src/playback.ts | 11 +++++++++-- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/commands/loop.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index 369e604..a0b3168 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -7,6 +7,7 @@ import queue from "./queue" import resume from "./resume" import skip from "./skip" import stop from "./stop" +import loop from "./loop" export const commands: { [key: string]: Command } = { ping, @@ -18,5 +19,6 @@ export const commands: { [key: string]: Command } = { stop, pause, skip, + loop, } diff --git a/src/commands/loop.ts b/src/commands/loop.ts new file mode 100644 index 0000000..464e809 --- /dev/null +++ b/src/commands/loop.ts @@ -0,0 +1,34 @@ +import { CacheType, ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'; +import { toggleLoop } from '../playback'; +import { formatFilePath } from '../util/downloader' + + +const name = "loop" + +function register() { + return new SlashCommandBuilder() + .setName(name) + .setDescription('loop current song') +} + +async function execute(interaction: ChatInputCommandInteraction) { + try { + await interaction.deferReply() + const audio = toggleLoop() + if (audio) { + await interaction.editReply(`looped ${formatFilePath(audio.url)}`); + } + else { + await interaction.editReply(`Loop turned off`); + } + } catch (error) { + console.error(error); + await interaction.reply('Coś poszło nie tak :/'); + } +} + +export default { + name, + register, + execute +} diff --git a/src/global.d.ts b/src/global.d.ts index b1f8c43..37160d4 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -10,12 +10,13 @@ type AudioFile = { } type Queue = { - songList: AudioFile[], - current: AudioFile | null, + songList: AudioFile[] + current: AudioFile | null + loop: AudioFile | null } type HistoryObject = { - interaction: ChatInputCommandInteraction, - url: string, + interaction: ChatInputCommandInteraction + url: string } diff --git a/src/playback.ts b/src/playback.ts index b887033..6397ee5 100644 --- a/src/playback.ts +++ b/src/playback.ts @@ -7,7 +7,14 @@ import { add_to_history } from "./util/history"; const queue: Queue = { songList: [], - current: null + current: null, + loop: null, +} + +export function toggleLoop(): AudioFile | null { + if (queue.loop) queue.loop = null + else queue.loop = queue.current + return queue.loop } export async function requestSong( @@ -36,7 +43,7 @@ export async function forceRequestSong( export async function updatePlayer() { if (player.state.status === AudioPlayerStatus.Idle) { - const nextSong = queue.songList.shift() + const nextSong = queue.loop ?? queue.songList.shift() if (!nextSong) { queue.current = null return