DiscordAPIError: Invalid Form Body - Discord 斜杠命令

时间:2021-05-13 19:12:14

标签: javascript node.js discord.js

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
                            }
                        }
                    });
                });
            }
        }
    });
});

命令/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.

2 个答案:

答案 0 :(得分:0)

嵌入使用自己的属性

data: {
embeds: [embed1];
}

你应该注意到它是一个数组,这只是一个例子,用你想要的任何嵌入替换 embed1。别担心,你不需要通过添加 data.content 让它看起来很奇怪,因为 discord 使得它只需要 data.content data.embeds。这意味着是的,这个嵌入可以是完整的内容

答案 1 :(得分:0)

虽然 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 数据本身中的任何内容。