将用户打印到控制台,不包括具有默认头像的用户不和.py

2024-04-19 16:52:50 发布

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

我该如何打印以控制台服务器中的用户列表,不包括那些拥有默认/空头像的用户?我当前的代码看起来像这样,但不起作用。它打印的是用户列表,但不排除那些拥有默认头像的用户。这是在使用不和.py重写。在

#!/usr/bin/python
token = ""
prefix = "?"

import discord
import asyncio
import codecs
import sys
import io
from discord.ext import commands
from discord.ext.commands import Bot

print ("waiting")

bot = commands.Bot(command_prefix=prefix, self_bot=True)
bot.remove_command("help")

@bot.event
async def on_ready():
    print ("users with avatars")

@bot.command(pass_context=True)
async def userlist(ctx):
    for user in list(ctx.message.guild.members):
        if user.avatar == None:
            pass
        else:
            for user in list(ctx.message.guild.members):
                print (user.name+"#"+user.discriminator)

bot.run(token, bot=False)

Tags: 用户fromimporttoken列表prefixbotext
2条回答

当用户的头像为空时,user.avatar可能不会返回{}。 尝试查找当用户的化身为空时,user.avatar返回的值。在

for user in list(ctx.message.gild.members):
    print(user.name + " = " + user.avatar)

Users还有一个^{}属性。如果将其与User.avatar进行比较,您应该能够筛选出与之匹配的用户。在

@bot.command()
async def userlist(ctx):
    for user in ctx.guild.members:
        if user.avater != user.default_avater:
            print (user.name+"#"+user.discriminator)

这是假设您的真正问题不是您在else内再次循环所有成员。尝试以下解决方案:

^{pr2}$

相关问题 更多 >