配置文件命令(需要一点帮助)

时间:2021-07-08 00:12:59

标签: python discord discord.py

是的,所以我有这个配置文件命令。在我尝试添加更多内容之前,它奏效了。但是,在尝试添加鉴别器和相互公会选项后,它停止工作。文档使用了我使用的代码,但它不起作用。

@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)

任何想法或帮助?具体来说,我想添加歧视和相互公会选项,以及炒作队的房子,但这也不起作用。

1 个答案:

答案 0 :(得分:0)

  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