python - 机器人不会在消息中添加表情符号
问题描述
我是 Python 的新手,并尝试为我的不和谐机器人添加反应角色,但我的机器人成功发送消息,但不要在消息中添加表情符号。
@client.event
async def on_message(message):
if message.content.lower().startswith("!lol"):
embed1 = discord.Embed(
title="Escolha seu Elo!",
color=COR,
description="- Ferro = \n"
"- Bronze = \n"
"- Prata = \n"
"- Ouro = \n"
"- Platina = \n"
"- Diamante = ✋\n"
"- Mestre = \n"
"- Grã-Mestre = ✊\n"
"- Desafiante = \n",)
botmsg = await message.channel.send(embed=embed1)
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "✋")
await ctx.message.add_reaction(botmsg, "")
await ctx.message.add_reaction(botmsg, "✊")
await ctx.message.add_reaction(botmsg, "")
global msg_id
msg_id = botmsg.id
global msg_user
msg_user = message.author
这是我得到的错误我已经尝试了很多解决方法我找不到任何可以解决这个问题的方法
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 55, in on_message
botmsg = await message.channel.send(embed=embed1)
UnboundLocalError: local variable 'embed1' referenced before assignment
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 57, in on_message
await ctx.message.add_reaction(botmsg, "")
NameError: name 'ctx' is not defined
我也试过使用
emoji = client.get_emoji(310177266011340803)
await message.add_reaction(emoji)
但这也行不通
upd:这些表情符号是示例,我想同时使用 unicode 表情符号和自定义表情符号
解决方案
尝试这个:
@client.event
async def on_message(message):
if message.content.lower().startswith("!lol"):
embed1 = discord.Embed(
title="Escolha seu Elo!",
color=COR,
description="- Ferro = \n"
"- Bronze = \n"
"- Prata = \n"
"- Ouro = \n"
"- Platina = \n"
"- Diamante = ✋\n"
"- Mestre = \n"
"- Grã-Mestre = ✊\n"
"- Desafiante = \n",)
botmsg = await message.channel.send(embed=embed1)
await botmsg.add_reaction("")
await botmsg.add_reaction("")
await botmsg.add_reaction("")
await botmsg.add_reaction("")
await botmsg.add_reaction("")
await botmsg.add_reaction("✋")
await botmsg.add_reaction("")
await botmsg.add_reaction("✊")
await botmsg.add_reaction("")
global msg_id
msg_id = botmsg.id
global msg_user
msg_user = message.author
on_message
调用时不传递上下文 arg 。而不是ctx.message.add_reaction()
你可以使用message.add_reaction()
.
编辑:哦,我了解您的全部问题,现在脚本应该对您的机器人消息做出反应。
推荐阅读
- java - Increase a Counter using 2 threads
- python-3.x - 将事件和边框宽度绑定到列表框条目
- google-apps-script - 我可以设置一个函数/脚本,当另一个单元格更新时将在单元格中返回一个日期 - Google 表格?
- python - 使用 Pygame 为网格中的正方形着色,但只能为第一个框着色,不知道该怎么做
- rust - 没有多个按钮系统的多个按钮?
- javascript - NPM 错误:必须是实际的 JSON,而不仅仅是 JavaScript
- ios - Flutter - iOS - 将用户名和密码保存到 webcredentials 以进行自动填充
- jquery - 在滚动时,在 x 像素后显示,并在 x 像素后移除?
- python - Matplotlib 根据另一个滑块的变化更新一个滑块
- css - lst是什么字体?