AttributeError: 'NoneType' 对象没有属性 'name' discord.py

时间:2021-02-02 18:22:41

标签: python discord

Part of the code:

def change_1_letter(word, l, r):
word_a = ""
for s in list(word):
    if s != l:
        word_a = word_a + s
    if s == l:
        word_a = word_a + r
return word_a

@bot.event
async def on_raw_reaction_add(payload):
    channel = bot.get_channel(payload.channel_id)
    guild = bot.get_guild(769858357607399443) # it is a valid id, this line does not return `None`
    if emoji.is_custom_emoji():
        emoji_count = discord.utils.get(msg.reactions, emoji=emoji).count
    else:
        emoji_count = discord.utils.get(msg.reactions, emoji = emoji.name).count
    if int(emoji_count) > 1:
        await msg.remove_reaction(emoji, author)
        if emoji == bot.get_emoji(771860972340117545):
            game = "Among Us"
            max_pl = 10
        for ch in guild.voice_channels:
            if change_1_letter(game.lower(), " ", "-") in ch.category.name and "lobby" in\ 
            ch.name.lower() and not len(ch.members) > max_pl:
                channel_party = ch
                break
            await author.move_to(channel_party)

错误:

Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", 
line 312, in _run_event
await coro(*args, **kwargs)
File "C:\Users\plays\OneDrive\Рабочий стол\Python\bot2.py", line 146, in on_raw_reaction_add
if change_1_letter(game.lower(), " ", "-") in ch.category.name and "пати" in ch.name.lower() and 
not len(ch.members) > max_pl:
AttributeError: 'NoneType' object has no attribute 'name'

当您添加新反应时,该功能会将您移动到满足所有条件的公会第一个频道。所有频道都在名称类似于 game 变量的类别中,其中带有 - 而不是 (discord 中的类别名称中没有空格)。

出现错误是因为change_1_letter(game.lower(), " ", "-") in ch.category.name,没有它,函数正常工作。它以前可以工作,但是当我使机器人变得更复杂时,它停止了运行。我不知道是哪部分代码导致此函数运行出错,因为我很长时间没有测试这部分代码。

SFMBE。

1 个答案:

答案 0 :(得分:0)

问题是 ch.category 返回 None,因为其中一个语音频道不在任何类别中,所以当机器人检查这个频道时,他(它)从None

解决方案可以是:

ch.category