Jamie
Jamie

Reputation: 101

Discord NoneType Object Error within discord files?

I have been messing around with discord bots for quite a while now and I ran into this error today. I wanted to see if anyone can help me figure this out.

The error will be posted at the bottom as well as my event that is running.

I am making a ticket system for my discord server, I have found that I can not do this without using the On_Raw_Reaction_Add Event. The payload seems to be very confusing to me but I got almost everything to work. The last thing left is the guild. For some reason when referencing/pulling the guild using the below snip, within guild.py on discords import, it says guild is considered a NoneType but if I print it from the event, it works just fine. I just want to create a channel.

Snip

guild = self.client.get_guild(reactionpayload.guild_id)
    @commands.Cog.listener()
    async def on_raw_reaction_add(self, reactionpayload):
        print('ran')
        db = sqlite3.connect(DATA_FILENAME)
        cursor = db.cursor()
        cursor.execute(f"SELECT ID FROM Ticket_Channel")
        channelID = cursor.fetchone()[0]
        cursor.close()
        channel = self.client.get_channel(int(channelID))
        if channel is None:
            print('Ticket System: Channel not Found!')
        else:
            if reactionpayload.message_id == channel.last_message_id:
                if not self.client.get_user(reactionpayload.user_id).bot:
                    guild = self.client.get_guild(reactionpayload.guild_id)
                    print(guild.id)
                    name = 'ticket-' + str(self.client.get_user(reactionpayload.user_id).name)
                    overwrites = {
                        guild.default_role: discord.PermissionOverwrite(read_messages=False),
                        guild.me: discord.PermissionOverwrite(read_messages=True),
                        guild.get_member(self.client.get_user(reactionpayload.user_id)): discord.PermissionOverwrite(read_messages=True)
                    }
                    channel = await guild.create_text_channel(name, overwrites=overwrites, category=self.client.get_channel(self.client.get_channel(reactionpayload.channel_id).category_id), position=100)
                    await remove_reaction('\U0001F4E9', self.client.get_user(reactionpayload.user_id))
            else:
                print(reactionpayload.message_id)
                print(channel.last_message_id)
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 270, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\gunzb\Desktop\AkumaBot\cogs\ticket_system.py", line 41, in on_raw_reaction_add
    channel = await guild.create_text_channel(name, overwrites=overwrites, category=self.client.get_channel(self.client.get_channel(reactionpayload.channel_id).category_id), position=100)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\guild.py", line 882, in create_text_channel
    data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\guild.py", line 785, in _create_channel
    'id': target.id
AttributeError: 'NoneType' object has no attribute 'id'

Upvotes: 1

Views: 423

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

get_member takes an id, not a User object, so guild.get_member(self.client.get_user(reactionpayload.user_id)) is returning None.

@commands.Cog.listener()
async def on_raw_reaction_add(self, reactionpayload):
    if not reactionpayload.guild_id:
        return  # Private message
    print('ran')
    db = sqlite3.connect(DATA_FILENAME)
    cursor = db.cursor()
    cursor.execute(f"SELECT ID FROM Ticket_Channel")
    channelID = cursor.fetchone()[0]
    cursor.close()
    channel = self.client.get_channel(int(channelID))
    if channel is None:
        print('Ticket System: Channel not Found!')
    else:
        if reactionpayload.message_id == channel.last_message_id:
            if not self.client.get_user(reactionpayload.user_id).bot:
                guild = self.client.get_guild(reactionpayload.guild_id)
                member = guild.get_member(reactionpayload.user_id)
                print(guild.id)
                name = 'ticket-' + member.name
                overwrites = {
                    guild.default_role: discord.PermissionOverwrite(read_messages=False),
                    guild.me: discord.PermissionOverwrite(read_messages=True),
                    member: discord.PermissionOverwrite(read_messages=True)
                }
                channel = await guild.create_text_channel(name, overwrites=overwrites, category=channel.category, position=100)
                await remove_reaction('\U0001F4E9', self.client.get_user(reactionpayload.user_id))
        else:
            print(reactionpayload.message_id)
            print(channel.last_message_id)

Upvotes: 2

Related Questions