【发布时间】: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