I'm making a discord bot using discord.js v14.9.0. Actually, Im working on a combat command.
This is my code:
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButtonComponent, Discord } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('combate')
.setDescription('Empieza un combate contra un slime.'),
async execute(interaction) {
// Variables del usuario
let usuario_vida = 100;
let usuario_ataque = 0;
// Variables del slime
let slime_vida = 50;
let slime_ataque = 0;
// Función para lanzar el dado
function rollDice(dado) {
return Math.floor(Math.random() * dado) + 1;
}
// Función para mostrar el resultado del combate
function mostrarResultado(ataque, defensa, ataque_roll, defensa_roll) {
let resultado = "";
resultado += `El ${ataque} ha atacado y ha hecho ${ataque_roll} puntos de daño.\n`;
resultado += `La ${defensa} ha recibido ${ataque_roll} puntos de daño y ahora tiene ${defensa_roll} puntos de vida.`;
return resultado;
}
function mostrarTablaVidas(vida_usuario, vida_slime) {
let tabla = "```";
tabla += `Slime: ${vida_slime} puntos de vida\n`;
tabla += "- - -\n- - -\n- - -\n- - -\n- - -\n";
tabla += `Usuario: ${vida_usuario} puntos de vida\n` + "```";
return tabla;
}
// Función para actualizar la tabla de vidas
async function actualizarTablaVidas(interaction, vida_usuario, vida_slime) {
const tabla = mostrarTablaVidas(vida_usuario, vida_slime);
const row = new MessageActionRow()
.addComponents(
new MessageButtonComponent()
.setCustomId('ataque')
.setLabel('Atacar')
.setColor('PRIMARY')
);
await interaction.editReply({ content: tabla, components: [row] });
}
// Mostrar la tabla de vidas y el botón de ataque
await actualizarTablaVidas(interaction, usuario_vida, slime_vida);
// Registrar la función que se llamará cuando se haga clic en el botón de ataque
const filter = i => i.customId === 'ataque' && i.user.id === interaction.user.id;
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });
collector.on('interactionCreate', async (buttonInteraction) => {
if (buttonInteraction.isButton() && buttonInteraction.customId === 'ataque') {
// El usuario ataca al slime
const ataque_roll = rollDice(6);
slime_vida -= ataque_roll;
const resultado_usuario = mostrarResultado("usuario", "slime", ataque_roll, slime_vida);
// El slime ataca al usuario
const defensa_roll = rollDice(4);
usuario_vida -= defensa_roll;
const resultado_slime = mostrarResultado("slime", "usuario", defensa_roll, usuario_vida);
// Actualizar la tabla de vidas
await actualizarTablaVidas(buttonInteraction, usuario_vida, slime_vida);
// Mostrar el resultado del ataque
await buttonInteraction.followUp({ content: resultado_usuario + "\n" + resultado_slime });
// Comprobar si el combate ha terminado
if (usuario_vida <= 0) {
await buttonInteraction.followUp({ content: "Has perdido el combate." });
collector.stop();
} else if (slime_vida <= 0) {
await buttonInteraction.followUp({ content: "Has ganado el combate." });
collector.stop();
}
}
});
// Tiempo máximo de espera para el botón de ataque
collector.on('end', async () => {
await interaction.editReply({ content: "El combate ha terminado." });
});
},
};
And this is the error that keeps appearing in the console everythime I try to use de command:
TypeError: MessageActionRow is not a constructor at actualizarTablaVidas (C:\Users\usago\OneDrive\Escritorio\RpgBot\commands\combate.js:43:25) at Object.execute (C:\Users\usago\OneDrive\Escritorio\RpgBot\commands\combate.js:54:15) at Client. (C:\Users\usago\OneDrive\Escritorio\RpgBot\index.js:33:17) at Client.emit (node:events:513:28) at InteractionCreateAction.handle (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12) at module.exports [as INTERACTION_CREATE] (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31) at WebSocketShard.onPacket (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22) at WebSocketShard.onMessage (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10) at callListener (C:\Users\usago\OneDrive\Escritorio\RpgBot\node_modules\ws\lib\event-target.js:290:14)
No matter what I change, everytame it ends whit this error apearing in the console.
What is suposed to hapen when I use the command is that the table appears WITH the buttons for the user to atack.
You no longer use MessageActionRow
or MessageButtonComponent
in Discord.js. Replace them with ActionRowBuilder
and ButtonBuilder
.