Discord.py bot leaving voice channel

社会主义新天地 提交于 2020-06-29 04:37:07

问题


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:

  1. I needed to use: await vc.disconnect()
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!