Profile命令(需要一些帮助)

2024-05-18 23:32:59 发布

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

对,我有这个profile命令。在我尝试添加更多内容之前,它成功了。但是,在尝试添加鉴别器和互助协会选项后,它停止工作。文档使用了我使用的代码,但不起作用

@commands.command()
async def profile(self, ctx, *, user: discord.Member):
    em = discord.Embed(title=f"Profile of {user}", color=0xeb0000)
    em.set_thumbnail(url = user.avatar_url)
    em.add_field(name="Nickname", value=f'{user.nick}', inline=True)
    em.add_field(name="Joined at", value=f'{user.joined_at}', inline=True)
    em.add_field(name="ID", value=f'{user.id}', inline=True)
    em.add_field(name="Discrim", value=f'{user.discriminator}', inline=True)
    em.add_field(name="Mutual Guilds", value=f'{user.mutual_guilds}', inline=True)
    em.set_footer(text="\nGOD", icon_url="https://i.imgur.com/Zfh8Una.jpg")
    em.timestamp = datetime.datetime.utcnow()
    await ctx.send(embed = em)

工作代码:

@commands.command()
async def profile(self, ctx, *, user: discord.Member):
    em = discord.Embed(title=f"Profile of {user}", color=0xeb0000)
    em.set_thumbnail(url = user.avatar_url)
    em.add_field(name="Nickname", value=f'{user.nick}', inline=True)
    em.add_field(name="Joined at", value=f'{user.joined_at}', inline=True)
    em.add_field(name="ID", value=f'{user.id}', inline=True)
    await ctx.send(embed = em)

有什么想法或帮助吗?具体来说,我想添加discrim和互助协会选项,以及hypesquad house,但这也不起作用


Tags: nameaddtrueurlfieldvalueinlineprofile
1条回答
网友
1楼 · 发布于 2024-05-18 23:32:59
  1. 在你的set_footer中,你做了emb.set_footer。这将引发错误NameError: name 'emb' is not defined,因此您需要将其更改为em.set_footer
  2. em.timestamp中,您可能会收到错误AttributeError: type object 'datetime.datetime' has no attribute 'datetime'。目前,我建议使用datetime.utcnow(),它应该做完全相同的事情
  3. user.mutual_guilds返回一个guild object,问题是每个公会都会得到类似的结果:[<Guild id=778530777729335316 name='Friends of Mara' shard_id=1 chunked=True member_count=29>, <Guild id=747061937673732097 name='Blitz Support' chunked=True member_count=18>...etc。由于您可能只需要每个公会的名称,因此必须遍历提供的列表
  4. 假设你的机器人有100台服务器。如果有人去!profile @yourbot,很可能会有太多的互助协会。不过,我不会在这里解释如何应对这种情况

另外,作为旁注,请检查您的错误处理程序是否正在“吃掉”您的错误。在测试时,我收到了如上所述的多个错误,所以请确保这样做

修订守则如下:

@client.command()
async def test(ctx, *, user: discord.Member=None): # included this None as an extra in case user doesn't mention anyone
    if user == None:
        user = ctx.author # so if the user didn't mention anyone, the ctx.author becomes the user
    em = discord.Embed(title=f"Profile of {user}", color=0xeb0000)
    em.set_thumbnail(url = user.avatar_url)
    # following furas' comment, you don't need the f-strings for single strings
    # but if you do, it will not break the code
    em.add_field(name="Nickname", value=f'{user.nick}', inline=True)
    em.add_field(name="Joined at", value=f'{user.joined_at}', inline=True)
    em.add_field(name="ID", value=f'{user.id}', inline=True)
    em.add_field(name="Discrim", value=f'{user.discriminator}', inline=True)

    # new part #
    guild_list = [] # create a new empty list
    for guild in user.mutual_guilds: # for every guild the bot and the user shares..
        guild_list.append(str(guild.name)) # append the name to the guild_list
    # str(guild.name) is important, otherwise you'll get an error of:
    # TypeError: sequence item 0: expected str instance, Guild found
    em.add_field(name="Mutual Guilds", value=f"{', '.join(guild_list)}", inline=True)
    ##

    em.set_footer(text="\nGOD", icon_url="https://i.imgur.com/Zfh8Una.jpg")
    em.timestamp = datetime.utcnow() # changed it to datetime.utcnow()
    await ctx.send(embed = em)

以下是按预期工作的代码: Code working as expected

相关问题 更多 >

    热门问题