JavaScript Discord Embed 未读取某些 Embed.set 命令。 (Discord 模因机器人)[[已解决]]

JavaScript Discord Embed is not reading certain Embed.set commands. (Discord meme bot) [[Solved]]

我正在尝试制作一个不和谐的表情包机器人(在本例中,是可爱的动物图片机器人)。代码是这样的;

但是当我在discord中使用命令时,回复只显示描述,不显示颜色或图像。

我做错了什么?

这里是复制粘贴命令文件的代码;

const { MessageEmbed } = require('discord.js');
const randomPuppy = require('random-puppy');

module.exports = {
    name: 'cute',
    description: 'Embeds pictures pulled from listed subreddits',
    execute(message, args, Discord){
        let reddit = [
            "aww",
            "puppies",
            "toebeans"
        ]

        let subreddit = reddit[Math.floor(Math.random()*reddit.length -1)];

        const cuteEmbed = new MessageEmbed()
        .setDescription("Some cute animals to blow away your anxieties!");

        randomPuppy(subreddit).then(url => {
            console.log(url);
            const cuteurl = url;
            
            cuteEmbed.setColor('#91B2C7');
            cuteEmbed.setImage('${cuteurl}');
            });

            message.channel.send(cuteEmbed);
        }
    }

请帮忙:'(

编辑:后退。该死的背部滴答声。我正在使用一个新代码,所以 idk 如果使用反引号会修复它,但这是代码中的一个错误;我没有使用反引号进行插值。

方法randomPuppy()是异步的。这意味着您需要等待发送嵌入消息的承诺。在您当前的代码中,您无需等待请求完成即可发送嵌入。

您必须更改您的代码:

randomPuppy(subreddit).then(url => {
  cuteEmbed.setColor('#91B2C7');
  cuteEmbed.setImage('${url}');
});

message.channel.send(cuteEmbed);

收件人:

randomPuppy(subreddit).then(url => {
  cuteEmbed.setColor('#91B2C7');
  cuteEmbed.setImage('${url}');
  message.channel.send(cuteEmbed); //Send the embed once the request is completed.
});
    randomPuppy(subreddit).then(url => {
        console.log(url);
        const cuteurl = url;
        
        cuteEmbed.setColor('#91B2C7');
        cuteEmbed.setImage('${cuteurl}');
        });

        message.channel.send(cuteEmbed);
    }

即使请求未完成,您仍在尝试发送嵌入,您需要做的就是将 message.channel.send() 放入 .then

    randomPuppy(subreddit).then(url => {
        console.log(url);
        const cuteurl = url;
        
        cuteEmbed.setColor('#91B2C7');
        cuteEmbed.setImage('${cuteurl}');
        message.channel.send(cuteEmbed);
        });
    }

而且我注意到您错过了输入 keypress 之一的内容,您应该输入 backquote 而不是 quote,因此在编辑您的代码之后。您只需要将 quote 编辑为 backquote:

    randomPuppy(subreddit).then(url => {
        console.log(url);
        const cuteurl = url;
        
        cuteEmbed.setColor('#91B2C7');
        cuteEmbed.setImage('${cuteurl}'); //This is the part you need to change it
        message.channel.send(cuteEmbed);
        });
    }

为此:

   randomPuppy(subreddit).then(url => {
        console.log(url);
        const cuteurl = url; //image should call this after changing it
        
        cuteEmbed.setColor('#91B2C7');
        cuteEmbed.setImage(`${cuteurl}`); //To this, to call your cuteurl
        message.channel.send(cuteEmbed);
        });
    }