fix: rozpierdalał sie przy dodawaniu do kolejki kiedy coś grało

This commit is contained in:
Patryk Koreń
2026-04-06 22:36:56 +02:00
parent 20a36703e6
commit 60f702fc73
8 changed files with 89 additions and 86 deletions

1
history/06-04-2026.json Normal file
View File

@@ -0,0 +1 @@
{"timestamp":1775507776136,"url":"https://www.youtube.com/watch?v=xTemcPZw8Eo","user":"kapitan.papryk"}

1
history/26-02-2026.json Normal file
View File

@@ -0,0 +1 @@
{"timestamp":1772133865649,"url":"https://www.youtube.com/watch?v=TWiOB4lUZrQ","user":"kapitan.papryk"}

View File

@@ -32,10 +32,10 @@ async function execute(interaction: ChatInputCommandInteraction<CacheType>) {
} }
await interaction.editReply(msg_downloading(url)); await interaction.editReply(msg_downloading(url));
await forceRequestSong(interaction, url) forceRequestSong(url, { user: interaction.user.username }).then(() => {
// const msg = getPlayMsg(url)
const msg = getPlayMsg(url) interaction.editReply(msg_play());
await interaction.editReply(msg_play()); })
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await interaction.editReply('Coś poszło nie tak :/'); await interaction.editReply('Coś poszło nie tak :/');

View File

@@ -36,10 +36,9 @@ async function execute(interaction: ChatInputCommandInteraction<CacheType>) {
} }
await interaction.editReply(msg_downloading(url)); await interaction.editReply(msg_downloading(url));
await requestSong(interaction, url) requestSong(url, { user: interaction.user.username }).then(() => {
interaction.editReply(msg_play());
const msg = getPlayMsg(url) })
await interaction.editReply(msg_play());
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await interaction.editReply('Coś poszło nie tak :/'); await interaction.editReply('Coś poszło nie tak :/');

4
src/global.d.ts vendored
View File

@@ -16,7 +16,7 @@ type Queue = {
} }
type HistoryObject = { type HistoryObject = {
interaction: ChatInputCommandInteraction<CacheType> user?: number | string,
url: string url?: string,
} }

View File

@@ -18,24 +18,25 @@ export function toggleLoop(): AudioFile | null {
} }
export async function requestSong( export async function requestSong(
interaction: ChatInputCommandInteraction<CacheType>,
url: string, url: string,
user?: any data: Partial<HistoryObject>
) { ) {
getAudioFile(url).then((path) => {
const path = await getAudioFile(url)
queue.songList.push({ path, url }) queue.songList.push({ path, url })
add_to_history({ interaction, url }) add_to_history({ url, user: data.user })
updatePlayer() updatePlayer()
})
} }
export async function forceRequestSong( export async function forceRequestSong(
interaction: ChatInputCommandInteraction<CacheType>, url: string,
url: string) { data: Partial<HistoryObject>
) {
const path = await getAudioFile(url) const path = await getAudioFile(url)
const audio = { path, url } const audio = { path, url }
queue.songList.push(audio) queue.songList.push(audio)
add_to_history({ url, user: data.user })
queue.current = audio queue.current = audio
playSong(player, path); playSong(player, path);
} }

View File

@@ -61,6 +61,7 @@ export async function connectToChannel(channel: VoiceBasedChannel) {
} }
export async function playSong(player: AudioPlayer, songUrl: string) { export async function playSong(player: AudioPlayer, songUrl: string) {
try {
/** /**
* Here we are creating an audio resource using a sample song freely available online * Here we are creating an audio resource using a sample song freely available online
* (see https://www.soundhelix.com/audio-examples) * (see https://www.soundhelix.com/audio-examples)
@@ -84,4 +85,7 @@ export async function playSong(player: AudioPlayer, songUrl: string) {
* state within 5 seconds, otherwise it will reject with an error. * state within 5 seconds, otherwise it will reject with an error.
*/ */
return entersState(player, AudioPlayerStatus.Playing, 5_000); return entersState(player, AudioPlayerStatus.Playing, 5_000);
} catch (e) {
console.error(e)
}
} }

View File

@@ -2,7 +2,8 @@ import path from "node:path";
import fs from "node:fs"; import fs from "node:fs";
import { HISTORY_DIR_PATH } from "../main"; import { HISTORY_DIR_PATH } from "../main";
export async function add_to_history(history: HistoryObject) { export async function add_to_history(data: HistoryObject) {
try {
const today = new Date(); const today = new Date();
const day = String(today.getDate()).padStart(2, '0'); const day = String(today.getDate()).padStart(2, '0');
@@ -16,18 +17,14 @@ export async function add_to_history(history: HistoryObject) {
fs.mkdirSync(HISTORY_DIR_PATH); fs.mkdirSync(HISTORY_DIR_PATH);
} }
let data = []; let history = {
timestamp: today.getTime(),
url: data.url,
user: data.user,
};
try { fs.writeFileSync(file_path, JSON.stringify(history));
const content = fs.readFileSync(file_path, 'utf8'); } catch (error) {
data = JSON.parse(content); console.log(error)
} catch (err) {
data = [];
} }
data.push(history)
fs.writeFileSync(file_path, JSON.stringify(data, (key, value) => {
return typeof value === "bigint" ? value.toString() : value
}, 2));
} }