我将如何修复 AttributeError: 'Guild' 对象在 on_guild_join 函数中没有属性 'guild' |不和谐.py

时间:2021-03-13 19:24:32

标签: python discord discord.py

当机器人加入服务器时,我希望它创建角色、类别、频道。但它给了我这个。它是用齿轮写的

它给了我这个错误

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\Jerry\Documents\Dump-Bot\cogs\welcome.py", line 12, in on_guild_join
    guild = ctx.guild
AttributeError: 'Guild' object has no attribute 'guild'

代码:

    @commands.Cog.listener()
    async def on_guild_join(self, ctx):
        guild = ctx.guild
        await ctx.guild.create_role(name="Muted")
        Muted_role = discord.utils.get(member.guild.roles, name="Muted")
        await ctx.Muted_role.set_permissions(send_messages=False)
        verify_role = discord.utils.get(member.guild.roles, name="Member")
        await ctx.verify_role.set_permissions(send_messages=True)
        ticket_category = discord.utils.get(guild.categories, name="Tickets") 

        if ticket_category is None: 
            await guild.create_category_channel(name="Tickets")

        await ctx.guild.create_text_channel(name="welcome")

        guild_join_embed = discord.Embed(title="Thanks for adding me")
        guild_join_embed.add_field(name="Thanks for adding me!", value="It helps alot. I will provide my best support", inline=True)
        guild_join_embed.set_footer(text=f"Right now I am in {len(client.guilds)} guilds/servers")
        await ctx.send(embed=guild_join_embed)
        await ctx.guild.create_voice_channel(name=f"Total Member Count: {guild.member_count}")
        await ctx.guild.create_voice_channel(name=f"Boost Count: {guild.premium_subscription_count}")
        await ctx.guild.create_voice_channel(name=f"Ticket Count: {ticket_category.children.size}") 
        member_count_vc = discord.utils.get(guild.channel, name=f"Total Member Count: {guild.member_count}", category=None)
        server_boost_count_vc = discord.utils.get(guild.channel, name=f"Boost Count: {guild.premium_subscription_count}", category=None)
        ticket_count_vc = discord.utils.get(guild.channel, name=f"Ticket Count: {ticket_category.children.size}", category=None)
        await ctx.member_count_vc.set_permissions(ctx.guild.verify_role, connect=False)
        await ctx.server_boost_count_vc.set_permissions(ctx.guild.verify_role, connect=False)

我该如何解决这个错误?

1 个答案:

答案 0 :(得分:1)

正如您的错误所暗示的那样,您有一个 Guild 的实例,而不是 Context。看看 API docs 还给出:

<块引用>

参数: 公会(Guild)——加入的公会。

这意味着 on_guild_join 没有得到 ctx,它得到的是 Guild(即您刚刚加入的公会)。要“修复此问题”,请删除 .guild您已经拥有公会