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

时间:2021-01-07 11:39:05

标签: python discord

这是我上一个问题 (AttributeError: 'NoneType' object has no attribute 'count' discord.py) 的更新

问题是 Unicode 表情符号使 emoji_count = ... 行产生错误。这是因为 Unicode 表情符号返回 NoneNoneType,所以 emoji = emoji 也返回 None,因此整个函数会产生错误。

代码:

@bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
msg = await channel.fetch_message(payload.message_id)
emoji = payload.emoji
author = payload.member
emoji_count = discord.utils.get(msg.reactions, emoji=emoji).count
if payload.channel_id == channel_play:
    if author in buffer.members:
        if int(emoji_count) > 1:
            ...
            ...
            await msg.remove_reaction(emoji, author)

错误:

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 125, in on_raw_reaction_add
emoji_count = discord.utils.get(msg.reactions, emoji = emoji).count
AttributeError: 'NoneType' object has no attribute 'count'

我该如何解决这个问题,让自定义和 Unicode 表情符号都能正常工作?代码中应该写什么?

1 个答案:

答案 0 :(得分:1)

    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