简体   繁体   中英

Polling system on Discord.py

@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)

I have this piece of code that makes my discord.py bot create a poll. When I trigger the command, it doesn't do anything. No errors, no messages, nothing. I am thinking that some part of my code could be outdated. No the code is not under a class.

You're still using the old documentation when running on a more recent version of d.py.

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

The most notable changes in your case would be:

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)

And for editing the message:

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

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

And there's also the old syntax you're using for adding reactions, but you can find the documentation for adding reactions here - same concept as above: Message.add_reaction() .

It's important to make sure what version of a library you're using, as it'll prevent you from running into syntactical errors and mishaps like this in the future. From now on, don't refer to the old documentation at all, only the new docs should be used.


References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM