简体   繁体   English

Discord.js 机器人的代码响应错误“TypeError:”

[英]Code for Discord.js bot responds with error "TypeError:"

I'm testing sending embed messages for a Discord Bot using Discord.js which is basically a node.js module used to interact with Discord's API. This is the code i'm using for the bot to send the embed message:我正在测试使用Discord.js为 Discord Bot 发送嵌入消息,它基本上是一个node.js模块,用于与 Discord 的 API 交互。这是我用于机器人发送嵌入消息的代码:

const Discord = require('discord.js');
const embed = new Discord.RichEmbed()
    .setTitle("This is your title, it can hold 256 characters")
    .setAuthor("Author Name", "https://i.imgur.com/lm8s41J.png")
    .setColor(0x00AE86)
    .setDescription("This is the main body of text, it can hold 2048 characters.")
    .setFooter("This is the footer text, it can hold 2048 characters", "http://i.imgur.com/w1vhFSR.png")
    .setImage("http://i.imgur.com/yVpymuV.png")
    .setThumbnail("http://i.imgur.com/p2qNFag.png")
    .setTimestamp()
    .setURL("https://discord.js.org/#/docs/main/indev/class/RichEmbed")
    .addField("This is a field title, it can hold 256 characters",
        "This is a field value, it can hold 1024 characters.")
    .addField("Inline Field", "They can also be inline.", true)
    .addBlankField(true)
    .addField("Inline Field 3", "You can have a maximum of 25 fields.", true);

  message.channel.send({embed});

When I run the code I get this error in the Visual Studio Code IDE:当我运行代码时,我在 Visual Studio Code IDE 中收到此错误:

TypeError: (intermediate value).setTitle(...).setAuthor(...).setColor(...).setDescription(...).setFooter(...).setImage(...).setThumbnail(...).setTimestamp(...).setURL(...).addField(...).addField(...).addBlankField is not a function TypeError: (中间值).setTitle(...).setAuthor(...).setColor(...).setDescription(...).setFooter(...).setImage(...).setThumbnail (...).setTimestamp(...).setURL(...).addField(...).addField(...).addBlankField 不是 function

There is not addBlankField() function in MessageEmbed class when you look at documentation , check your discord.js version.当您查看文档时, MessageEmbed类中没有addBlankField()函数,请检查您的discord.js版本。

As of v12.0.0 they changed RichEmbed to MessageEmbed .从 v12.0.0 开始,他们将RichEmbed更改为MessageEmbed

If you want to add a blank field in another way, because .addBlankField() isn't supported in discord.js v12.如果您想以其他方式添加空白字段,因为.addBlankField() v12 不支持.addBlankField() You can write this:你可以这样写:

.addField("** **", "** **")

This will add a field with an almost empty title an description (They are containing one space after you sent the embed)这将添加一个标题几乎为空的字段和描述(发送嵌入后它们包含一个空格)

Try this :试试这个 :

    const Discord = require('discord.js');
    const client = new Discord.Client();
    client.login('Your bot\'s token here');
    client.on('ready', () => console.log('I\'m ready !');
    client.on('message', message => {
        const embed = new Discord.MessageEmbed()
            .setTitle("This is your title, it can hold 256 characters")
            .setAuthor("Author Name", "https://i.imgur.com/lm8s41J.png")
            .setColor('WHITE')
            .setDescription("This is the main body of text, it can hold 2048 characters.")
            .setFooter("This is the footer text, it can hold 2048 characters", "http://i.imgur.com/w1vhFSR.png")
            .setImage("http://i.imgur.com/yVpymuV.png")
            .setThumbnail("http://i.imgur.com/p2qNFag.png")
            .setTimestamp()
            .setURL("https://discord.js.org/#/docs/main/indev/class/RichEmbed")
            .addField("This is a field title, it can hold 256 characters",
        "This is a field value, it can hold 1024 characters.")
            .addField("Inline Field", "They can also be inline.", true)
            .addField("Inline Field 3", "You can have a maximum of 25 fields.", true);

  message.channel.send(embed);

If that doesn't work, I would suggest checking the official documentation clicking here如果这不起作用,我建议单击此处查看官方文档

.addField('', '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM