在我的机器人读取音频文件后,我需要它离开频道。我试图在线路中放置一个“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')
答案 0 :(得分:0)
有一个after参数可以传递给voice.play
。所以你可以用
voice.play("audio.mp3", after=await voice.disconnect())
我没有尝试使用异步功能,但我想这会奏效。