【发布时间】:2021-09-04 17:52:15
【问题描述】:
我正在开发一个用于创建投票的不和谐机器人,我的代码在这里:
@bot.command(name="newpoll")
async def new_poll(ctx, question, *options):
if len(options) > 12:
await ctx.send("You can have a maximum of 12 choices in your poll")
else:
embed = discord.Embed(title = "Poll",
description = question,
colour = discord.Colour.red())
fields = [("Options", "\n".join([f"{emotes[idx]} {option}" for idx, option in enumerate(options)]), False),
("Instructions", "Please react in order to vote!", False)]
for name, value, inline in fields:
embed.add_field(name = name, value = value, inline = inline)
embed = embed.add_field(name = "Total votes", value = 0, inline = False)
message = await ctx.send(embed = embed)
for emoji in emotes[:len(options)]:
await message.add_reaction(emoji)
message_win = await bot.get_channel(message.channel.id).fetch_message(message.id)
total_votes = sum(reaction.count for reaction in message_win.reactions) - len(options)
tv_embed = embed.set_field_at(2, name = "Total votes", value = total_votes, inline = False)
await message.edit(embed = tv_embed)
总投票数存储在total_votes 变量中,我计划通过编辑嵌入消息(如最后两行)来显示该变量。我希望在发生反应时发生这种情况。关于这一点,我尝试使用类似于以下的代码访问tv_embed:
@bot.event
async def on_reaction_add(reaction, user):
print("planning to ember.set_field_at(...) here")
from_new_poll = await new_poll.tv_embed
from_new_polll = await new_poll.message
await from_new_polll.edit(embed = from_new_poll)
但我得到类似这样的错误:
planning to ember.set_field_at(...) here
Ignoring exception in on_reaction_add
Traceback (most recent call last):
File "/home/vale/.virtualenvs/discord_env/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/vale/Documents/arcane/e_project-kindling/Discord-Bot/main.py", line 108, in on_reaction_add
from_new_poll = await new_poll.tv_embed
AttributeError: 'Command' object has no attribute 'tv_embed'
谁能解释一下这里发生了什么,提前谢谢!
【问题讨论】:
标签: python discord.py python-asyncio