python - 我的机器人的一个功能只有在我的电脑上运行时才起作用
问题描述
我的目标是在您对特定消息上的特定表情符号做出反应时,用您的名字创建一个频道,. 我实际上已经尝试过:
@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 操作
解决方案
问题是,您的机器人无法将用户从缓存中取出(这不仅是您的机器人的问题),因此最简单的方法是从不和谐中获取它(但您不应该这样做)。修改:
@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}")
推荐阅读
- linux - 在linux中更改子网掩码
- android - 如何在 ListView 的字符串数组中显示 HTML 文本
- arrays - Angular 8 为什么承诺返回一个未定义的值,尽管它在解决之前得到了适当的安慰?
- schema.org - Schema.org - 嵌套事件
- python - 找出两个字典是否导致循环
- sql-server - SQL 错误 超出最大存储过程、函数、触发器或视图嵌套级别
- css - 滚动条不适用于父级的最大高度
- python - 如何使用谷歌colab中的python Open CV从文件夹中检索图像以测试模型的性能并将所有图像打印到csv文件中
- php - 不使用 group by、distinct 或使用 php 计数的 mysql 查询计数
- google-cloud-platform - 将 json 文件加载到 bigquery 的云功能