Python int对象没有属性 www.example.com

p8ekf7hl  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(164)

我目前正在使用上传到pypi的最新py-cord版本开发一个discord机器人。我试图使一个按钮时,其按下创建一个门票渠道与按钮pressers名称和门票在年底与特定覆盖下的类别“门票”。下面是按钮回调的代码。

class MyView(discord.ui.View):
    @discord.ui.button(label="Create Ticket", style=discord.ButtonStyle.primary, emoji="🎫")
    async def button_callback(self, button, interaction : discord.Interaction):
        supportRole = config.support_role_id
        overwrite = {
            interaction.guild.default_role: discord.PermissionOverwrite(view_channel=False),
            supportRole: discord.PermissionOverwrite(view_channel=True, read_messages=True, send_messages=True, add_reactions=True)
        }
        ticket_creator = interaction.user.display_name
        await interaction.guild.create_text_channel(f"{ticket_creator} ticket", category="tickets", overwrites=overwrite)

接收到的错误说明ID没有属性 www.example.com ,但我不提供任何ID参数在此代码的任何地方。
这是秋天的回溯。

Ignoring exception in view <MyView timeout=180.0 children=1> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Create Ticket' emoji=<PartialEmoji animated=False name='🎫' id=None> row=None>:
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/ui/view.py", line 414, in _scheduled_task
    await item.callback(interaction)
  File "/home/container/bot.py", line 18, in button_callback
    await interaction.guild.create_text_channel(f"{ticket_creator} ticket", category="tickets", overwrites=overwrite)
  File "/home/container/.local/lib/python3.11/site-packages/discord/guild.py", line 1263, in create_text_channel
    data = await self._create_channel(
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.11/site-packages/discord/guild.py", line 1142, in _create_channel
    "id": target.id,
          ^^^^^^^^^
AttributeError: 'int' object has no attribute 'id'
h6my8fg2

h6my8fg21#

经过进一步的诊断,我发现2个问题的代码和固定它。
第一个问题是supportRole,当将它传递到覆盖时,它需要是一个角色,而不仅仅是ID。
我通过更改重写代码来获取角色来修复此问题。

overwrite = {
            interaction.guild.default_role: discord.PermissionOverwrite(view_channel=False),
            interaction.guild.get_role(supportRole): discord.PermissionOverwrite(view_channel=True, read_messages=True, send_messages=True, add_reactions=True)
        }

第二个问题是类别部分,当试图在类别下创建票证时,它也试图获取类别ID,但当它只是一个字符串时失败,通过获取类别然后通过create_text_channel函数传递来修复此问题。

class MyView(discord.ui.View):
    @discord.ui.button(label="Create Ticket", style=discord.ButtonStyle.primary, emoji="🎫")
    async def button_callback(self, button, interaction : discord.Interaction):
        supportRole = config.support_role_id
        overwrite = {
            interaction.guild.default_role: discord.PermissionOverwrite(view_channel=False),
            interaction.guild.get_role(supportRole): discord.PermissionOverwrite(view_channel=True, read_messages=True, send_messages=True, add_reactions=True)
        }
        ticket_creator = interaction.user.display_name
        category = discord.utils.get(interaction.guild.channels, name="tickets", type=discord.ChannelType.category)
        new_ticket = await interaction.guild.create_text_channel(f"{ticket_creator} ticket", category=category, overwrites=overwrite)
        await new_ticket.send(f"Hello {interaction.user.mention}! Welcome to your ticket! An <@&{supportRole}> member will be with you soon! While you wait, can you please describe your issue.", view=AnotherView())
        await interaction.response.send_message("Ticket Created!", ephemeral=True)

相关问题