【问题标题】:Bot don't add emoji to message机器人不会在消息中添加表情符号
【发布时间】:2021-10-12 04:51:13
【问题描述】:

我是 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 表情符号和自定义表情符号

【问题讨论】:

    标签: python discord.py bots roles


    【解决方案1】:

    试试这个:

    @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 时不传递上下文参数。您可以使用message.add_reaction() 而不是ctx.message.add_reaction()
    编辑:哦,我了解您的全部问题,现在脚本应该对您的机器人消息做出反应。

    【讨论】:

    • 谢谢你,你是最棒的
    猜你喜欢
    • 2021-03-26
    • 2020-06-29
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2020-09-23
    相关资源
    最近更新 更多