Bot won't join voice channel [duplicate] - javascript

I recently installed Discord.js 13.1.0 and my music commands broke because, apparently, channel.join(); is not a function, although I have been using it for months on 12.5.3...
Does anybody know a fix for this?
Some parts of my join command:
const { channel } = message.member.voice;
const voiceChannel = message.member.voice.channel;
await channel.join();
It results in the error.

Discord.js no longer supports voice. You need to use the other package they made (#discordjs/voice). You can import joinVoiceChannel from there.
//discord.js and client declaration
const { joinVoiceChannel } = require('#discordjs/voice');
client.on('messageCreate', message => {
if(message.content === '!join') {
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
}
})

Related

Discord.js 12.0.0 msg.content returning blank

I'm making a stats checker bot in discord.js v12.0.0 because I don't want to deal with slash commands and intents, just wanted this to be a quick little project that I throw together. But, after coding for a while a command I made didn't work, and I decided to console log msg.content to see if that was the issue. It shows as completely blank when I log msg.content, as well as logging msg itself. NO, I am not running a self bot, I read that doing that can also give this issue.
Images:
Code:
import Discord from 'discord.js';
const client = new Discord.Client();
import fetch from 'node-fetch';
client.on('ready', () => {
console.log(`online`);
});
let lb;
async function fet() {
await fetch("https://login.deadshot.io/leaderboards").then((res) => {
return res.json();
}).then((res) => {
lb = res;
})
}
client.on('message', async msg => {
console.log(msg)
let channel = msg.channel.id;
if (msg.author.id == client.user.id) return;
if (channel != 1015992080897679370) return;
await fet();
let r;
for (var x in lb.all.kills) {
if (msg.content.toLowerCase() == lb.all.kills[x].name.toLowerCase()) {
r = lb.all.kills[x].name;
}
}
if (!r) return msg.channel.send('Error: user not found')
})
client.login(token)
Discord enforced the Message Content privileged intent, since September 1st.
Here's the announcement from Discord Developers server (invite)
Source message: Discord Developers #api-announcements
You can fix this, but you do need to use intents...
const client = new Discord.Client({
ws: {
intents: [Discord.Intents.ALL, 1 << 15]
}
})
You also need to flip the Message Content intent switch in developer portal
It's much better to update to version 13+, v12 is deprecated and is easily broken.

Play Audio when Joining a Voice Channel DiscordJS v13

I've been trying all night to get a simple task working, but yet I am not able to figure out how.
Basically what I achieved is that the Bot joins a Voice Channel when somebody joins, play an Audio file and that's it. What I want to achieve is that it joins when somebody joins, play the Audio file and then leave and as when somebody leaves the voice chat. It's supposed to join the voice chat, play a different audio file and then leave again.
All in all seems easy, but I've been banging my head against it with no results. If possible I would also like to have the code be tidy there aswell.
Here is the code (using discord.js v13):
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelId === null) {
console.log("Joined")
//playAudio(newState.channelId, './files/bruh.mp3')
const connection = joinVoiceChannel({
channelId: newState.channelId,
guildId: newState.guild.id,
adapterCreator: newState.guild.voiceAdapterCreator,
})
console.log(newState.guild.voiceAdapterCreator)
const player = createAudioPlayer();
connection.subscribe(player)
const resource = createAudioResource('./files/riff.mp3')
player.play(resource)
} else if (newState.channelId === null) {
console.log("Left")
}
});
Edith: With when somebody joins. I mean when somebody joins Voice Chat.
After a Good Nights sleep I was able to figure it out, for anyone corious, here is the code!
var isReady = true
function playAudio(chID, guID, adpID, audioFile) {
isReady = false
const connection = joinVoiceChannel({
channelId: chID,
guildId: guID,
adapterCreator: adpID,
})
const player = createAudioPlayer();
connection.subscribe(player)
const resource = createAudioResource(audioFile)
player.play(resource)
player.on(AudioPlayerStatus.Idle, () => {
connection.destroy()
});
isReady = true
}

How can i make it play music?

I tried making a rickroll command like this:
client.on('message', message => {
if (message.content.startsWith('music plz')) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply(`Wow what a scrub, join a Voice Channel first`);
}
voiceChannel.join()
.then(connection => {
const stream = ytdl('https://www.youtube.com/watch?v=dQw4w9WgXcQ', { filter: 'audioonly' });
const dispatcher = connection.playStream(stream, streamOptions);
})
It joins the VC and gives an error when not in VC but does not play music what am I doing wrong? I use Discord.js v12.5 and I am new to
it.
Check the guide Here they have a guide of how to do this. Also please update to v13, it's much better. Something like this:
const fs = require('fs');
const broadcast = client.voice.createBroadcast();
// From a path
broadcast.play('audio.mp3');
// From a ReadableStream
broadcast.play(fs.createReadStream('audio.mp3'));
// From a URL
broadcast.play('http://myserver.com/audio.aac');

Play local music files using djs v13

I know a lot of people already asked how to play music from youtube in discord voice channel, but I can't find anything about playing local files on djs version 13.2.0!
I tried using this code:
const { createReadStream } = require('fs');
const { join } = require('path');
const { createAudioResource, StreamType, createAudioPlayer, joinVoiceChannel } = require('#discordjs/voice');
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
message.guild.me.voice.setRequestToSpeak(true);
let resource = createAudioResource(join(../music/audio.mp3, 'audio.mp3'));
const player = createAudioPlayer();
player.play(resource);
When I try to eval() it - my bot joins the channel (stage channel) and says everything worked, but it's not playing anything! How can I make my bot play local music files in stage channel?
There are 2 problems here.
Firstly, the path is completely wrong. It is not a string and even if you try to change it to a string it will be invalid as the first argument ends with audio.mp3, and the second one is audio.mp3. Use this path instead:
let resource = createAudioResource(join('..', 'music', 'audio.mp3'));
Secondly, you are playing audio in the player, but not the voice connection. You must subscribe to the audio player.
This should be the final code:
const player = createAudioPlayer()
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
}).subscribe(player)
message.guild.me.voice.setRequestToSpeak(true);
let resource = createAudioResource(join('..', 'music', 'audio.mp3'));
player.play(resource)

Cannot read properties of undefined (reading 'join') Discord.js

im trying to create my discord music bot, but when i run it. It cannot join my voiceChannel, returning this error: channel_info.channelId.join is not a function. Below, my code:
const Discord = require('discord.js');
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const ytdl = require('ytdl-core');
const streamOptions = { seek: 0, volume: 1 };
const botToken = 'mytoken';
bot.login(botToken);
bot.on('ready', () => {
console.log('to olain');
});
bot.on('message', msg => {
if (msg.author.bot) {
return;
}
if (msg.content.toLowerCase().startsWith(';p')) {
const channel_info = msg.member.guild.voiceStates.cache.find(user => user.id == msg.author.id);
if (channel_info.channelId == null) {
return console.log('Canal não encontrado!');
}
console.log('Canal encontrado');
channel_info.channelId.join().then(connection => {
const stream = ytdl('https://www.youtube.com/watch?v=BxmMGnvvDCo', { filter: 'audioonly' });
const DJ = connection.playStream(stream, streamOptions);
DJ.on('end', end => {
channel_info.channelId.leave();
});
})
.catch(console.error);
}
});
There are several issues in this code. Even if we fix some of these issues, this code will still not work due to differences between discord.js v12 and v13. Let's get started.
Issue #1
This is not one of the core issues causing your code to not work, but it's something useful to consider. You are doing this to get the voice state of the message author:
msg.member.guild.voiceStates.cache.find(user => user.id == msg.author.id);
When you could easily do the exact same thing in a much shorter, less-likely-to-produce-errors way:
msg.member.voice;
Issue #2
Now this is a core issue causing your code to not work. In fact, it is causing the error in your question. You are trying to do:
channel_info.channelId.join();
That does not make sense. You are trying to join a channel ID? channelId is just a String of numbers like so: "719051328038633544". You can't "join" that String of numbers. You want to join the actual channel that the member is in, like so:
channel_info.channel.join();
Issue #3
Based on how you are using a channelId property instead of channelID, I assume you are on discord.js v13. Voice channels do not have a .join() method on v13; in fact, discord.js v13 has no support for joining or playing audio in voice channels. You must install the discord.js/voice package in order to join a voice channel in discord.js v13. This is critical. Even if you fix the above two issues, you must solve this third issue or your code will not work (unless you downgrade to discord.js v12).

Categories

Resources