How a discord bot can join a voice channel in discord rewrite?

匆匆过客 提交于 2020-05-30 07:52:19

问题


I want to make my discord bot to connect to the voice channel I am when I type !join. i have tried to do it with the following code but i got this error: bot: BotInstance of 'Bot' has no 'voice_client_int' memberpylint(no-member)

i found that my code is not compatible with the rewrite discord version.

@bot.command(pass_context = True)
async def join(ctx):
        channel = ctx.message.author.voice.voice_channel
        await bot.join_voice_channel(channel)
@bot.command(pass_context = True)
async def leave(ctx):
        server = ctx.message.server
        voice_client = bot.voice_client_int(server)
        await voice_client.disconnect()

can someone help me?


回答1:


As stated in the migrating page, in the rewrite version, the voice connection is now a method of the VoiceChannel model. The pass_context is also deprecated as context is now always passed.

It now would look like this:

@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()
@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()

Of course this oversimplified version lacks error handling.



来源:https://stackoverflow.com/questions/57285423/how-a-discord-bot-can-join-a-voice-channel-in-discord-rewrite

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