I am trying to build a discord music playing bot. For reference purposes I am on
I am using an event driven design system to know when to load a new video however the bot is not joining the voice channel but no error messages are being given. Here is where I try to join the voice channel
events.on(eventNames.PlayNextEvent, async () => {
logger.logInfo(eventNames.PlayNextEvent);
const item = await queue.next();
// establish voice channel connection
let voiceConnection = getVoiceConnection(item.guildId);
if (!voiceConnection) {
logger.logInfo(`creating voice connection in guild ${item.guildId} in channel ${item.voiceChannelId}...`);
let guild = client.guilds.cache.get(item.guildId);
voiceConnection = joinVoiceChannel({
channelId: item.voiceChannelId,
guild: item.guildId,
adapterCreator: guild.voiceAdapterCreator
});
}
const stream = await audioPlayer.buildStream(item);
if (!stream) {
logger.logError('stream is null');
return;
}
const resource = createAudioResource(stream.stream, {
inputType: stream.type
});
let discordPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Stop
}
});
discordPlayer.play(resource);
voiceConnection.subscribe(discordPlayer);
logger.logInfo(`playing "${item.title}"`);
});
I have verified that the event is being called and all the log messages I have in there are being printed. I have also tried having the voice connection and the player be global variables as well but that did not work. I have also confirmed that the guilId
and voiceChannelId
i am providing to joinVoiceChannel
were correct and guild.voiceAdapterCreator
is not null. Additionally I have initialized my client with the following intents
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
]
});
For additional reference, here is buildStream
exports.buildStream = async (videoInfo) => {
try {
return await play.stream(videoInfo.info.url);
} catch (err) {
logger.logError('error bulding stream');
logger.logError(err);
return null;
}
};
and const item = await queue.next()
returns an item from a queue in mongodb which i confirmed is working and saving all the info i need correctly
Just in case anyone else has this problem, I have the guild id variable name in joinVoiceChannel wrong. I just had guild when it should have been guildId.
voiceConnection = joinVoiceChannel({
channelId: item.voiceChannelId,
guildId: item.guildId,
adapterCreator: guild.voiceAdapterCreator
});