首页 > 解决方案 > 需要 Discord Bot 删除以前的消息

问题描述

我正在编写一个不和谐的机器人,使用它需要很多命令,所以它很快就会阻塞一个通道。这是一些理论上应该可以工作的示例代码。

    @commands.command() 
     async def play(self,ctx,url):
         ctx.voice_client.stop()
         FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 - 
         reconnect_delay_max 5', 'options': '-vn'}
         YDL_OPTIONS = {'format':"bestaudio"}
         vc = ctx.voice_client


    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
      info = ydl.extract_info(url, download=False)
      url2 = info['formats'][0]['url']
      source = await discord.FFmpegOpusAudio.from_probe(url2,
      **FFMPEG_OPTIONS)
      vc.play(source)
      await ctx.message.delete()

此示例有效,但是此示例有时有效,但有时无效,我不知道为什么。

@commands.command()
async def join(self,ctx):
  await ctx.message.delete()
  if ctx.author.voice is None:
    await ctx.send("You are not in a voice channel.")
  voice_channel = ctx.author.voice.channel
  if ctx.voice_client is None:
      await voice_channel.connect()
  else:
      await ctx.voice_client.move_to(voice_channel)

我真的很困惑为什么它不起作用,我想做的就是删除以前的消息,这样它就不会阻塞频道。

标签: pythondiscord

解决方案


Discord.Py 有一个delete_afterkwarg,它需要花费几秒钟来删除发送的消息,例如

await channel.send("this message will be deleted after 10 seconds", delete_after=10)

您可以在任何ctx.send消息中使用此参数,以便在特定时间后自动删除。


推荐阅读