我在discord.py中遇到了一个问题,下面的代码运行得很好,但ctx.guild.owner
没有返回,文档中说,如果发生这种情况,事件guild_subscriptions
设置为False
,我如何将此事件设置为True
才能使其工作?我在discord.py文档中找不到任何解决方案,谷歌也找不到。
我的serverstats
命令的代码:
@commands.command(name="serverstats", aliases = ["serverinfo", "si", "ss", "check-stats", 'cs'])
async def serverstats(self, ctx: commands.Context):
embed = discord.Embed(
color = discord.Colour.orange(),
title = f"{ctx.guild.name}")
embed.set_thumbnail(url = f"{ctx.guild.icon_url}")
embed.add_field(name = "ID", value = f"{ctx.guild.id}")
embed.add_field(name = "👑Owner", value = f"{ctx.guild.owner}")
embed.add_field(name = "🌍Region", value = f"{ctx.guild.region}")
embed.add_field(name = "👥Member Count", value = f"{ctx.guild.member_count}")
embed.add_field(name = "📆Created at", value = f"{ctx.guild.created_at}")
embed.set_footer(icon_url = f"{ctx.author.avatar_url}", text = f"Requested by {ctx.author.name}")
await ctx.send(embed=embed)
事件guild_subscriptions设置为False
我在不一致.py文档中找不到任何解决方案
这不是事件。文档说,这是创建commands.Bot instance
时的一个参数,可以在Clients的文档中找到,也可以。如果你向下滚动到参数列表的底部,你会看到guild_subscriptions(bool)
,这就是你想要的。
要启用此功能,您所要做的就是将其添加到创建客户端的代码中:
client = commands.Bot(command_prefix=your_prefix, guild_subscriptions=True)
此外,由于Discord 1.5,您现在需要传递正确的Intents
,而Client的文档指出,如果您不希望guild.owner
返回None
,则需要启用Intents.members
,这也是导致问题的原因。你还必须将这些意图作为一个参数传递,所以最后的结果会是这样的:
# Configure intents (1.5.0)
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=your_prefix, guild_subscriptions=True, intents=intents)