使用不和谐按钮时出错:“类扩展值未定义不是构造函数或空值"

2022-01-10 00:00:00 javascript discord.js discord-buttons

I tried to make a button in Discord.js. However, when I wrote the code, I found that there was an error during startup.

I don't know why this error is happening. I checked many related questions online, but none of my problems were solved, and I even became even more confused.

This is my code:

const config = require("./config.json");
const Discord = require('discord.js');
const bot = new Discord.Client();
require('discord-buttons')(client);
const fs = require("fs");
const client = new Discord.Client({disableEveryone: false});
const moment = require("moment");
const { MessageButton, MessageActionRow } = require('discord-buttons')

client.commands = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {

  if(err) console.log(err);
  let jsfile = files.filter(f => f.split(".").pop() === "js");
  if(jsfile.length <= 0){
    console.log("XX");
    return;
  }
  jsfile.forEach((f, i) =>{
    let props = require(`./commands/${f}`);
    client.commands.set(props.help.name, props);
  });
});

    client.on('message', async message => {
      if(message.content === "buttonstest"){
      const button = new MessageButton()
      .setLabel("test")
      .setStyle("green")
      .setID("btn1")
      var embed = new Discord.RichEmbed()
  .setColor("#FFFFFF")
  .setTitle("test")
    
      message.channel.send(embed, button);
      }
    })
        
client.on("message", async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  let prefix = config.prefix;
  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = messageArray.slice(1);
  let commandfile = client.commands.get(cmd.slice(prefix.length));
  if(commandfile) commandfile.run(client,message,args);
});

client.login(config.token)

This is the error message:

I have no name!@f7808405-1373-45ee-bac7-0059f94bd574:~$ /home/container/node_modules/discord.js-buttons/src/Classes/APIMessage.js:5
class sendAPICallback extends APIMessage {
                              ^

TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/home/container/node_modules/discord.js-buttons/src/Classes/APIMessage.js:5:31)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/container/node_modules/discord.js-buttons/src/Classes/Message.js:3:20)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)

解决方案

In Discord.js v13, the API message class has been renamed to MessagePayload. APIMessage would therefore be undefined.

This error is occurring in the discord-buttons module. Discord.js v13 supports buttons so you do not need this module. See the Discord.js guide on buttons for more details.

To send a button, you could use this code:

client.on('message', async message => {
  if(message.content === "buttonstest"){
    const button = new Discord.MessageButton()
      .setLabel("test")
      .setStyle("SUCCESS")
      .setCustomId("btn1");
    // Note that RichEmbed was renamed to MessageEmbed in v12
    const embed = new Discord.MessageEmbed()
      .setColor("#FFFFFF")
      .setTitle("test");
    message.channel.send({
      embeds: [embed],
      components: [{components: [button]}]
      // alternatively
      // components: [new Discord.MessageActionRow([button])] 
    });
  }
});

You should also take a look at the Discord.js v13 upgrade guide.

相关文章