【问题标题】:So I made a meme command for my discord.js bot所以我为我的 discord.js 机器人做了一个 meme 命令
【发布时间】:2021-09-04 10:59:41
【问题描述】:
const embed = new Discord.MessageEmbed();
    got('https://www.reddit.com/r/memes/random/.json')
        .then(response => {
            const [list] = JSON.parse(response.body);
            const [post] = list.data.children;

            const permalink = post.data.permalink;
            const memeUrl = `https://reddit.com${permalink}`;
            const memeImage = post.data.url;
            const memeTitle = post.data.title;
            const memeUpvotes = post.data.ups;
            const memeNumComments = post.data.num_comments;

            embed.setTitle(`${memeTitle}`);
            embed.setURL(`${memeUrl}`);
            embed.setColor('BLACK');
            embed.setImage(memeImage);
            embed.setFooter(`???? ${memeUpvotes}  |  ???? ${memeNumComments}`);

            message.channel.send(embed);
        })

这是代码。我想要它做的是只显示超过 4k 点赞的表情包,我该如何实现?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    你可以应用一个非常简单的方法,添加一个 if 语句

    if(parseInt(post.data.ups) > 4000) {
    // Code here 
    }
    

    parseInt() 是一个返回整数值的函数,你可以使用它,其余的似乎不言自明?。

    【讨论】:

    • 我试过了,效果很好。唯一的问题是,如果它之前获得了少于 4k 点赞的 meme,它不会给出任何输出。有没有办法解决这个问题?
    • 您的问题表明您只需要显示最多 4k 的赞成票,所以我这样做了哈哈,无论如何您可以添加一个 else 声明,低于 4k if(parseInt(post.data.ups) > 4000) { // Code here } else { // aww man less than 4k upvotes },确保将其标记为正确额外的代表!
    • @AromaticArmadillo 如果这对您有帮助,您可以通过单击复选标记来接受此答案:D
    猜你喜欢
    • 2022-01-12
    • 2020-10-11
    • 2021-05-16
    • 2021-06-02
    • 2020-08-21
    • 2021-11-19
    • 2021-08-09
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多