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
相关产品推荐
相关产品推荐

