0

I'm trying to make a discord bot for my server, and create a channel for modmail. But every time I try creating one, this error appears. I'm using python 3.10

  File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\cogs\modmail.py", line 35, in on_message  
    channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
  File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py", 
line 948, in create_text_channel
    data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
  File "C:\Users\dyimi\Desktop\Code related stuff\python\discord..py\Palace Guard\venv\lib\site-packages\discord\guild.py", 
line 844, in _create_channel
    'id': target.id
AttributeError: 'str' object has no attribute 'id'

Here's my code:

@commands.Cog.listener()
    async def on_message(self, message):
        if message.content.lower() in ["yes", "y", "ya", "yee", "ye", "no", "n", "nope"]:
            return
        
        if message.author == self.client.user:
            return
        
        def check(msg):
            return msg.content.lower() in ["yes", "y", "ya", "yee", "ye"] and msg.author == message.author
        if isinstance(message.channel, discord.channel.DMChannel):
            await message.channel.send("Create ticket to modmail in Veemo's Kingdom? Yes or no.")
            response = await self.client.wait_for("message", check=check)
        if response:
            guild = discord.utils.get(self.client.guilds, id=834666434042658816)
            text_channel_name = message.author.name + "-" + message.author.discriminator
            roles = guild.roles
            overwrites = {
                guild.default_role: discord.PermissionOverwrite(view_channel=False)
            }
            for role in roles:
                if role.name not in mod_roles_list:
                    overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)
            channel = await guild.create_text_channel(name=text_channel_name, overwrites=overwrites)
        else:
            await message.channel.send("Ticket not created.")

1 Answer 1

1

I think the issue here is with the overwrites dictionary variable,

Below line appends the overwrites variable with keys as str instead of discord.py Role objects. Which causes this AttributeError: 'str' object has no attribute 'id' error.

overwrites[role.name] = discord.PermissionOverwrite(view_channel=False)

Output with issue:

{
  <Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
  '@everyone': <discord.permissions.PermissionOverwrite object at ...>,
  'Bot': <discord.permissions.PermissionOverwrite object at ...>,
  'pbot': <discord.permissions.PermissionOverwrite object at ...>
}

It should be like below, so the key is discord.py Role object which contains the attribute id.

overwrites[role] = discord.PermissionOverwrite(view_channel=False)

Output:

{
  <Role id=<id no> name='@everyone'>: <discord.permissions.PermissionOverwrite object at ...>,
  <Role id=<id no> name='Bot'>: <discord.permissions.PermissionOverwrite object at ...>,
  <Role id=<id no> name='pbot'>: <discord.permissions.PermissionOverwrite object at ...>
}
Sign up to request clarification or add additional context in comments.

1 Comment

A bit overcomplicated (you could just say that discord.py expects a Role / User / Member as the key) but the fix is correct so +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.