首页 > 解决方案 > 无效的表单正文 embed.image.url:无法将“{}”解释为字符串

问题描述

我正在尝试创建一个 meme 命令,它从 reddit 发布 meme

这是 meme 发布的命令:

const randompuppy = require("random-puppy");

    if(message.content.startsWith(prefix + 'meme')){
        const subredddits = ["dankmeme", "meme", "me_irl"];
        // Grab a random property from the array
        const random = subredddits[Math.floor(Math.random() * subredddits.length)];

        // Get a random image from the subreddit page
        const image = randompuppy(random);
        const embed = new RichEmbed()
            .setColor("RANDOM")
            .setImage(image)
            .setTitle(`From /r/${random}`)
            .setFooter(`© Extendo Selfbot | prefix: ${prefix} | Made By 7teen#1646`)
            .setURL(`https://reddit.com/r/${random}`);
        message.channel.send(embed);
    }

这是错误

(node:2180) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.image.url: Could not interpret "{}" as string.
    at C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\Discord Bots\Testing\SELFBOT\extendo v2\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15
    at C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\Discord Bots\Testing\SELFBOT\extendo v2\node_modules\snekfetch\src\index.js:215:21
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:2180) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:2180) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

帮助将不胜感激!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


根据随机小狗npm 页面:

randomPuppy(subreddit)

从选定的 subreddit返回一个promise随机图像 url。警告:我们不能保证它会是一只小狗的形象!

randomPuppy函数不返回字符串,而是返回解析字符串的承诺。要使用它,您可以使用awaitPromise 或使用.then()函数。

// when using `await`, always make sure the function is async!
// client.on('message', async (message) => {...}
//                      ^^^^^

const image = await randompuppy(random);
const embed = new RichEmbed()
 .setColor('RANDOM')
 .setImage(image)
 .setTitle(`From /r/${random}`)
 .setFooter(`© Extendo Selfbot | prefix: ${prefix} | Made By 7teen#1646`)
 .setURL(`https://reddit.com/r/${random}`);
message.channel.send(embed);

// or:

randompuppy(random).then((image) => {
 const embed = new RichEmbed()
  .setColor('RANDOM')
  .setImage(image)
  .setTitle(`From /r/${random}`)
  .setFooter(`© Extendo Selfbot | prefix: ${prefix} | Made By 7teen#1646`)
  .setURL(`https://reddit.com/r/${random}`);
 message.channel.send(embed);
});

推荐阅读