Working on discord.js v14.9.0, an error TypeError: MessageActionRow is not a constructor keeps apearing

huangapple go评论109阅读模式
英文:

Working on discord.js v14.9.0, an error TypeError: MessageActionRow is not a constructor keeps apearing

问题

I see that you're encountering an error related to the MessageActionRow constructor not being recognized. This might be due to changes or updates in the discord.js library. Here's the translation of your code with potential corrections:

  1. const { SlashCommandBuilder } = require('@discordjs/builders');
  2. const { MessageActionRow, MessageButton, Client } = require('discord.js');
  3. module.exports = {
  4. data: new SlashCommandBuilder()
  5. .setName('combate')
  6. .setDescription('Empieza un combate contra un slime.'),
  7. async execute(interaction) {
  8. // Variables del usuario
  9. let usuario_vida = 100;
  10. let usuario_ataque = 0;
  11. // Variables del slime
  12. let slime_vida = 50;
  13. let slime_ataque = 0;
  14. // Función para lanzar el dado
  15. function rollDice(dado) {
  16. return Math.floor(Math.random() * dado) + 1;
  17. }
  18. // Función para mostrar el resultado del combate
  19. function mostrarResultado(ataque, defensa, ataque_roll, defensa_roll) {
  20. let resultado = "";
  21. resultado += `El ${ataque} ha atacado y ha hecho ${ataque_roll} puntos de daño.\n`;
  22. resultado += `La ${defensa} ha recibido ${ataque_roll} puntos de daño y ahora tiene ${defensa_roll} puntos de vida.`;
  23. return resultado;
  24. }
  25. function mostrarTablaVidas(vida_usuario, vida_slime) {
  26. let tabla = "```";
  27. tabla += `Slime: ${vida_slime} puntos de vida\n`;
  28. tabla += "- - -\n- - -\n- - -\n- - -\n- - -\n`;
  29. tabla += `Usuario: ${vida_usuario} puntos de vida\n` + "```";
  30. return tabla;
  31. }
  32. // Función para actualizar la tabla de vidas
  33. async function actualizarTablaVidas(interaction, vida_usuario, vida_slime) {
  34. const tabla = mostrarTablaVidas(vida_usuario, vida_slime);
  35. const row = new MessageActionRow()
  36. .addComponents(
  37. new MessageButton()
  38. .setCustomId('ataque')
  39. .setLabel('Atacar')
  40. .setColor('PRIMARY')
  41. );
  42. await interaction.editReply({ content: tabla, components: [row] });
  43. }
  44. // Mostrar la tabla de vidas y el botón de ataque
  45. await actualizarTablaVidas(interaction, usuario_vida, slime_vida);
  46. // Registrar la función que se llamará cuando se haga clic en el botón de ataque
  47. const filter = i => i.customId === 'ataque' && i.user.id === interaction.user.id;
  48. const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });
  49. // Tiempo máximo de espera para el botón de ataque
  50. collector.on('end', async () => {
  51. await interaction.editReply({ content: "El combate ha terminado." });
  52. });
  53. collector.on('interactionCreate', async (buttonInteraction) => {
  54. if (buttonInteraction.isButton() && buttonInteraction.customId === 'ataque') {
  55. // El usuario ataca al slime
  56. const ataque_roll = rollDice(6);
  57. slime_vida -= ataque_roll;
  58. const resultado_usuario = mostrarResultado("usuario", "slime", ataque_roll, slime_vida);
  59. // El slime ataca al usuario
  60. const defensa_roll = rollDice(4);
  61. usuario_vida -= defensa_roll;
  62. const resultado_slime = mostrarResultado("slime", "usuario", defensa_roll, usuario_vida);
  63. // Actualizar la tabla de vidas
  64. await actualizarTablaVidas(buttonInteraction, usuario_vida, slime_vida);
  65. // Mostrar el resultado del ataque
  66. await buttonInteraction.followUp({ content: resultado_usuario + "\n" + resultado_slime });
  67. // Comprobar si el combate ha terminado
  68. if (usuario_vida <= 0) {
  69. await buttonInteraction.followUp({ content: "Has perdido el combate." });
  70. collector.stop();
  71. } else if (slime_vida <= 0) {
  72. await buttonInteraction.followUp({ content: "Has ganado el combate." });
  73. collector.stop();
  74. }
  75. }
  76. });
  77. },
  78. };

I've replaced MessageButtonComponent with MessageButton and MessageActionRow remains the same. This should help resolve the issue you're facing.

英文:

I'm making a discord bot using discord.js v14.9.0. Actually, Im working on a combat command.

This is my code:

  1. const { SlashCommandBuilder } = require(&#39;@discordjs/builders&#39;);
  2. const { MessageActionRow, MessageButtonComponent, Discord } = require(&#39;discord.js&#39;);
  3. module.exports = {
  4. data: new SlashCommandBuilder()
  5. .setName(&#39;combate&#39;)
  6. .setDescription(&#39;Empieza un combate contra un slime.&#39;),
  7. async execute(interaction) {
  8. // Variables del usuario
  9. let usuario_vida = 100;
  10. let usuario_ataque = 0;
  11. // Variables del slime
  12. let slime_vida = 50;
  13. let slime_ataque = 0;
  14. // Funci&#243;n para lanzar el dado
  15. function rollDice(dado) {
  16. return Math.floor(Math.random() * dado) + 1;
  17. }
  18. // Funci&#243;n para mostrar el resultado del combate
  19. function mostrarResultado(ataque, defensa, ataque_roll, defensa_roll) {
  20. let resultado = &quot;&quot;;
  21. resultado += `El ${ataque} ha atacado y ha hecho ${ataque_roll} puntos de da&#241;o.\n`;
  22. resultado += `La ${defensa} ha recibido ${ataque_roll} puntos de da&#241;o y ahora tiene ${defensa_roll} puntos de vida.`;
  23. return resultado;
  24. }
  25. function mostrarTablaVidas(vida_usuario, vida_slime) {
  26. let tabla = &quot;```&quot;;
  27. tabla += `Slime: ${vida_slime} puntos de vida\n`;
  28. tabla += &quot;- - -\n- - -\n- - -\n- - -\n- - -\n&quot;;
  29. tabla += `Usuario: ${vida_usuario} puntos de vida\n` + &quot;```&quot;;
  30. return tabla;
  31. }
  32. // Funci&#243;n para actualizar la tabla de vidas
  33. async function actualizarTablaVidas(interaction, vida_usuario, vida_slime) {
  34. const tabla = mostrarTablaVidas(vida_usuario, vida_slime);
  35. const row = new MessageActionRow()
  36. .addComponents(
  37. new MessageButtonComponent()
  38. .setCustomId(&#39;ataque&#39;)
  39. .setLabel(&#39;Atacar&#39;)
  40. .setColor(&#39;PRIMARY&#39;)
  41. );
  42. await interaction.editReply({ content: tabla, components: [row] });
  43. }
  44. // Mostrar la tabla de vidas y el bot&#243;n de ataque
  45. await actualizarTablaVidas(interaction, usuario_vida, slime_vida);
  46. // Registrar la funci&#243;n que se llamar&#225; cuando se haga clic en el bot&#243;n de ataque
  47. const filter = i =&gt; i.customId === &#39;ataque&#39; &amp;&amp; i.user.id === interaction.user.id;
  48. const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });
  49. collector.on(&#39;interactionCreate&#39;, async (buttonInteraction) =&gt; {
  50. if (buttonInteraction.isButton() &amp;&amp; buttonInteraction.customId === &#39;ataque&#39;) {
  51. // El usuario ataca al slime
  52. const ataque_roll = rollDice(6);
  53. slime_vida -= ataque_roll;
  54. const resultado_usuario = mostrarResultado(&quot;usuario&quot;, &quot;slime&quot;, ataque_roll, slime_vida);
  55. // El slime ataca al usuario
  56. const defensa_roll = rollDice(4);
  57. usuario_vida -= defensa_roll;
  58. const resultado_slime = mostrarResultado(&quot;slime&quot;, &quot;usuario&quot;, defensa_roll, usuario_vida);
  59. // Actualizar la tabla de vidas
  60. await actualizarTablaVidas(buttonInteraction, usuario_vida, slime_vida);
  61. // Mostrar el resultado del ataque
  62. await buttonInteraction.followUp({ content: resultado_usuario + &quot;\n&quot; + resultado_slime });
  63. // Comprobar si el combate ha terminado
  64. if (usuario_vida &lt;= 0) {
  65. await buttonInteraction.followUp({ content: &quot;Has perdido el combate.&quot; });
  66. collector.stop();
  67. } else if (slime_vida &lt;= 0) {
  68. await buttonInteraction.followUp({ content: &quot;Has ganado el combate.&quot; });
  69. collector.stop();
  70. }
  71. }
  72. });
  73. // Tiempo m&#225;ximo de espera para el bot&#243;n de ataque
  74. collector.on(&#39;end&#39;, async () =&gt; {
  75. await interaction.editReply({ content: &quot;El combate ha terminado.&quot; });
  76. });
  77. },
  78. };

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.<anonymous> (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.

答案1

得分: 1

你不再使用MessageActionRowMessageButtonComponent在Discord.js中。请将它们替换为ActionRowBuilderButtonBuilder

英文:

You no longer use MessageActionRow or MessageButtonComponent in Discord.js. Replace them with ActionRowBuilder and ButtonBuilder.

huangapple
  • 本文由 发表于 2023年4月13日 20:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定