簡體   English   中英

AttributeError: 'NoneType' object 沒有屬性 'id'_

[英]AttributeError: 'NoneType' object has no attribute 'id'_

這是我在機器人中的代碼

@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()

這是我在終端的錯誤

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'

請幫助我,我自己無法解決這個錯誤,我使用 Python 39

不是最理想的方式,但我想你可以使用這個:

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

它基本上首先檢查after是否為none,如果不是,那么如果after,channel是none? 如果不是,那么它將引用它的 id

如果成員在此更新后(不再)在語音頻道中,則after.channel值為 None ,如此所述。

您將不得不以某種方式處理這種情況,也許什么都不做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM