从公会中删除成员ID,并将其列入列表

2024-10-01 09:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试执行一个命令,返回公会中的所有成员ID,到目前为止我一直在这样做:

themembers = []
@client.command()
async def getids(ctx):
    guild = ctx.guild
    for members in guild.members:
        try:
            themembers.append(members.id)
        except:
            pass
    print(themembers)

但是这个代码只给了我机器人帐户的ID;如何解决此问题


Tags: in命令clientidforasyncdef成员
2条回答

试试看:

@bot.event
async def on_ready():
    print(list(mab(lambda i: i.id, bot.get_all_members())))

确保在application中启用了意图

Make sure both options are enabled

import discord
from discord.ext import commands
from discord.utils import get

intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix=("!"), case_insensitive=True, guild_subscriptions=True, intents=intents)

@bot.event
async def on_ready():
    member_list = []
    for guild in bot.guilds:
        for member in guild.members:
            member_list.append({member.name:member.id})
    print(member_list)

相关问题 更多 >