-
I have an error that is showing up in the terminal and I can't find a solution. I'll share the code, installed dependencies, and I would greatly appreciate it if someone could help me out. const { Client, Intents } = require('discord.js');
const config = require('./config.json');
const Discord = require('discord.js');
const ytdl = require('ytdl-core-discord');
const { YTSearcher } = require('ytsearcher');
const { prefix, token, youtubeAPIKey } = require('./config.json');
const intents = new Intents();
config.intents.forEach(intent => {
intents.add(intent);
});
const client = new Client({
intents: intents
});
const searcher = new YTSearcher(youtubeAPIKey);
const queue = new Map();
client.once('ready', () => {
console.log('El bot está en línea.');
});
client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'play') {
execute(message, args);
} else if (command === 'skip') {
skip(message);
} else if (command === 'stop') {
stop(message);
}
});
async function execute(message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.channel.send('Debes estar en un canal de voz para reproducir música.');
}
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
return message.channel.send('No tengo los permisos necesarios para unirme y reproducir música en ese canal de voz.');
}
const serverQueue = queue.get(message.guild.id);
let songInfo;
let song;
if (ytdl.validateURL(args[0])) {
try {
songInfo = await ytdl.getInfo(args[0]);
song = {
title: songInfo.videoDetails.title,
url: songInfo.videoDetails.video_url,
};
} catch (error) {
console.error(error);
return message.channel.send('Ocurrió un error al obtener la información del video.');
}
} else {
try {
const searchResult = await searcher.search(args.join(' '), { type: 'video' });
if (!searchResult.first) {
return message.channel.send('No se encontraron resultados de búsqueda.');
}
songInfo = await ytdl.getInfo(searchResult.first.url);
song = {
title: songInfo.videoDetails.title,
url: songInfo.videoDetails.video_url,
};
} catch (error) {
console.error(error);
return message.channel.send('Ocurrió un error al buscar el video en YouTube.');
}
}
if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true,
};
queue.set(message.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
const connection = await voiceChannel.join();
queueContruct.connection = connection;
play(message.guild, queueContruct.songs[0]);
} catch (error) {
console.error(error);
queue.delete(message.guild.id);
return message.channel.send('Ocurrió un error al unirse al canal de voz.');
}
} else {
serverQueue.songs.push(song);
return message.channel.send(`**${song.title}** ha sido añadida a la cola.`);
}
}
function skip(message) {
const serverQueue = queue.get(message.guild.id);
if (!message.member.voice.channel) {
return message.channel.send('Debes estar en un canal de voz para saltar la canción.');
}
if (!serverQueue) {
return message.channel.send('No hay canciones en la cola para saltar.');
}
serverQueue.connection.dispatcher.end();
}
function stop(message) {
const serverQueue = queue.get(message.guild.id);
if (!message.member.voice.channel) {
return message.channel.send('Debes estar en un canal de voz para detener la reproducción.');
}
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
async function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(await ytdl(song.url), { type: 'opus' })
.on('finish', () => {
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
})
.on('error', (error) => {
console.error(error);
});
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Reproduciendo: **${song.title}**`);
}
client.login(config.token); The dependencies {
"dependencies": {
"discord.js": "^14.11.0",
"node.js": "^0.0.1-security",
"ytdl-core-discord": "^1.3.1",
"ytsearcher": "^1.2.4"
}
} The error that appears is as follows.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
What's your discord.js version? |
Beta Was this translation helpful? Give feedback.
-
For v13 const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); For v14 import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Guilds] }); |
Beta Was this translation helpful? Give feedback.
Intents
is a class in v13 though. Their issue comes from having v14 installed which doesn’t have an Intents class anymore (and so your code would be just as invalid).