Problems in developing discord bot
Hello everyone, I'm new here , sorry, if i do something wrong. I'm trying to develop my own bot, I'm trying and wondering how to do it. For now I’m trying to make a discord bot using the guides. At the moment, I have already done so that the bot can respond to any messages. Then make a command similar to !ping so that the pong bot responds, thereby showing that the bot is active and working. And the !flip command, for playing a coin, drops out with varying probability 3 parts heads, tails, and edge.
Now I need help, and I wanted to ask where I should write regarding the development of a discord bot?. Well, okay.... while I’m writing here, the problem is I want to create my own bot (either a new one or implement it into an existing one), so that when writing !play [URL youtube_video] the bot will go into the voice chat and turn on music or otherwise the sound from the video, The problem is that I kind of wrote the code, but it doesn't work.
I checked the status of discord.js v13.1.0, and ytdl-core 4.9.1, the bot token is active and set correctly, and the bot has admin rights, and commands work such as !ping.
14 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by staff6)my code:
This is my code:
const { Client, Intents } = require('discord.js');
const ytdl = require('ytdl-core');
const client = new Client({Intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
const prefix = '!';
let isPlaying = false;
let connection;
client.on('ready', () => {
console.log(
Bot ${client.user.tag} is ready!);
});
client.on('messageCreate', async (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'play') {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.channel.send('You must be in a voice channel to play music!');
}
const url = args[0];
if (!url) {
return message.channel.send('You must provide a YouTube video URL!');
}
if (!ytdl.validateURL(url)) {
return message.channel.send('Invalid YouTube video URL!');
}
try {
if (!isPlaying) {
connection = await voiceChannel.join();
}
const stream = ytdl(url, { filter: 'audioonly' });
const dispatcher = connection.play(stream);
dispatcher.on('finish', () => {
if (isPlaying) {
voiceChannel.leave();
isPlaying = false;
}
});
isPlaying = true;
message.channel.send('Playing music from YouTube!');
} catch (error) {
console.error(error);
message.channel.send('An error occurred while playing music!');
}
}
if (command === 'stop') {
if (isPlaying) {
connection.disconnect();
isPlaying = false;
message.channel.send('Music stopped and the bot left the voice channel!');
} else {
message.channel.send('The bot is not playing any music!');
}
}
});
client.login(env.token);
*env.token -this another file where, i put token of my bot
5)error of discord.js:
PS D:\bots\toast> node musicbot.js
D:\bots\toast\node_modules\discord.js\src\client\Client.js:548
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (D:\bots\toast\node_modules\discord.js\src\client\Client.js:548:13)
at new Client (D:\bots\toast\node_modules\discord.js\src\client\Client.js:76:10)
at Object.<anonymous> (D:\bots\toast\musicbot.js:4:16)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
Node.js v18.18.1
1)PS D:\bots\toast> node -v
v18.18.1
PS D:\bots\toast> npm list discord.js
[email protected] D:\bots\toast
└── [email protected]
it should be
intents
not Intents
in your client constructorwell, ... i correct it
const { Client, intents } = require('discord.js');
const ytdl = require('ytdl-core');
const client = new Client({intents: [intents.flags.GUILDS, intents.flags.GUILD_MESSAGES]});
const prefix = '!';
let isPlaying = false;
let connection;
Still have a problem..
PS D:\bots\toast> node musicbot.js
D:\bots\toast\musicbot.js:4
const client = new Client({intents: [intents.GUILDS, intents.GUILD_MESSAGES]});
^
TypeError: Cannot read properties of undefined (reading 'GUILDS')
at Object.<anonymous> (D:\bots\toast\musicbot.js:4:46)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
Node.js v18.18.1
now you made it worse, actually
just change Intents to intents here in the first word inside the client options, not the class itself
like that?
const { Client, Intents } = require('discord.js');
const ytdl = require('ytdl-core');
const client = new Client({intents: [Intents.flags.GUILDS, Intents.flags.GUILD_MESSAGES]});
const prefix = '!';
sorry if I'm dumb, I'm just confused
welp...
D:\bots\toast\musicbot.js:4
const client = new Client({intents: [Intents.flags.GUILDS, Intents.flags.GUILD_MESSAGES]});
^
TypeError: Cannot read properties of undefined (reading 'GUILDS')
at Object.<anonymous> (D:\bots\toast\musicbot.js:4:52)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
literally take that code and change what I told you to change
you are changing stuff that I didn't even tell you to change
it's
Intents.FLAGS.GUILDS
like that,
const { Client, Intents } = require('discord.js');
const ytdl = require('ytdl-core');
const client = new Client({intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
lets try.
okay thank code has no errors... but..
results:
PS D:\bots\toast> node musicbot.js
Bot mybot#3956 is ready!
im sorry, my bot doesn't join voice and don't just saying in chat "You must be in a voice channel to play music!".
I understand that this is no longer a discord.js problem. But...
Can you tell me what the problem is and where I can turn for help, in the discord developers group?you are missing the required intents
GUILD_VOICE_STATES and GUILD_MEMBERS
well.. i paste but then i try to run it..
TypeError: voiceChannel.join is not a function
at Client.<anonymous> (D:\bots\toast\musicbot.js:36:41)
at Client.emit (node:events:517:28)
at MessageCreateAction.handle (D:\bots\toast\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
at module.exports [as MESSAGE_CREATE] (D:\bots\toast\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (D:\bots\toast\node_modules\discord.js\src\client\websocket\WebSocketManager.js:346:31)
at WebSocketShard.onPacket (D:\bots\toast\node_modules\discord.js\src\client\websocket\WebSocketShard.js:493:22)
at WebSocketShard.onMessage (D:\bots\toast\node_modules\discord.js\src\client\websocket\WebSocketShard.js:327:10)
at callListener (D:\bots\toast\node_modules\ws\lib\event-target.js:290:14)
at WebSocket.onMessage (D:\bots\toast\node_modules\ws\lib\event-target.js:209:9)
at WebSocket.emit (node:events:517:28)
and bot says: "An error occurred while playing music!"also, ytdl/playing music from YouTube is against TOS
everything about voice stuff drastically changed
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
okay, thank gonna learning more about it, i think problem solved for now, about voice im try do myself