如何在Discord.py中添加消息并对该消息添加反应

时间:2020-06-02 06:33:07

标签: python discord.py

我想发送消息并与discord.py进行反应(尝试使用我自己的BOT创建民意调查),如下所示:

enter image description here

这是可以的:

enter image description here

使用$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.

2 个答案:

答案 0 :(得分:0)

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

enter image description here

变成:

enter image description here

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

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

这意味着您的表情符号列表看起来像["?", "?", "?", ....["\u1f1e6", "\u1f1e7", "\u1f1e8", ....

答案 1 :(得分:0)

尝试使用表情符号模块。

import emoji

它有一个功能可以让“emojize”成为聊天中的表情符号

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