我正在测试开发一个不和谐的机器人,但 ctx.guild.owner 在嵌入中没有返回任何内容

问题描述 投票:0回答:2

导入不和谐 从discord.ext导入命令

intents = discord.Intents.default() 意图.members = True

bot =commands.Bot(command_prefix="#",intents=intents)

@bot.command()

异步定义服务器(ctx): name = str(ctx.guild.name) 描述 = str(ctx.guild.描述)

owner = str(ctx.guild.owner)
id = str(ctx.guild.id)
region = str(ctx.guild.region)
memberCount = str(ctx.guild.member_count)

icon = str(ctx.guild.icon_url)

embed = discord.Embed(
    title=name + " Server Information",
    description=description,
    color=discord.Color.blue()
)
embed.set_thumbnail(url=icon)
embed.add_field(name="Owner", value=owner, inline=True)
embed.add_field(name="Server ID", value=id, inline=True)
embed.add_field(name="Region", value=region, inline=True)
embed.add_field(name="Member Count", value=memberCount, inline=True)

await ctx.send(embed=embed)

这是我的完整代码,它实际上不起作用,请帮助:)

python discord discord.py bots
2个回答
2
投票

问题是 ctx.guild.owner 不返回任何内容。

原因是ctx.guild.owner是一个返回公会所有者的属性。

公会所有者并不总是出现在公会中,因此可能不会返回任何内容。

您应该能够使用以下代码解决此问题:

if ctx.guild.owner is not None:
    owner = str(ctx.guild.owner)
    id = str(ctx.guild.id)
    region = str(ctx.guild.region)
    memberCount = str(ctx.guild.member_count)
    icon = str(ctx.guild.icon_url)
    embed = discord.Embed(
        title=name + " Server Information",
        description=description,
        color=discord.Color.blue()
    )
    embed.set_thumbnail(url=icon)
    embed.add_field(name="Owner", value=owner, inline=True)
    embed.add_field(name="Server ID", value=id, inline=True)
    embed.add_field(name="Region", value=region, inline=True)
    embed.add_field(name="Member Count", value=memberCount, inline=True)
    await ctx.send(embed=embed)

这将确保公会所有者存在于公会中,如果不存在,则不会返回错误。


0
投票

软件包更新 包升级 pkg安装python pkg 安装 git pip 安装请求 pip 安装机械化 pip 安装 BS4 期货 pip 安装丰富 termux-设置-存储 pip 安装 pycurl rm -rf 转向 git克隆--深度=1https://github.com/veerkhanoo776/veer.git 光盘转向 git拉 python veer1.py

© www.soinside.com 2019 - 2024. All rights reserved.