【发布时间】: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