added loop

This commit is contained in:
Patryk Koreń
2026-02-12 17:51:08 +01:00
parent f9f0ba9b1f
commit 20a36703e6
4 changed files with 50 additions and 6 deletions

View File

@@ -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,
}

34
src/commands/loop.ts Normal file
View File

@@ -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<CacheType>) {
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
}

9
src/global.d.ts vendored
View File

@@ -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<CacheType>,
url: string,
interaction: ChatInputCommandInteraction<CacheType>
url: string
}

View File

@@ -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