简体   繁体   中英

Poll command discord.py

DISCORD.PY

I try to create an command for a poll system and encounter a problem. Command is as follows:

@commands.command(pass_context = True)
async def poll(self, ctx, question, *options: str):
    author = ctx.message.author
    server = ctx.message.server

    if not author.server_permissions.manage_messages: return await self.bot.say(DISCORD_SERVER_ERROR_MSG)

    if len(options) <= 1:
        await self.bot.say("```Error! A poll must have more than one option.```")
        return
    if len(options) > 2:
        await self.bot.say("```Error! Poll can have no more than two options.```")
        return

    if len(options) == 2 and options[0] == "yes" and options[1] == "no":
        reactions = ['👍', '👎']
    else:
        reactions = ['👍', '👎']

    description = []
    for x, option in enumerate(options):
        description += '\n {} {}'.format(reactions[x], option)

    embed = discord.Embed(title = question, color = 3553599, description = ''.join(description))

    react_message = await self.bot.say(embed = embed)

    for reaction in reactions[:len(options)]:
        await self.bot.add_reaction(react_message, reaction)

    embed.set_footer(text='Poll ID: {}'.format(react_message.id))

    await self.bot.edit_message(react_message, embed=embed)

My question is : How can I make the question I ask using the command to have more words. If I use more words now, I read them as options and get an error.

Ex 1 : /poll You are human yes no (only read "you" as a question, and the rest are options.)

Ex 2 : /poll You are human yes no (that's what I want)

Thank you!

调用命令时,将字符串放在引号中会导致它被视为一个参数:

 /poll "You are human" yes no

You can do:

@bot.command()
async def poll(ctx, question, option1=None, option2=None):
  if option1==None and option2==None:
    await ctx.channel.purge(limit=1)
    message = await ctx.send(f"```New poll: \n{question}```\n**✅ = Yes**\n**❎ = No**")
    await message.add_reaction('❎')
    await message.add_reaction('✅')
  elif option1==None:
    await ctx.channel.purge(limit=1)
    message = await ctx.send(f"```New poll: \n{question}```\n**✅ = {option1}**\n**❎ = No**")
    await message.add_reaction('❎')
    await message.add_reaction('✅')
  elif option2==None:
    await ctx.channel.purge(limit=1)
    message = await ctx.send(f"```New poll: \n{question}```\n**✅ = Yes**\n**❎ = {option2}**")
    await message.add_reaction('❎')
    await message.add_reaction('✅')
  else:
    await ctx.channel.purge(limit=1)
    message = await ctx.send(f"```New poll: \n{question}```\n**✅ = {option1}**\n**❎ = {option2}**")
    await message.add_reaction('❎')
    await message.add_reaction('✅')

but now you must do /poll hello-everyone text text if you want to to not have the "-" you must do:

@bot.command()
async def poll(ctx, *, question):
    await ctx.channel.purge(limit=1)
    message = await ctx.send(f"```New poll: \n✅ = Yes**\n**❎ = No**")
    await message.add_reaction('❎')
    await message.add_reaction('✅')

But in this one you can't have your own options...

use :

/poll "You are human" yes no

here, "You are human" is one string "yes" "no " are other two strings.

another better way to do it is:

@commands.command(pass_context = True)
async def poll(self, ctx, option1: str, option2: str, *, question):

or, you can use wait_for

@commands.command(pass_context = True)
async def poll(self, ctx, *options: str):
    ...
    question = await self.bot.wait_for('message', check=lambda message: message.author == ctx.author)
    ...

it is working

@commands.has_permissions(manage_messages=True)
@commands.command(pass_context=True)
async def poll(self, ctx, question, *options: str):

    if len(options) > 2:
        await ctx.send('```Error! Syntax = [~poll "question" "option1" "option2"] ```')
        return

    if len(options) == 2 and options[0] == "yes" and options[1] == "no":
        reactions = ['👍', '👎']
    else:
        reactions = ['👍', '👎']

    description = []
    for x, option in enumerate(options):
        description += '\n {} {}'.format(reactions[x], option)

    poll_embed = discord.Embed(title=question, color=0x31FF00, description=''.join(description))

    react_message = await ctx.send(embed=poll_embed)

    for reaction in reactions[:len(options)]:
        await react_message.add_reaction(reaction)
#create a yes/no poll
@bot.command()
#the content will contain the question, which must be answerable with yes or no in order to make sense
async def pollYN(ctx, *, content:str):
  print("Creating yes/no poll...")
  #create the embed file
  embed=discord.Embed(title=f"{content}", description="React to this message with ✅ for yes, ❌ for no.",  color=0xd10a07)
  #set the author and icon
  embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url) 
  print("Embed created")
  #send the embed 
  message = await ctx.channel.send(embed=embed)
  #add the reactions
  await message.add_reaction("✅")
  await message.add_reaction("❌")

for your code to treat the text in the user's message as one string, use "content:str" in your command arguments

["

I know my code isn't very dynamic and pretty but i did that and it's functional.<\/i>

@client.command()
async def poll(ctx):
options = ['1️⃣ Yes Or No', '2️⃣ Multiple Choice']
embed = discord.Embed(title="What type of poll you want ?" , description="\n".join(options), color=0x00ff00)
msg_embed = await ctx.send(embed=embed)
reaction = await client.wait_for("reaction_add", check=poll)
if (reaction[0].emoji == '1️⃣'):
    user = await client.fetch_user(ctx.author.id)
    msg = await user.send("What is your question ?")
    question = await client.wait_for("message", check=lambda m: m.author == ctx.author)
    answers = ['\n\u2705 Yes\n', '\u274C No']
    new_embed = discord.Embed(title="Poll : " + question.content, description="\n".join(answers), color=0x00ff00)
    await msg_embed.edit(embed=new_embed)
    await msg_embed.remove_reaction('1️⃣', user)
    await msg_embed.add_reaction('\N{WHITE HEAVY CHECK MARK}')
    await msg_embed.add_reaction('\N{CROSS MARK}')


elif (reaction[0].emoji == '2️⃣'):
    user = await client.fetch_user(ctx.author.id)
    msg = await user.send("What is your question ?")
    question = await client.wait_for("message", check=lambda m: m.author == ctx.author)
    msg = await user.send("What are the choices ? (Four compulsory choices, separated by comma)")
    choices = await client.wait_for("message", check=lambda m: m.author == ctx.author)
    choices = choices.content.split(',')

    if (len(choices) > 4):
        await msg_embed.delete()
        await user.send("You can't have more than four choices")
        return
    if (len(choices) < 4):
        await msg_embed.delete()
        await user.send("You can't have less than four choices")
        return
    
    answers = ['1️⃣ '+ choices[0] + '\n', '2️⃣ '+ choices[1] + '\n', '3️⃣ '+ choices[2] + '\n', '4️⃣ '+ choices[3] + '\n']
    new_embed = discord.Embed(title=question.content, description="\n ".join(answers), color=0x00ff00)
    await msg_embed.edit(embed=new_embed)
    await msg_embed.remove_reaction('2️⃣', user)
    await msg_embed.add_reaction('1️⃣')
    await msg_embed.add_reaction('2️⃣')
    await msg_embed.add_reaction('3️⃣')
    await msg_embed.add_reaction('4️⃣')

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