首页 > 解决方案 > 我的机器人的一个功能只有在我的电脑上运行时才起作用

问题描述

我的目标是在您对特定消息上的特定表情符号做出反应时,用您的名字创建一个频道,. 我实际上已经尝试过:

@bot.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    if message_id == **************:
        guild = bot.get_guild(payload.guild_id)
        user = bot.get_user(payload.user_id)
        print(is_user(payload.user_id))
        if is_user(payload.user_id):
            return
        chan = await guild.create_text_channel(user.name)
        await chan.set_permissions(user, read_messages=True,send_messages=True)
        new_user(payload.user_id,chan.id)
        await chan.send(f"hi there {user.mention}")

但此代码仅适用于我的 PC:当我在我的树莓派(或带有 github 操作的 github)上运行它时,此功能根本不起作用。我得到了回溯:

Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "****.py", line 336, in on_raw_reaction_add
    chan = await guild.create_text_channel(user.name)
AttributeError: 'NoneType' object has no attribute 'name'

异常结果:创建了一个以做出反应的人的姓名的频道。

结果:如图所示的回溯

我的电脑在 Windows 10 上,我的树莓派在 raspbian 上运行,在 ubuntu 上运行 github 操作

标签: pythonpython-3.xdiscorddiscord.py

解决方案


问题是,您的机器人无法将用户从缓存中取出(这不仅是您的机器人的问题),因此最简单的方法是从不和谐中获取它(但您不应该这样做)。修改:

@bot.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    if message_id == **************:
        guild = bot.get_guild(payload.guild_id)
        user = await bot.fetch_user(payload.user_id)
        
        print(is_user(payload.user_id))
        
        if is_user(payload.user_id):
            return
        chan = await guild.create_text_channel(user.name)
        await chan.set_permissions(user, read_messages=True, send_messages=True)
        new_user(payload.user_id, chan.id)

        await chan.send(f"hi there {user.mention}")

推荐阅读