[英]How to have bot automatically disconnect after playing audio file
在我的机器人读取音频文件后,我需要它离开频道。 我试图在线路中加入“await voice.disconect()”,但在机器人加入频道后断开了它。 如何实现这一点以使机器人在播放文件后离开?
#imports
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import random
from discord import embeds
from discord import message
from discord import guild
from discord_slash import SlashCommand
#define client & prefix
client = commands.Bot(command_prefix = '!')
#list of audio for random.choice
Audio = ['LIST OF AUDIO FILES']
#join & play file command
@client.command(pass_context=True)
async def yolk(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voice = await channel.connect()
source = FFmpegPCMAudio(random.choice(Audio))
player = voice.play(source)
else:
await ctx.send("User not in a voice channel, unable to connect.")
client.run('BOT TOKEN')
您可以将 after 参数传递给voice.play
。 所以你可以用
voice.play("audio.mp3", after=await voice.disconnect())
我没有用异步 function 尝试它,但我想这会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.