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

Discord.py统计Discord服务器内机器人总数 解决guild无bot_count属性问题

实现方案

Discord.py 的 Guild 对象原生没有提供 bot_count 属性,你可以通过遍历服务器成员列表统计机器人账号数量,操作步骤如下:

  • 第一步:开启 Members Intent
    该权限默认关闭,你需要先到 Discord 开发者后台的应用设置页,进入「Bot」选项卡打开「Server Members Intent」开关,同时在代码初始化 Bot 时显式启用该权限:
    import discord
    from discord.ext import commands
    
    intents = discord.Intents.default()
    intents.members = True # 必须开启才能获取全量服务器成员数据
    
    bot = commands.Bot(command_prefix="你的指令前缀", intents=intents)
    
  • 第二步:修改统计逻辑
    遍历服务器成员,统计bot属性为True的账号总数即可:
    @bot.command(name="serverinfo")
    async def serverinfo(ctx: commands.Context):
        bot_count = sum(member.bot for member in ctx.guild.members)
        await ctx.send(f"服务器内机器人总数:{bot_count}")
    

以上写法兼容 Discord.py 2.x 全版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 16:54:08