Search code examples
pythondiscordpycord

Python int object has no attribute target.id


I am currently working on an discord bot using the latest py-cord version uploaded to pypi. I am trying to make an button when its pressed creates an ticket channel with the button pressers name and ticket at the end with specific overwrites under the category "tickets". Here is the code for the button callback.

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)

The error that is being received is talking about an ID not having the attribute target.id but I am not supplying any ID param anywhere in this code.
Here is the fall traceback.

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'

Solution

  • After further diagnostics I found 2 issues with the code and fixed it.

    The first issue was with the supportRole, when passing it into the overwrites it needs to be an role and not just the ID.

    I fixed this by changing the overwrites code to get the role.

            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)
            }
    

    Seconds issue was with the category part, when trying to create the ticket under the category it also was trying to get the category ID but failed when it was just an string, fixed this by getting the category then passing that through the create_text_channel function.

    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)