【问题标题】:Polling system on Discord.pyDiscord.py 上的轮询系统
【发布时间】:2020-06-09 19:21:17
【问题描述】:
@client.command()
async def quickpoll( ctx, question, *options: str):
    if len(options) <= 1:
        await ctx.send('You need more than one option to make a poll!')
        return
    if len(options) > 10:
        await ctx.send('You cannot make a poll for more than 10 things!')
        return


    if len(options) == 2 and options[0] == 'yes' and options[1] == 'no':
        reactions = ['✅', '❌']
    else:
        reactions = ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '????']

    description = []
    for x, option in enumerate(options):
        description += '\n {} {}'.format(reactions[x], option)
    embed = discord.Embed(title=question, description=''.join(description))
    react_message = await ctx.send(embed=embed)
    for reaction in reactions[:len(options)]:
        await react_message.edit(react_message, reaction)
    embed.set_footer(text='Poll ID: {}'.format(react_message.id))
    await react_message.edit(embed=embed)

我有这段代码可以让我的 discord.py 机器人创建一个投票。当我触发命令时,它什么也不做。没有错误,没有消息,什么都没有。我在想我的代码的某些部分可能已经过时了。不,代码不在一个类下。

【问题讨论】:

  • 你能做出一个完全有效的命令吗?如果是这样,请尝试逐步添加内容以处理所需的 poll 命令,直到找到导致问题的部分。

标签: python discord.py


【解决方案1】:

在更新版本的 d.py 上运行时,您仍在使用旧文档。

Recent docs
Major changes from v0.16.x -> v1.x

您的案例中最显着的变化是:

await self.bot.say(... # <- OLD SYNTAX

# await abc.Messageable.send(... <- general usage for NEW SYNTAX
await ctx.send(... # for your case specifically (which you have used at the top)

对于编辑消息:

await self.bot.edit_message(... # <- OLD SYNTAX

# await Message.edit(... <- general usage for NEW SYNTAX
await react_message.edit(... # for your case specifically

还有您用于添加反应的旧语法,但您可以在此处找到添加反应的文档 - 与上述概念相同:Message.add_reaction()

确定您使用的是什么版本的库很重要,因为它可以防止您在未来遇到类似的语法错误和事故。从现在开始,不要再参考旧文档了,只能使用新文档。


参考资料:

【讨论】:

  • 我创建的民意调查似乎可以回答,但没有添加任何反应。我编辑了问题
  • 看看你能不能解决它——看看Message.add_reaction()提示:您已经获得了要添加反应的消息对象,它的语法与您编辑消息的方式相似。
  • 当我像纪录片所说的那样改变它时,程序无法知道 add_reaction 是什么
  • await react_message.add_reaction(reaction) - 这是一个协程,所以你需要等待它。您只能在存储在react_message 中的消息对象上使用add_reaction()。协程采用 one 参数,即表情符号 - 您将其存储为 reaction
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 2013-03-28
  • 2013-03-22
  • 2020-09-26
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多