【问题标题】:Poll command discord.py轮询命令 discord.py
【发布时间】:2020-09-26 14:35:47
【问题描述】:

DISCORD.PY

我尝试为投票系统创建命令并遇到问题。命令如下:

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

我的问题是:我怎样才能使我使用命令提出的问题有更多的单词。如果我现在使用更多单词,我会将它们作为选项阅读并得到错误。

Ex 1:/poll You are human yes no(只将“you”读作问题,其余为选项。)

Ex 2:/poll 你是人类是的,不是(这就是我想要的)

谢谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    #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("❌")
    

    要让您的代码将用户消息中的文本视为一个字符串,请在命令参数中使用“content:str”

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    它正在工作

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

    【讨论】:

    • 欢迎来到 Stackoverflow!请您对错误原因添加一些解释吗?
    【解决方案3】:

    使用:

    /poll "You are human" yes no
    

    这里,"You are human" 是一个字符串 "yes" "no" 是另外两个字符串。

    另一种更好的方法是:

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

    或者,您可以使用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)
        ...
    

    【讨论】:

      【解决方案4】:

      你可以这样做:

      @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('✅')
      

      但是现在你必须做 /poll hello-everyone text text 如果你不想有“-”,你必须这样做:

      @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('✅')
      

      但在这一个中,你不能有自己的选择......

      【讨论】:

        【解决方案5】:

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

         /poll "You are human" yes no
        

        【讨论】:

        • 我看到它是这样工作的,但我可以直接通过命令让它工作吗?
        猜你喜欢
        • 2021-03-26
        • 1970-01-01
        • 2021-08-28
        • 2021-01-22
        • 2021-03-30
        • 2021-05-17
        • 2021-01-15
        • 2022-12-04
        • 2023-02-25
        相关资源
        最近更新 更多