问题
I've been making a discord bot which enters a vc plays an audio then leaves, but I can't seem to get the leaving part to work. Here is my code:
# Discord specific import
import discord
from discord.ext import commands
import asyncio
Client = discord.Client()
client = commands.Bot(command_prefix="..")
@client.command(pass_context=True)
async def dan(ctx):
author = ctx.message.author
channel = author.voice_channel
vc = await client.join_voice_channel(channel)
player = vc.create_ffmpeg_player('dan.mp3', after=lambda: print('done'))
player.start()
player.disconnect()
client.run('token')
I'm not getting any errors with this but at the same time the bot is not disconnecting from the vc and I have tried changing 'player' for 'client', 'Client' and 'client.voice'
回答1:
My problems were that:
- I needed to use: await vc.disconnect()
- My version of websockets was too high and needed to be below v4.0.0
Hope this helps people with my problem
回答2:
try vc.disconnect() as stated here in the docs, since Client.join_voice_channel(channel) creates a VoiceClient. Also I suggest not having redundant variables like
author = ctx.message.author
channel = author.voice_channel
When you can have vc = await client.join_voice_channel(ctx.message.author.voice_channel)
Also, another redundant variable is Client = discord.Client() since you don't use it anywhere, you use the commands.Bot instance, so it's best to remove that.
回答3:
player.disconnect() is a coroutine, you should use the await keyword before it.
await player.disconnect()
来源:https://stackoverflow.com/questions/48139249/discord-py-bot-leaving-voice-channel