【问题标题】:How can I add a message and add a reaction to that message in Discord.py如何在 Discord.py 中添加消息并对该消息添加反应
【发布时间】:2021-02-28 16:43:45
【问题描述】:

我想通过discord.py 发送消息和反应(尝试使用我自己的 BOT 创建投票),如下所示:

这是可以做到的:

使用$poll,只需调用该函数。 参数:"question" "option1" "option2" ... 至 10 个选项。如果用户发送超过 10 个,则忽略它们。

我可以添加粗体文本,引用一些文本(选项部分),并在文本中插入一个表情符号

但是!我可以对消息本身添加一个反应,让用户提出他们的意见。

import os
from discord.utils import get
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

bot = commands.Bot(command_prefix='$')

@bot.command(name='poll', help='Create a Poll!')
async def poll(ctx, q : str, *opt):
    bar = ":bar_chart: "
    qu = "> "
    e = "\n"
    c = 0
    opts = ""
    l = [":regional_indicator_a:",
         ":regional_indicator_b:",
         ":regional_indicator_c:",
         ":regional_indicator_d:",
         ":regional_indicator_e:",
         ":regional_indicator_f:",
         ":regional_indicator_g:",
         ":regional_indicator_h:",
         ":regional_indicator_i:",
         ":regional_indicator_j:",
         ":regional_indicator_k:"]
    new_l = [] #This is just to store the name from :name: to name
    question = bar + '**' + q + '**' + e
    for i in opt:
        opts = opts + qu + l[c] + ' ' + i + (e if c <= 9 else "")
        p = str(l[c])
        new_l.append(p[1:-1]) #Here from :name: to name 
        c = c + 1
        if c == 10: break
    response = question + opts
    m = await ctx.send(response)

    # Here is where the error comes... When the user adds the options, 
    #I need to add that emoji as a reaction 
    #(:regional_indicator_a:, and so on, to 10) to 
    #led the user make the vote. 
    #BUT! As you can see, i can not add the reaction! 
    for j in new_l:
        emoji = get(ctx.guild.emojis, name=j)
        await m.add_reaction(emoji)


@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')
bot.run(TOKEN)

这是错误的一部分:

Command raised an exception: InvalidArgument: emoji argument must be str, Emoji, or Reaction not NoneType.

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    添加表情符号时,您需要使用实际的表情符号,例如?,您可以通过在不和谐中执行\:emoji_name: 并发送消息来获得:

    变成:

    然后您可以将其直接复制到您的 IDE 中。

    或者,如果您的 IDE 不支持表情符号,您可以使用 unicode,您可以从 this 站点找到它。例如,条形图的 unicode 为 \u1f4ca,因为 python 中的格式遵循 \u 作为前缀的格式,然后是其 unicode。

    这意味着您的表情符号列表将类似于 ["?", "?", "?", ....["\u1f1e6", "\u1f1e7", "\u1f1e8", ....

    【讨论】:

    • 我想使用 A、B、C,正如您在图片中看到的索引项(例如:regional_indicator_a:)。如何获取该表情符号/反应的 ID 或 Unicode?​​span>
    • 我将它们作为示例包含在列表的末尾。您可以从我链接的网站上获取它们并搜索表情符号的名称。
    【解决方案2】:

    尝试使用表情符号模块。

    import emoji
    

    它具有使聊天中的表情符号“表情符号”的功能

    await add_reaction(emoji.emojize(x)) # where x is the emoji in your chat that your code recieve
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2019-09-06
      • 2020-10-27
      • 2020-11-07
      • 2022-01-14
      相关资源
      最近更新 更多