python - 需要 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)
我真的很困惑为什么它不起作用,我想做的就是删除以前的消息,这样它就不会阻塞频道。
解决方案
Discord.Py 有一个delete_after
kwarg,它需要花费几秒钟来删除发送的消息,例如
await channel.send("this message will be deleted after 10 seconds", delete_after=10)
您可以在任何ctx.send
消息中使用此参数,以便在特定时间后自动删除。
推荐阅读
- javascript - 在 PHP 中更新和传递值
- react-native - 如何根据 React Native 中的用户搜索从 api 获取数据?
- javascript - 解释 Vigenère 密码的功能
- python-3.x - 如何在 Windows 中在 MSYS2 之外运行 GTK3 Python3 脚本
- c++ - C++ cout 行为/执行顺序
- angular - Angular 7 在 *ngFor 列表中显示和隐藏孩子
- php - 当有出口时如何在php的foreach循环中传递一个值
- r - 如何根据 R 中的列表从矩阵中删除列?
- php - 转换多用户登录
- tableau-api - 如何仅使用每个类别的特定行数进行计算?