got stuck here, the bot seems to do exactly what I want but it does not produce any sound, it shows the song but just no sound. I put GatewayIntentBits.GuildVoiceStates in the Client instance, I also did selfMute : false
.
export async function execute(interaction: CommandInteraction) {
const player = useMainPlayer();
player.extractors.register(YouTubeExtractor, {});
player.extractors.register(SpotifyExtractor, {});
if (!interaction.guildId || !interaction.member) {
await interaction.reply("You must be in a guild to use this command");
return;
}
const guildId = interaction.guildId;
const member = interaction.member as GuildMember;
const voiceChannel = member.voice.channel;
if (!voiceChannel) {
await interaction.reply(
"You must be in a voice channel to use this command."
);
return;
}
const options = interaction.options as CommandInteractionOptionResolver;
const subcommand = options.getSubcommand();
const query = options.getString("url") || options.getString("searchterms");
if (!query) {
await interaction.reply("You must provide a query.");
return;
}
const adapterCreator = interaction.guild
?.voiceAdapterCreator as DiscordGatewayAdapterCreator;
if (!adapterCreator) {
await interaction.reply("Failed to get the voice adapter creator.");
return;
}
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: guildId,
adapterCreator: adapterCreator,
});
const audioPlayer = createAudioPlayer();
connection.subscribe(audioPlayer);
connection.on(VoiceConnectionStatus.Ready, () => {
console.log(`The Bot has connected to the channel`);
});
connection.on(VoiceConnectionStatus.Disconnected, async () => {
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
} catch (error) {
connection.destroy();
}
});
const queue =
player.nodes.get(guildId) ||
player.nodes.create(guildId, {
metadata: {
channel: interaction.channel,
},
});
if (!queue.connection) {
await queue.connect(voiceChannel);
return;
}
await interaction.reply(`Searching for ${query}`);
try {
let result;
if (subcommand == "song" || subcommand == "search") {
result = await player.search(query, {
requestedBy: member,
searchEngine: QueryType.AUTO,
});
} else if (subcommand == "playlist" || result) {
result = await player.search(query, {
requestedBy: member,
searchEngine: QueryType.YOUTUBE_PLAYLIST,
});
}
console.log("Search result: ", result);
if (!result || !result.tracks.length) {
await interaction.editReply("No results found.");
return;
}
const track = result.tracks[0];
if (track) {
const stream = ytdl(track.url, {
filter: "audioonly",
highWaterMark: 1 << 25,
});
const resource = createAudioResource(stream);
audioPlayer.play(resource);
audioPlayer.on(AudioPlayerStatus.Playing, () => {
console.log(`Audio Player started playing...`);
});
audioPlayer.on(AudioPlayerStatus.Idle, () => {
console.log(`Audio Player is idle...`);
});
audioPlayer.on("error", (error) => {
console.error(
`Error: ${error.message} with resource ${error.resource.metadata}`
);
interaction.followUp({
content: "Error occured during playback",
ephemeral: true,
});
});
await interaction.editReply(`Now playing : ${track.title}`);
}
} catch (error) {
console.error("Error during search and playback: ", error);
await interaction.editReply(
"An error occurred while trying to play the track."
);
}
}
I gave it the proper permissions (CONNECT,SPEAK) still no sound and
here’s also my client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
installed dependencies
"@discord-player/extractor": "^4.4.7",
"@discordjs/voice": "^0.17.0",
"discord-player": "^6.6.8",
"discord.js": "^14.15.2",
"ytdl-core": "^4.11.5"
"opusscript": "^0.0.8",
"ffmpeg-static": "^5.2.0",
New contributor
Brent V is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.