【问题标题】:AttributeError: 'NoneType' object has no attribute 'count' - discord.pyAttributeError:'NoneType' 对象没有属性'count' - discord.py
【发布时间】:2021-04-13 03:20:12
【问题描述】:

这是我上一个问题的更新 (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 表情符号都能正常工作?代码应该写什么?

【问题讨论】:

    标签: python discord


    【解决方案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
    

    【讨论】:

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