【问题标题】:Server Info Command Discord.py服务器信息命令 Discord.py
【发布时间】:2022-01-17 09:28:46
【问题描述】:
    @client.command()
    async def server(ctx):
    embed = discord.Embed(title = f"{ctx.guild.name} Info", description = "Information of this Server", color = discord.Colour.blue())
    embed.add_field(name = '????Server ID', value = f"{ctx.guild.id}", inline = True)
    embed.add_field(name = '????Created On', value = ctx.guild.created_at.strftime("%b %d %Y"), inline = True)
    embed.add_field(name = '????Owner', value = f"{ctx.guild.owner}", inline = True)
    embed.add_field(name = '????Members', value = f'{ctx.guild.member_count} Members', inline = True)
    embed.add_field(name = '????Channels', value = f'{ctx.guild.text_channel_count} Text | {ctx.guild.voice_channel_count} Voice', inline = True)
    embed.add_field(name = '????Region', value = f'{ctx.guild.region}', inline = True)
    embed.set_thumbnail(url = ctx.guild.icon_url)
    embed.set_footer(text = "⭐ • Duo")    
    embed.set_author(name = f'{ctx.guild.name}', url = embed.Empty, icon_url = {ctx.guild.icon_url})
    await ctx.send(embed=embed)

我写了这段代码,一切看起来都很好,当我运行机器人时,没有错误。终端是空白的,但是当我运行这个命令时,它不起作用,它没有发送响应,终端也没有错误。请帮忙..

【问题讨论】:

  • 使用print 语句查看机器人卡在哪里。我也没听说过embed.Empty,这是什么?

标签: python discord discord.py


【解决方案1】:

乍一看,你没有正确缩进。我也从未听说过url = embed.Empty

顺便说一句,我用embed.set_author(name = f'{ctx.author.name}' icon_url = {ctx.message.author.avatar_url} 替换了embed.set_authorembed.set_author(name = f'{ctx.guild.name}', icon_url = {ctx.guild.icon_url}),因为在我看来,那是您想要显示有关作者而不是公会信息的行。

@client.command()
async def server(ctx):
    embed = discord.Embed(title = f"{ctx.guild.name} Info", description = "Information of this Server", color = discord.Colour.blue())
    embed.add_field(name = '?Server ID', value = f"{ctx.guild.id}", inline = True)
    embed.add_field(name = '?Created On', value = ctx.guild.created_at.strftime("%b %d %Y"), inline = True)
    embed.add_field(name = '?Owner', value = f"{ctx.guild.owner}", inline = True)
    embed.add_field(name = '?Members', value = f'{ctx.guild.member_count} Members', inline = True)
    embed.add_field(name = '?Channels', value = f'{ctx.guild.text_channel_count} Text | {ctx.guild.voice_channel_count} Voice', inline = True)
    embed.add_field(name = '?Region', value = f'{ctx.guild.region}', inline = True)
    embed.set_thumbnail(url = ctx.guild.icon_url)
    embed.set_footer(text = "⭐ • Duo")    
    embed.set_author(name = f'{ctx.author.name}' icon_url = {ctx.message.author.avatar_url}
    await ctx.send(embed=embed)

如果解决缩进并删除 url = embed.Empty 不起作用,那么您必须逐行运行调试打印语句,直到找出麻烦的行。

【讨论】:

    【解决方案2】:

    这里有一些问题。

    1。缩进。

    这是基本的东西。在async def server(ctx): 行之后的每一行缩进。

    2。阅读文档!

    如果您查看guild 的文档,您会发现guild.text_channel_countguild.voice_channel_count 不存在。相反,您必须改为使用len(guild.text_channels)len(guild.voice_channels)

    3。不必要的大括号

    在倒数第二行,我不确定您为什么将{} 包裹在icon_url = {ctx.message.author.avatar_url} 周围。所以摆脱那些。

    另外,url=embed.Empty 是完全没有必要的,它是embed.set_author() (see here) 的默认值。所以摆脱它。

    4。 [不是错误,而是快速提示]

    ctx.guild.owner 本身会给出一个 Member 对象,但是当你把它放在一个 f 字符串中时,它会给出一个丑陋的表示(类似于<discord.member_object_at_12391873123> or something idk 所以改为使用ctx.guild.owner.mention 所以它实际上会显示提及格式的服务器所有者(但是由于它是嵌入的,因此实际上不会对它们执行 ping 操作)

    总的来说,你应该有:

    @client.command()
    async def server(ctx):
        embed = discord.Embed(title=f"{ctx.guild.name} Info", description="Information of this Server", color=discord.Colour.blue())
        embed.add_field(name='?Server ID', value=f"{ctx.guild.id}", inline=True)
        embed.add_field(name='?Created On', value=ctx.guild.created_at.strftime("%b %d %Y"), inline=True)
        embed.add_field(name='?Owner', value=f"{ctx.guild.owner.mention}", inline=True)
        embed.add_field(name='?Members', value=f'{ctx.guild.member_count} Members', inline=True)
        embed.add_field(name='?Channels', value=f'{len(ctx.guild.text_channels)} Text | {len(ctx.guild.voice_channels)} Voice', inline=True)
        embed.add_field(name='?Region', value=f'{ctx.guild.region}', inline=True)
        embed.set_thumbnail(url=ctx.guild.icon_url) 
        embed.set_footer(text="⭐ • Duo")    
        embed.set_author(name=f'{ctx.author.name}' icon_url=ctx.message.author.avatar_url
    
        await ctx.send(embed=embed)
    
    

    编辑:我忘了提到我已经对您的代码进行了一些格式化以遵守PEP8(如函数调用中= 周围没有空格等)

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2020-11-17
      • 2018-11-22
      • 2018-08-27
      • 1970-01-01
      相关资源
      最近更新 更多