You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改discord.js机器人的meme指令仅展示点赞超4k的reddit梗图

实现方案

你当前的代码逻辑是单次请求拉取随机meme,只需新增点赞数阈值判断即可,不符合要求就重新发起请求。建议额外增加最大重试次数限制,避免无限循环触发Reddit接口速率限制。

修改后代码

const embed = new Discord.MessageEmbed();
// 定义最大重试次数,可根据需求自行调整
const MAX_RETRY = 10;
let retryCount = 0;

function getMeme() {
  got('https://www.reddit.com/r/memes/random/.json')
    .then(response => {
      const [list] = JSON.parse(response.body);
      const [post] = list.data.children;

      const memeUpvotes = post.data.ups;
      // 点赞数不足4k且还有重试次数时重新拉取
      if (memeUpvotes < 4000 && retryCount < MAX_RETRY) {
        retryCount++;
        return getMeme();
      }
      // 重试次数耗尽时返回提示
      if (retryCount >= MAX_RETRY) {
        return message.channel.send('暂时未找到符合要求的高赞梗图,请稍后再试');
      }

      const permalink = post.data.permalink;
      const memeUrl = `https://reddit.com${permalink}`;
      const memeImage = post.data.url;
      const memeTitle = post.data.title;
      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);
    })
    .catch(err => {
      console.error('拉取meme出错:', err);
      message.channel.send('拉取梗图失败,请稍后再试');
    })
}

// 首次调用拉取逻辑
getMeme();

内容的提问来源于stack exchange,提问作者Aromatic Armadillo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 10:36:00