【问题标题】:AttributeError: 'NoneType' object has no attribute 'name' discord.pyAttributeError:'NoneType' 对象没有属性'name' discord.py
【发布时间】:2021-05-06 23:22:26
【问题描述】:
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。

【问题讨论】:

  • 好像ch.categoryNone

标签: python discord


【解决方案1】:

问题在于ch.category 正在返回None,因为其中一个语音通道不在任何类别中,因此当机器人检查此通道时,他(它)从ch.category 得到None

解决办法可以是:

for ch in guild.voice_channels:
     if not ch.category == None:
         if change_1_lett...:
             channel_party = ch
             break

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多