【问题标题】:Trying to create a !say command in my discord.py rewrite bot试图在我的 discord.py 重写机器人中创建一个 !say 命令
【发布时间】:2020-08-02 10:36:03
【问题描述】:

我正在使用 discord.py rewrite 并想创建一个命令,该命令会返回命令“$say”之后的文本。我查看了文档和在线,但找不到任何最新的代码。有谁知道我怎么能做到这一点?谢谢。

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    如果您想使用命令,请确保导入命令并创建上下文参数。像这样:

    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix='$')
    
    @bot.command()
    async def say(ctx, message=None)
        await ctx.send(message)
    
    bot.run("TOKEN HERE")
    

    该命令将由$say message 执行,因为当我们定义机器人时,我们设置了前缀,而当我们设置命令时,我们执行了async def name,显然我们的命令名称是say。

    因此,当您的机器人运行时,您可以使用$say good morning!,而机器人将在该频道中回复good morning!

    ctx.send 是一个协程,这就是我们在它前面等待的原因。而 ctx 定义了通道,而 message 是我们的消息参数。

    一些有用的资源:

    【讨论】:

      【解决方案2】:
      @bot.command()
      async def say(ctx, *, question):
          await ctx.message.delete()
          await ctx.send(f'{question}')
      

      我发送这个只是因为你可以使用它并让它删除作者的消息。然后它还会在您的命令之后发送任何类型的消息。

      【讨论】:

        【解决方案3】:

        在这里,我有一个 say 命令,它只适用于指定的用户 ID。

        async def speak(ctx, *, text):
            if ctx.message.author.id == 621102107621326878:
                message = ctx.message
                await message.delete()
        
                await ctx.send(f"{text}")
            else:
                await ctx.send('lol mate what u doin that isn\'t a real command')
        
        

        如果你想要一个普通的,这样做:

        async def speak(ctx, *, text):
            message = ctx.message
            await message.delete()
        
            await ctx.send(f"{text}")
        

        【讨论】:

          【解决方案4】:

          试试这个,这是我用的

          @client.command
          async def say(ctx, *, text=''):
              if text == '':
                  ctx.send("You need to say something")
              else:
                  ctx.send(text)
                  ctx.message.delete()
          

          【讨论】:

            【解决方案5】:

            我写过的最简单的代码是

            @bot.command()
            async def say(ctx, *, message):
                try:
                    await ctx.send(message)
                except:
                    await ctx.send("Please Give Some Message!")
            

            【讨论】:

              【解决方案6】:

              您需要将 * 放在 ctx 和 message=None 之间。 message=None 将在命令后观看消息。

              我正在使用这个:

              @bot.command()
              async def say(ctx,*,message=None)
                await ctx.send(message)
              

              【讨论】:

                【解决方案7】:
                @client.command
                async def say(ctx, *, text=''):
                if text == '':
                    ctx.send("Say Something First Noob!")
                else:
                await ctx.send(text)
                

                现在请注意不要执行 ctx.send 在 ctx 之前等待,如果你不这样做,那么 100% 它会说它从未等待过

                【讨论】:

                  【解决方案8】:

                  这里!

                  import discord
                  from discord.ext import commands
                  
                  client = commands.Bot(command_prefix='!')
                  
                  @client.command()
                  @commands.is_owner()
                  async def say(ctx, *, message):
                    await ctx.message.delete()
                    await ctx.send(message)
                  
                  client.run("YOUR_TOKEN")
                  

                  【讨论】:

                    猜你喜欢
                    • 2021-06-20
                    • 1970-01-01
                    • 2021-01-07
                    • 2021-09-04
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-11-19
                    • 1970-01-01
                    • 2021-09-20
                    相关资源
                    最近更新 更多