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

抓取Instagram粉丝数并定时同步为Discord频道名称的问题求助

问题原因与修复方案

你的代码存在三个核心问题导致频道更新失败:

  • 变量作用域与异步执行逻辑错误:instaObj.getFollowers是异步请求方法,你在.then回调内部定义的getFollowers变量仅在回调作用域内生效,回调外的代码执行时异步请求还未返回结果,此时调用getFollowers属于未定义变量,自然无法传入频道名称修改接口。
  • Discord.js 接口调用不兼容:如果你使用的是v12及以上版本的Discord.js,获取服务器、频道实例需要从缓存中调用,原有bot.guilds.get、bot.channels.get写法已失效,需要替换为bot.guilds.cache.get、bot.channels.cache.get才能正确拿到频道实例。
  • Discord 速率限制触发风险:Discord官方限制频道名称修改频率为每5分钟最多2次,你当前设置30秒修改一次会直接触发速率限制,请求会被官方拦截。
修复后代码
const Discord = require('discord.js');
const config = require("./config.json");
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
const instaObj = require('instagram-basic-data-scraper-with-username');
const user = 'milliontoken';

// Log stats-bot in to the server and set status
bot.on("ready", async () => {
  console.log(`${bot.user.username} has logged on.`)
  bot.user.setActivity('Half Life 3', { type: 'PLAYING' })
    .then(presence => console.log(`Activity set to ${presence.game ? presence.game.name : 'none'}`))
    .catch(console.error);

  // 获取服务器与频道实例,v12+版本需要加cache
  const guild = bot.guilds.cache.get('875154076814438430');
  const totalUsers = bot.channels.cache.get('875154076814438434');

  // 建议间隔改为5分钟(300000毫秒)避免触发速率限制
  setInterval(function() {
    // 所有依赖粉丝数的逻辑都要放到异步回调内部执行
    instaObj.getFollowers(user).then(res => {
      const getFollowers = res.data;
      // 打印调试信息
      console.log("Total Users: " + getFollowers);
      // 更新频道名称
      totalUsers.setName("Total Users: " + getFollowers)
        .then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
        .catch(console.error);
    }).catch(console.error);
  }, 300000)
});

bot.login(config.token);

内容的提问来源于stack exchange,提问作者Ryan Tang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:27:02