diff --git a/history/06-04-2026.json b/history/06-04-2026.json new file mode 100644 index 0000000..d789a49 --- /dev/null +++ b/history/06-04-2026.json @@ -0,0 +1 @@ +{"timestamp":1775508648856,"url":"https://www.youtube.com/watch?v=xTemcPZw8Eo","user":"kapitan.papryk"} \ No newline at end of file diff --git a/history/26-02-2026.json b/history/26-02-2026.json new file mode 100644 index 0000000..dafe908 --- /dev/null +++ b/history/26-02-2026.json @@ -0,0 +1 @@ +{"timestamp":1772133865649,"url":"https://www.youtube.com/watch?v=TWiOB4lUZrQ","user":"kapitan.papryk"} \ No newline at end of file diff --git a/src/commands/forceplay.ts b/src/commands/forceplay.ts index 041db1b..0067912 100644 --- a/src/commands/forceplay.ts +++ b/src/commands/forceplay.ts @@ -32,10 +32,10 @@ async function execute(interaction: ChatInputCommandInteraction) { } await interaction.editReply(msg_downloading(url)); - await forceRequestSong(interaction, url) - - const msg = getPlayMsg(url) - await interaction.editReply(msg_play()); + forceRequestSong(url, { user: interaction.user.username }).then(() => { + // const msg = getPlayMsg(url) + interaction.editReply(msg_play()); + }) } catch (error) { console.error(error); await interaction.editReply('Coś poszło nie tak :/'); diff --git a/src/commands/play.ts b/src/commands/play.ts index f3a1785..1a2b803 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -36,10 +36,9 @@ async function execute(interaction: ChatInputCommandInteraction) { } await interaction.editReply(msg_downloading(url)); - await requestSong(interaction, url) - - const msg = getPlayMsg(url) - await interaction.editReply(msg_play()); + requestSong(url, { user: interaction.user.username }).then(() => { + interaction.editReply(msg_play()); + }) } catch (error) { console.error(error); await interaction.editReply('Coś poszło nie tak :/'); diff --git a/src/global.d.ts b/src/global.d.ts index 37160d4..3b92b23 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -16,7 +16,7 @@ type Queue = { } type HistoryObject = { - interaction: ChatInputCommandInteraction - url: string + user?: number | string, + url?: string, } diff --git a/src/playback.ts b/src/playback.ts index 6397ee5..8088aaf 100644 --- a/src/playback.ts +++ b/src/playback.ts @@ -18,24 +18,25 @@ export function toggleLoop(): AudioFile | null { } export async function requestSong( - interaction: ChatInputCommandInteraction, url: string, - user?: any + data: Partial ) { - - const path = await getAudioFile(url) - queue.songList.push({ path, url }) - add_to_history({ interaction, url }) - updatePlayer() + getAudioFile(url).then((path) => { + queue.songList.push({ path, url }) + add_to_history({ url, user: data.user }) + updatePlayer() + }) } export async function forceRequestSong( - interaction: ChatInputCommandInteraction, - url: string) { + url: string, + data: Partial +) { const path = await getAudioFile(url) const audio = { path, url } queue.songList.push(audio) + add_to_history({ url, user: data.user }) queue.current = audio playSong(player, path); } diff --git a/src/util/helpers.ts b/src/util/helpers.ts index cf2ca40..17da895 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -14,17 +14,17 @@ import { player } from '../main.js'; export async function connectToChannelByInteraction(interaction: ChatInputCommandInteraction): Promise { const member = await interaction.guild?.members.fetch(interaction.user.id); if (!member) throw new Error("ale chuj") - const voiceChannel = member.voice.channel; + const voiceChannel = member.voice.channel; if (!voiceChannel) throw new Error("ale chuj 2") - const connection = await connectToChannel(voiceChannel); + const connection = await connectToChannel(voiceChannel); connection.subscribe(player); } export async function connectToChannel(channel: VoiceBasedChannel) { /** - * Here, we try to establish a connection to a voice channel. If we're already connected - * to this voice channel, \@discordjs/voice will just return the existing connection for us! - */ + * Here, we try to establish a connection to a voice channel. If we're already connected + * to this voice channel, \@discordjs/voice will just return the existing connection for us! + */ const connection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, @@ -32,28 +32,28 @@ export async function connectToChannel(channel: VoiceBasedChannel) { }); /** - * If we're dealing with a connection that isn't yet Ready, we can set a reasonable - * time limit before giving up. In this example, we give the voice connection 30 seconds - * to enter the ready state before giving up. - */ + * If we're dealing with a connection that isn't yet Ready, we can set a reasonable + * time limit before giving up. In this example, we give the voice connection 30 seconds + * to enter the ready state before giving up. + */ try { /** - * Allow ourselves 30 seconds to join the voice channel. If we do not join within then, - * an error is thrown. - */ + * Allow ourselves 30 seconds to join the voice channel. If we do not join within then, + * an error is thrown. + */ await entersState(connection, VoiceConnectionStatus.Ready, 30_000); /** - * At this point, the voice connection is ready within 30 seconds! This means we can - * start playing audio in the voice channel. We return the connection so it can be - * used by the caller. - */ + * At this point, the voice connection is ready within 30 seconds! This means we can + * start playing audio in the voice channel. We return the connection so it can be + * used by the caller. + */ return connection; } catch (error) { /** - * At this point, the voice connection has not entered the Ready state. We should make - * sure to destroy it, and propagate the error by throwing it, so that the calling function - * is aware that we failed to connect to the channel. - */ + * At this point, the voice connection has not entered the Ready state. We should make + * sure to destroy it, and propagate the error by throwing it, so that the calling function + * is aware that we failed to connect to the channel. + */ connection.destroy(); throw error; @@ -61,27 +61,31 @@ export async function connectToChannel(channel: VoiceBasedChannel) { } export async function playSong(player: AudioPlayer, songUrl: string) { - /** - * Here we are creating an audio resource using a sample song freely available online - * (see https://www.soundhelix.com/audio-examples) - * - * We specify an arbitrary inputType. This means that we aren't too sure what the format of - * the input is, and that we'd like to have this converted into a format we can use. If we - * were using an Ogg or WebM source, then we could change this value. However, for now we - * will leave this as arbitrary. - */ - const resource = createAudioResource(songUrl, { inputType: StreamType.Arbitrary }); + try { + /** + * Here we are creating an audio resource using a sample song freely available online + * (see https://www.soundhelix.com/audio-examples) + * + * We specify an arbitrary inputType. This means that we aren't too sure what the format of + * the input is, and that we'd like to have this converted into a format we can use. If we + * were using an Ogg or WebM source, then we could change this value. However, for now we + * will leave this as arbitrary. + */ + const resource = createAudioResource(songUrl, { inputType: StreamType.Arbitrary }); - /** - * We will now play this to the audio player. By default, the audio player will not play until - * at least one voice connection is subscribed to it, so it is fine to attach our resource to the - * audio player this early. - */ - player.play(resource); + /** + * We will now play this to the audio player. By default, the audio player will not play until + * at least one voice connection is subscribed to it, so it is fine to attach our resource to the + * audio player this early. + */ + player.play(resource); - /** - * Here we are using a helper function. It will resolve if the player enters the Playing - * state within 5 seconds, otherwise it will reject with an error. - */ - return entersState(player, AudioPlayerStatus.Playing, 5_000); + /** + * Here we are using a helper function. It will resolve if the player enters the Playing + * state within 5 seconds, otherwise it will reject with an error. + */ + return entersState(player, AudioPlayerStatus.Playing, 5_000); + } catch (e) { + console.error(e) + } } diff --git a/src/util/history.ts b/src/util/history.ts index 6ec2326..27ac3f0 100644 --- a/src/util/history.ts +++ b/src/util/history.ts @@ -2,32 +2,29 @@ import path from "node:path"; import fs from "node:fs"; import { HISTORY_DIR_PATH } from "../main"; -export async function add_to_history(history: HistoryObject) { - const today = new Date(); - - const day = String(today.getDate()).padStart(2, '0'); - const month = String(today.getMonth() + 1).padStart(2, '0'); - const year = today.getFullYear(); - - const file_name = `${day}-${month}-${year}.json`; - const file_path = path.join(HISTORY_DIR_PATH, file_name) - - if (!fs.existsSync(HISTORY_DIR_PATH)) { - fs.mkdirSync(HISTORY_DIR_PATH); - } - - let data = []; - +export async function add_to_history(data: HistoryObject) { try { - const content = fs.readFileSync(file_path, 'utf8'); - data = JSON.parse(content); - } catch (err) { - data = []; + const today = new Date(); + + const day = String(today.getDate()).padStart(2, '0'); + const month = String(today.getMonth() + 1).padStart(2, '0'); + const year = today.getFullYear(); + + const file_name = `${day}-${month}-${year}.json`; + const file_path = path.join(HISTORY_DIR_PATH, file_name) + + if (!fs.existsSync(HISTORY_DIR_PATH)) { + fs.mkdirSync(HISTORY_DIR_PATH); + } + + let history = { + timestamp: today.getTime(), + url: data.url, + user: data.user, + }; + + fs.writeFileSync(file_path, JSON.stringify(history)); + } catch (error) { + console.log(error) } - - data.push(history) - - fs.writeFileSync(file_path, JSON.stringify(data, (key, value) => { - return typeof value === "bigint" ? value.toString() : value - }, 2)); }