hapless.dev
hapless.dev
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
i just wanted to bother with a last question, is there any way to clean up or refactor the code so its more concise and DRY?
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
// HACK: Proper refactor, this ugly af, `stateChange` already gives me access
// to previous states, so its redundant, but if not, loops until queue underflow
try {
const addedTrack = await Track.create(path);
TrackQueue.enqueue(addedTrack);
let currTrack = TrackQueue.peek();

if (player.state.status === AudioPlayerStatus.Idle) {
if (currTrack) {
player.play(currTrack.res);

await interaction.followUp({
ephemeral: false,
content: `Now playing... [**${currTrack.title}**](<${currTrack.path}>).`,
});
}
} else {
await interaction.followUp({
ephemeral: true,
content: `Added to queue... [**${addedTrack.title}**](<${addedTrack.path}>).`,
});
}

player.on("stateChange", async (prev) => {
const nextTrack = TrackQueue.next();

if (
prev.status === AudioPlayerStatus.Playing &&
player.state.status === AudioPlayerStatus.Idle
) {
if (nextTrack) {
TrackQueue.dequeue();
currTrack = TrackQueue.peek();
if (currTrack) {
player.play(currTrack.res);

await interaction.followUp({
ephemeral: false,
content: `Now playing... [**${currTrack.title}**](<${currTrack.path}>).`,
});
}
} else if (!nextTrack && TrackQueue.length() === 1) {
TrackQueue.dequeue();
}
}

if (
player.state.status === AudioPlayerStatus.Idle &&
TrackQueue.isEmpty()
) {
return;
}
});
} catch (err) {
console.log(err);

return await interaction.followUp({
ephemeral: true,
content:
"Something went wrong playing and maybe I crashed, but I'll be back... I hope so :anguished:",
});
}
// HACK: Proper refactor, this ugly af, `stateChange` already gives me access
// to previous states, so its redundant, but if not, loops until queue underflow
try {
const addedTrack = await Track.create(path);
TrackQueue.enqueue(addedTrack);
let currTrack = TrackQueue.peek();

if (player.state.status === AudioPlayerStatus.Idle) {
if (currTrack) {
player.play(currTrack.res);

await interaction.followUp({
ephemeral: false,
content: `Now playing... [**${currTrack.title}**](<${currTrack.path}>).`,
});
}
} else {
await interaction.followUp({
ephemeral: true,
content: `Added to queue... [**${addedTrack.title}**](<${addedTrack.path}>).`,
});
}

player.on("stateChange", async (prev) => {
const nextTrack = TrackQueue.next();

if (
prev.status === AudioPlayerStatus.Playing &&
player.state.status === AudioPlayerStatus.Idle
) {
if (nextTrack) {
TrackQueue.dequeue();
currTrack = TrackQueue.peek();
if (currTrack) {
player.play(currTrack.res);

await interaction.followUp({
ephemeral: false,
content: `Now playing... [**${currTrack.title}**](<${currTrack.path}>).`,
});
}
} else if (!nextTrack && TrackQueue.length() === 1) {
TrackQueue.dequeue();
}
}

if (
player.state.status === AudioPlayerStatus.Idle &&
TrackQueue.isEmpty()
) {
return;
}
});
} catch (err) {
console.log(err);

return await interaction.followUp({
ephemeral: true,
content:
"Something went wrong playing and maybe I crashed, but I'll be back... I hope so :anguished:",
});
}
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
i did it! although its not the most clean code and im sure its reduntand af, but couldnt figure out how to track the state without creating a loop that emptied the queue when changing state or plain out making a queue underflow, thanks for all the patience and sorry if i came out as lazy or frustratingly dumb, i was really tired since it was late here, thanks once again for the guidance this was my implementation
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
do you have an example snippet that i can check? when i access AudioPlayer.stateChange the property doesnt exist, and .on of course is available since is the class that creates the player but im farily lost and confused by now, im so sorry for all the hassle 😭
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
and whats the syntax of the stateChange? is with the player.on or is there another way?
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
i mean like, continue the queue
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
how can i do that? ive seen implementations with player.on('stateChange' but couldnt understand how to then swap the already played resource with one that is pending in the queue
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
thank you for noticing it!
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
oh, i have to remove it, came from the first blogs i was following that are by now fairly deprecated, my bad
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
i figured out that, but now i have the same issue, how can i track the state between resources?
try {
const addedTrack = await Track.create(url);
TrackQueue.enqueue(addedTrack);

while (!TrackQueue.isEmpty()) {
const currTrack = TrackQueue.peek();

if (player.state.status === AudioPlayerStatus.Playing) {
await interaction.followUp({
ephemeral: true,
content: `Added to the queue... **${addedTrack?.title}**<${addedTrack?.path}>`,
});
break;
}

if (player.state.status === AudioPlayerStatus.Idle) {
if (currTrack) {
TrackQueue.dequeue();
player.play(currTrack.res);
await entersState(player, AudioPlayerStatus.Playing, 5000);
await interaction.followUp({
ephemeral: true,
content: `Now playing... **${currTrack.title}**<${currTrack.path}>`,
});
}
}
}
try {
const addedTrack = await Track.create(url);
TrackQueue.enqueue(addedTrack);

while (!TrackQueue.isEmpty()) {
const currTrack = TrackQueue.peek();

if (player.state.status === AudioPlayerStatus.Playing) {
await interaction.followUp({
ephemeral: true,
content: `Added to the queue... **${addedTrack?.title}**<${addedTrack?.path}>`,
});
break;
}

if (player.state.status === AudioPlayerStatus.Idle) {
if (currTrack) {
TrackQueue.dequeue();
player.play(currTrack.res);
await entersState(player, AudioPlayerStatus.Playing, 5000);
await interaction.followUp({
ephemeral: true,
content: `Now playing... **${currTrack.title}**<${currTrack.path}>`,
});
}
}
}
this generates an "infinite" loop where the bot constantly sends follow ups to the original command request but also does the job of keep playing until the queue is empty, how can i either: a. keep track constantly of the state of the player and change it accordingly, or b. send only and only one interaction even on a loop? c all of the above? haha
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
how can i make if statements with player.status if AudioPlayerStatus has no overlap and cant call AudioPlayerState enums since are types and not values?
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/21/2023 in #djs-voice
how to set and track audio player states?
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2",
}
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2",
}
Node 18.16.0
21 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
10 months is old? haha wow, i thought it was kinda recent, this api and its ecosystem go fast af gotta admit
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
huh, it worked with the normal adapter
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
weird right? i saw the discordjs.guide and all, and copied the code by hand, reading the types and interfaces but couldnt get why it did it like that instead of using the normal creator, its a shame that actual .ts implementations are so scarce for the discord api
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
let me try it brb
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
i plan to refactor it but when i've used my own ready.ts to instatiate the client and parse my commands but when i call the playSong(), player instance and adapter that the repo calls on index.ts the functions never get triggered because they dont log anything
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
yes, all this is the index.ts and i just followed the repo which abstracts the VoiceAdapter to the function in adapter.ts in the repo, let me try it out!
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
const discord_token = process.env.DISCORD_TOKEN;

console.log("Bot is starting...");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

// TODO: Refactor the awaiting in ready.ts
//ready(client);

client.on(Events.ClientReady, async () => {
console.log("Discord.js client is ready!");

try {
await client.application?.commands.set(Commands);
await playSong();
console.log("Song is ready to play!");
} catch (error) {
console.error(error);
}
});

// TODO: Refactor and make play command
client.on("messageCreate", async (message) => {
if (!message.guild) return;

if (message.content === "-join") {
const channel = message.member?.voice.channel;

if (channel) {
try {
const connection = await connectToChannel(channel);

connection.subscribe(player);
await message.reply("Playing now!");
} catch (error) {
console.error(error);
}
} else {
void message.reply("Join a voice channel then try again!");
}
}
});

interactionCreate(client);

void client.login(discord_token);
const discord_token = process.env.DISCORD_TOKEN;

console.log("Bot is starting...");

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

// TODO: Refactor the awaiting in ready.ts
//ready(client);

client.on(Events.ClientReady, async () => {
console.log("Discord.js client is ready!");

try {
await client.application?.commands.set(Commands);
await playSong();
console.log("Song is ready to play!");
} catch (error) {
console.error(error);
}
});

// TODO: Refactor and make play command
client.on("messageCreate", async (message) => {
if (!message.guild) return;

if (message.content === "-join") {
const channel = message.member?.voice.channel;

if (channel) {
try {
const connection = await connectToChannel(channel);

connection.subscribe(player);
await message.reply("Playing now!");
} catch (error) {
console.error(error);
}
} else {
void message.reply("Join a voice channel then try again!");
}
}
});

interactionCreate(client);

void client.login(discord_token);
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2"
}
"dependencies": {
"@discordjs/core": "^0.6.0",
"@discordjs/opus": "^0.9.0",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.43",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"ffmpeg": "^0.0.4",
"libsodium-wrappers": "^0.7.11",
"nodemon": "^2.0.22",
"sodium": "^3.0.2"
}
11 replies