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

Viewed 1172

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

2 Answers

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

The after.channel value is None if the member is not in a voice channel (anymore) after this update, as documented here.

You will have to handle that case somehow, perhaps by doing nothing:

if after.channel and after.channel.id == &&&&&&&&&&&&&&&&&&:
    print(f'{member} зашёл в канал')
Related