将Discord.PY代码更新到最新版本时出现问题

时间:2019-08-28 09:23:59

标签: python discord.py-rewrite

背景很少:我正在努力更新内嵌的反应(discord.py)。

我从这里获得了代码,但是需要将其更新为最新版本。

我已经更新了某些部分,但是我在反应检测方面遇到了问题。

@commands.command()
async def embedpages(self, ctx):
  page1=discord.Embed(
      title='Page 1/3',
      description='Description',
      colour=discord.Colour.orange()
  )
  page2=discord.Embed(
      title='Page 2/3',
      description='Description',
      colour=discord.Colour.orange()
  )
  page3=discord.Embed(
      title='Page 3/3',
      description='Description',
      colour=discord.Colour.orange()
  )

  pages=[page1,page2,page3]

  message = await ctx.send(embed=page1)

  await ctx.message.add_reaction('\u23ee')
  await ctx.message.add_reaction('\u25c0')
  await ctx.message.add_reaction('\u25b6')
  await ctx.message.add_reaction('\u23ed')

  i=0
  emoji=''

  while True:
      if emoji=='\u23ee':
          i=0
          await self.bot.edit_message(message,embed=pages[i])
      if emoji=='\u25c0':
          if i>0:
              i-=1
              await self.bot.edit_message(message,embed=pages[i])
      if emoji=='\u25b6':
          if i<2:
              i+=1
              await self.bot.edit_message(message,embed=pages[i])
      if emoji=='\u23ed':
          i=2
          await self.bot.edit_message(message,embed=pages[i])

      res = await self.bot.wait_for_reaction(message=message,timeout=30) #Need help
      if res==None:
          break
      if str(res[1])!='MyBot#1111':
          emoji=str(res[0].emoji)
          await self.bot.remove_reaction(message,res[0].emoji,res[1])

  await self.bot.clear_reactions(message)

我只是想将我的机器人更新为“ Discord.py

1 个答案:

答案 0 :(得分:0)

以下是所有更改的列表:
(我的不在内,并且bot代表self.bot

  • @commands.command()不接受self,因为我不使用课程
  • 要对漫游器的消息做出反应,您需要使用之前声明的变量message而不是ctx.message
  • 我更改了while循环,因为我使用了wait_for()。我在代码中添加了解释作为注释

    @commands.command()
    async def embedpages(ctx):
       page1 = discord.Embed(
           title='Page 1/3',
           description='Description',
           colour=discord.Colour.orange()
       )
       page2 = discord.Embed(
           title='Page 2/3',
           description='Description',
           colour=discord.Colour.orange()
       )
       page3 = discord.Embed(
           title='Page 3/3',
           description='Description',
           colour=discord.Colour.orange()
       )

       pages = [page1, page2, page3]
       message = await ctx.send(embed=page1)

       await message.add_reaction('\u23ee')
       await message.add_reaction('\u25c0')
       await message.add_reaction('\u25b6')
       await message.add_reaction('\u23ed')

       i = 0
       emoji = ''

       while True:
           try:
               reaction, user = await bot.wait_for('reaction_add', timeout=30.0) # Gets the reaction and the user with a timeout of 30 seconds + new Syntax

               if user == ctx.author: # Test if the user is the author
                   emoji = str(reaction.emoji)

                   if emoji == '\u23ee':
                       i = 0
                       await message.edit(embed=pages[i]) # New Syntax

                   elif emoji == '\u25c0':
                       if i > 0:
                           i -= 1
                           await message.edit(embed=pages[i]) # New Syntax

                   elif emoji == '\u25b6':
                       if i < 2:
                           i += 1
                           await message.edit(embed=pages[i]) # New Syntax

                   elif emoji == '\u23ed':
                       i = 2
                       await message.edit(embed=pages[i]) # New Syntax

               if client.user != user: # Test if it isn't the bot
                   await message.remove_reaction(reaction, user) # Removes reaction from a "stranger" or from the author + new Syntax

           except asyncio.TimeoutError: # Handles Timeout
               break

       await message.clear_reactions()` # In the end, all reactions get deleted + new Syntax


  

Link到文档