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?
hi there, im learning data structures and algorithms and i thought it would be a nice exercise using them in a discord bot that takes a local folder and creates a track instance and plays it according to an initialized DSA... the thing is, i need to keep track of the player status, but it seems that if (player.state === AudioPlayerStatus.__) have no overlap and it complains, ive seen some player.on(___, async () =>) implementations but looks confusing and verbose, is there any way to properly track and set player states to ensure the behaviour of a queue, stack or whatever?
try {
TrackQueue.enqueue(track);
const currTrack = TrackQueue.peek();

if (typeof currTrack !== "undefined") {
// player.state === 'idle', but it triggers anyway and returns this interaction
if (AudioPlayerStatus.Playing) { // Adds
return await interaction.followUp({
ephemeral: true,
content: `Added to the queue... ***${currTrack.title}***`,
});
}

player.play(currTrack.res);

await interaction.followUp({
ephemeral: true,
content: `Now playing... [${currTrack.title}](${currTrack.path})`,
});

return entersState(player, AudioPlayerStatus.Playing, 5000);
}
} catch (error) {
console.log(error);
return await interaction.followUp({
ephemeral: true,
content:
"Something went wrong playing and maybe I crashed, but I'll be back... I hope so :anguished:",
});
}
try {
TrackQueue.enqueue(track);
const currTrack = TrackQueue.peek();

if (typeof currTrack !== "undefined") {
// player.state === 'idle', but it triggers anyway and returns this interaction
if (AudioPlayerStatus.Playing) { // Adds
return await interaction.followUp({
ephemeral: true,
content: `Added to the queue... ***${currTrack.title}***`,
});
}

player.play(currTrack.res);

await interaction.followUp({
ephemeral: true,
content: `Now playing... [${currTrack.title}](${currTrack.path})`,
});

return entersState(player, AudioPlayerStatus.Playing, 5000);
}
} catch (error) {
console.log(error);
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/18/2023 in #djs-voice
bot joins to voice channel, doesn't play any sound, times out and disconnects
hi there! im following the basic example repo, although is a common error, i wonder, since i basically have the same code as the repo
const player = createAudioPlayer();

const playSong = () => {
const resource = createAudioResource(
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
{ inputType: StreamType.Arbitrary }
);
player.play(resource);

return entersState(player, AudioPlayerStatus.Playing, 5000);
};

// TODO: Refactor in its own file
const connectToChannel = async (channel: VoiceBasedChannel) => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: createDiscordJSAdapter(channel),
selfDeaf: false,
selfMute: false,
});

// FIX: It connects but the status is not correctly set so it never plays
// FIX: Times out instead of entering idle
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);

return connection;
} catch (err) {
connection.destroy();

throw err;
}
};
const player = createAudioPlayer();

const playSong = () => {
const resource = createAudioResource(
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
{ inputType: StreamType.Arbitrary }
);
player.play(resource);

return entersState(player, AudioPlayerStatus.Playing, 5000);
};

// TODO: Refactor in its own file
const connectToChannel = async (channel: VoiceBasedChannel) => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: createDiscordJSAdapter(channel),
selfDeaf: false,
selfMute: false,
});

// FIX: It connects but the status is not correctly set so it never plays
// FIX: Times out instead of entering idle
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);

return connection;
} catch (err) {
connection.destroy();

throw err;
}
};
11 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/16/2023 in #djs-voice
another bot not joining the voice channel
Hi there, im learning my way through the docs and making my first bot, i was following line by line the basic example repo from discord/voice (https://github.com/discordjs/voice-examples/blob/main/basic/src/main.ts) but its not working, its basically the same code minus the formatting and the difference in current Status comparison since it seems the deesctructuring of Constants for Events, Status is not longer used... the code logs is ready to play but when i type -join on the chat it doesnt respond at all, is it because of the intents?
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
});

//ready(client);

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

try {
await playSong();
console.log("Song is ready to play!");

} catch (error) {
console.error(error);
}
});

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!");
}
}
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
});

//ready(client);

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

try {
await playSong();
console.log("Song is ready to play!");

} catch (error) {
console.error(error);
}
});

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!");
}
}
});
these are my dependencies
"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"
}
8 replies
DIAdiscord.js - Imagine an app
Created by hapless.dev on 6/16/2023 in #djs-questions
good practices for project structure for a discord bot?
6 replies