如何在“on_guild_join”事件上使用覆盖?

How do i use overwrites on an "on_guild_join" event?

提问人:zoomingspeed 提问时间:11/13/2023 最后编辑:zoomingspeed 更新时间:11/14/2023 访问量:46

问:

我有我的 Discord 机器人的代码,当它加入公会时,它会向每个频道发送一条消息,并嵌入自我介绍。但是,这使我的票证命令不起作用,因为它给了我错误错误,所以我想出了一个解决方案来创建一个无权向频道发送消息的静音角色,但我坚持进行覆盖。如何对事件设置覆盖权限?NoneType has no attribute "id"on_guild_join

这是我的代码:

# on guild join
@bot.event
async def on_guild_join(guild):
    role = discord.utils.get(guild.roles, name="Muted")
    if role == None:
        permissions = ""
        overwrites = {
             permissions: discord.PermissionOverwrite(send_messages=False)
        }
        role2 = await guild.create_roles(name="Muted", overwrites=overwrites)
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).send_messages:
                try:
                    embed=discord.Embed(title="Introduction",
                                    description="**السلام عليكم!**",
                                    color=discord.Color.blurple())
                    embed.add_field(name='**What is Sheikh Bot?**', value='Sheikh Bot is a bot that is used for moderation and Islamic purposes. To get started, please run ".help"', inline=False)
                    await channel.send(embed=embed)
                    print(f"Bot has joined guild {guild} successfullym and sent a message to {channel}.")
                except Exception as e:
                    print(f"Bot has ran into an error while running on_guild_join function: {e}")
            else:
                print(f"Bot succesfully joined {guild} but could not send a message to {channel}.")
    else:
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).send_messages:
                try:
                    embed=discord.Embed(title="Introduction",
                                    description="**السلام عليكم!**",
                                    color=discord.Color.blurple())
                    embed.add_field(name='**What is Sheikh Bot?**', value='Sheikh Bot is a bot that is used for moderation and Islamic purposes. To get started, please run ".help"', inline=False)
                    await channel.send(embed=embed)
                    print(f"Bot has joined guild {guild} successfullym and sent a message to {channel}.")
                except Exception as e:
                    print(f"Bot has ran into an error while running on_guild_join function: {e}")

更新:我尝试使用上面的代码(已编辑),但它给了我这个错误:

'Guild' object has no attribute 'create_roles'
python discord.py python-3.11

评论


答:

1赞 Raymus 11/13/2023 #1

您遇到的错误是因为不和谐。公会没有属性“create_roles”。基本上,你试图使用一些不存在的东西。 要在公会中创建角色,请使用Guild.create_role

role2 = await guild.create_role(name="Muted",
                                 permissions=discord.Permissions(send_messages=False))

有关详细信息,请阅读文档

评论

0赞 zoomingspeed 11/14/2023
好吧,也许你没有看清楚,因为它说guild.create_role() got an unexpected keyword argument 'overwrites'
0赞 Raymus 11/14/2023
我已经编辑了我的答案,我错误地把它留在那里。您可以在此处Guild.create_role查看其参数。
0赞 zoomingspeed 11/15/2023
好的,非常感谢,这对我帮助很大:)