我正在制作一个具有实用命令的 Discord 机器人,因此您可以将其用于某些有用的目的。这是我使用的基本代码:
import os
import random
import discord
from discord.ext import commands
from dotenv import load_dotenv
from discord.ext.commands import Bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix=["bob ", "BOB ", "Bob ", "bOb "], case_insensitive=True)
@bot.command(name='serverStats', help="Gets the statistics of a server.")
async def serverinfo(ctx):
guild_members = ctx.guild.member_count
serverInfo=discord.Embed(title=f"OK, getting the stats of {ctx.guild.name} now.", description="We're just gathering some general basic information about this server...", color=0xfcd602)
serverInfo.set_author(name="Requested by " + ctx.author.display_name, url=random.choice(aintsupposedtobeheresunnyboy), icon_url=ctx.author.avatar_url)
serverInfo.add_field(name="Member Count:", value=f"{guild_members}")
serverInfo = await ctx.send(embed=serverInfo)
我正在尝试制作我的机器人,以便它可以获取服务器的成员,计算机器人和不计算机器人。我已经找到了计算包括机器人在内的所有成员的方法,这将使用 ctx.guild.member_count。但是,如果没有机器人工作,我不知道如何使成员计数。我在网上查了一些解决方案,我找到的一个解决方案是这样的:
len([m for m in ctx.guild.members if not m.bot])
但是,由于某种原因,这对我不起作用,因为机器人响应的成员数为 0。有什么办法可以解决这个问题?