首页 > 解决方案 > 我无法弄清楚如何在用户 DND 状态计数器循环中从 json 获取公会 ID 和频道 ID

问题描述

  @commands.Cog.listener()
  async def on_ready(self):
    self.send.start()

  @tasks.loop(minutes=10)
  async def send(self):
    with open('/home/runner/EBOT/info/odi_ch_id.json', 'r') as f:
      file = json.load(f)
    channelid = file[str(guild.id)]

    intchannelid = int(channelid)

    guild = self.bot.get_guild(guild.id)
    
    memchannel=guild.get_channel(intchannelid)
    online = len(list(filter(lambda m: str(m.status) == "online", guild.members)))
    idle = len(list(filter(lambda m: str(m.status) == "idle", guild.members)))
    dnd = len(list(filter(lambda m: str(m.status) == "dnd", guild.members)))

    await memchannel.edit(name=f" {online} ⛔ {dnd}  {idle}")

  @send.before_loop
  async def before(self):
    await self.bot.wait_until_ready()  

  send.start()

这是一个在线免打扰空闲计数器,每 10 分钟循环一次。我曾经指定公会 ID 和频道 ID,因为我只在 1 个服务器中使用了我的机器人。现在我正在将我的机器人重新制作为多服务器机器人。所以我用公会 ID 和频道 ID提供json 。我希望机器人更新 json 包含的每台服务器中的频道。

Json 看起来像这样

{
    "830981882890944512": 880480261899419668
}

左整数是公会 id,右整数是频道 id

标签: pythondiscorddiscord.pycounter

解决方案


推荐阅读