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

Print users to console excluding those with default avatars Discord.py

我将如何打印以控制服务器中的用户列表,不包括具有 default/null 头像的用户?我当前的代码看起来像这样,但不起作用。它打印的是用户列表,但不排除具有默认头像的用户。这是使用 Discord.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)

当用户的头像为空白时,user.avatar可能不会returnNone。 尝试在用户头像为空白时找到 user.avatar return 的值。

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

Users 也有一个 User.default_avatar 属性。如果将其与 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 中再次遍历所有成员。试试这个变体的解决方案:

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