服务器信息命令Discord.py


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

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

乍一看,您没有正确缩进。我也从未听说过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没有效果,那么您将不得不逐行运行调试打印语句,直到隔离出麻烦的行。

这里没有什么问题。

1.缩进

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

2.阅读文档

如果你看一下公会的文档,你会发现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()的默认值(参见此处(。所以把它扔掉。

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(就像函数调用中=周围没有空格一样(

最新更新