Search code examples
pythonpython-3.9

AttributeError: 'NoneType' object has no attribute 'id'_


here is my code in the bot

@Bot.event
async def on_voice_state_update(member,before,after): #Создвние войс комнат
    if after.channel.id == &&&&&&&&&&&&&&&&&&:
        print(f'{member} зашёл в канал')
        for guild in Bot.guilds:
            maincategory = discord.utils.get(guild.categories, id=$$$$$$$$$$$$$$$$$$)
            channel2 = await guild.create_voice_channel(name=f'{member.display_name}`s Channel',category = maincategory)
            await channel2.set_permissions(member,connect=True,mute_members=True,move_members=True,manage_channels=True)
            await member.move_to(channel2)
            def check(x,y,z):
                return len(channel2.members) == 0
            await Bot.wait_for('voice_state_update',check=check)
            await channel2.delete()

And this is my error in the terminal

Traceback (most recent call last):
  File "C:\Users\Макс\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Макс\DRACULA-rms\bot.py", line 108, in on_voice_state_update
    if after.channel.id == &&&&&&&&&&&&&&&&&&&&:
AttributeError: 'NoneType' object has no attribute 'id'

Please help me please, I can not solve this error myself, I use Python 39


Solution

  • not the most optimal way, but i guess you can use this:

    if after:
        if after.channel:
                 if after.channel.id == &&&&&&&&&&&&&&&&&&:
                 //Your code here
    

    It basically first checks if after is none, if its not then if after,channel is none? if its not then it will refer to it's id