【问题标题】:DiscordAPIError: Invalid Form Body - Discord slash commandsDiscordAPIError:无效的表单正文 - Discord 斜杠命令
【发布时间】:2021-08-04 01:44:09
【问题描述】:

DiscordAPIError: Invalid Form Body 有很多问题,但似乎没有一个答案有帮助。
使用message.channel.send(embed) 时,嵌入可以正常工作,没有错误。但是,当尝试通过斜杠命令发送嵌入时会导致很多问题。

Index.js:

const Discord = require('discord.js');
const fs = require('fs');
const client = new Discord.Client();
require('dotenv').config();
client.login(process.env.token);

client.on('ready', () => {
    const createAPIMessage = async (interaction, content) => {
        const { data, files } = await Discord.APIMessage.create(
            client.channels.resolve(interaction.channel_id),
            content
        )
            .resolveData()
            .resolveFiles()

        return { ...data, files }
    }

    client.ws.on('INTERACTION_CREATE', async interaction => {
        const command = (interaction.data.name).toLowerCase();
        const args = interaction.data.options;

        for (const file of commandFiles) {
            var commandFile = require(`./commands/${file}`);
            if (command == commandFile.name) {
                commandFile.execute(interaction, client, async function (message) {
                    if (typeof message == 'object')
                        message = await createAPIMessage(interaction, message);
                    client.api.interactions(interaction.id, interaction.token).callback.post({
                        data: {
                            type: 4,
                            data: {
                                content: message
                            }
                        }
                    });
                });
            }
        }
    });
});

commands/urban.js

const Discord = require('discord.js');
const urban = require('urban');

module.exports = {
    name: 'urban',
    description: 'Search the dictionary for a word',
    options: [{ name: 'word', description: 'A word to search in the dictionary.', type: 3, required: true }],
    execute(interaction, client, callback) {
        //console.log(interaction)
        urban(interaction.data.options[0].value).first(async json => {
            if (!json) return callback('The word ' + interaction.data.options[0].value + ' does not exist');
            const embed = new Discord.MessageEmbed()
                .setTitle(json.word)
                .setDescription((json.definition).split('[').join('').split(']').join(''))
                .setFooter('Billybobbeep is not responsible for what you search | Written by: ' + (json.author || 'Unknown'))
                .addField('Upvotes', json.thumbs_up || 0, true)
                .addField('Downvotes', json.thumb_down || 0, true)
            callback(embed)
        });
    }
}

收到完整错误:

(node:16584) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
data.content: Could not interpret "{'tts': False, 'embed': {'title': 'hi', 'type': 'rich', 'description': "testing", 'url': None, 'timestamp': None, 'color': None, 'fields': [{'name': 'Upvotes', 'value': '319', 'inline': True}, {'name': 'Downvotes', 'value': '0', 'inline': True}], 'thumbnail': None, 'image': None, 'author': None, 'footer': 'None'}, 'embeds': [{'title': 'hi', 'type': 'rich', 'description': "testing", 'url': None, 'timestamp': None, 'color': None, 'fields': [{'name': 'Upvotes', 'value': '319', 'inline': True}, {'name': 'Downvotes', 'value': '0', 'inline': True}], 'thumbnail': None, 'image': None, 'author': None, 'footer': 'None' }], 'files': []}" as string.

【问题讨论】:

  • 你传入一个对象,该方法需要一个字符串
  • 看起来描述是双引号,而其他所有内容都是单引号。 JSON 字符串初始化为使用双引号,因此当它在字符串中遇到一组双引号时,它会将其解释为字符串结尾。
  • @mjm0813 JSON字符串仅用于错误,双引号不会以任何方式影响对象。
  • @Elitezen 那么如何将嵌入对象转换为字符串呢?许多解决方案提供与我的问题相同的代码

标签: javascript node.js discord.js


【解决方案1】:

嵌入使用自己的属性

data: {
embeds: [embed1];
}

您应该注意它是一个数组,这只是一个示例,请将 embed1 替换为您想要的任何嵌入。不用担心,您不需要通过添加 data.content 让它看起来很奇怪,因为不和谐会造成它,所以只需要 data.content data.embeds。这意味着是的,这个嵌入可以是全部内容

【讨论】:

    【解决方案2】:

    虽然MrMythical's answer 可能会起作用,但我的问题是我试图将嵌入作为内容发送。
    完整解决方案:

    const Discord = require('discord.js');
    const fs = require('fs');
    const client = new Discord.Client();
    require('dotenv').config();
    client.login(process.env.token);
    
    client.on('ready', () => {
        const createAPIMessage = async (interaction, content) => {
            const { data, files } = await Discord.APIMessage.create(
                client.channels.resolve(interaction.channel_id),
                content
            )
                .resolveData()
                .resolveFiles()
    
            return { ...data, files }
        }
    
        client.ws.on('INTERACTION_CREATE', async interaction => {
            const command = (interaction.data.name).toLowerCase();
            const args = interaction.data.options;
    
            for (const file of commandFiles) {
                var commandFile = require(`./commands/${file}`);
                if (command == commandFile.name) {
                    commandFile.execute(interaction, client, async function(message) {
                        let data = {
                            content: message
                        }
                        if (typeof message == 'object')
                            data = await createAPIMessage(interaction, message);
                        client.api.interactions(interaction.id, interaction.token).callback.post({
                            data: {
                                type: 4,
                                data
                            }
                        });
                    });
                }
            }
        });
    });
    

    将所有嵌入定义为 data,而不是 JSON 数据本身中的任何内容。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2021-04-09
      • 2022-10-18
      • 2021-12-31
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多