How to make a bot join voice channels discord.py

可紊 提交于 2021-02-09 10:58:04

问题


I'm using discord.py to create a music bot, but I'm having trouble connecting the bot to a voice channel. Im using a Cog to seperate the music features from the rest.

@commands.command()
async def join_voice(self, ctx):
    channel = ctx.author.voice.channel
    print(channel.id)
    await self.client.VoiceChannel.connect()

But I get the error: AttributeError: 'NoneType' object has no attribute 'channel'

I've looked all through the docs and all the similar questions on here and still no solutions!

Could someone help please?


回答1:


You're really close! The only thing you've got to change is:

@commands.command()
async def join_voice(self, ctx):
    connected = ctx.author.voice
    if connected:
        await connected.channel.connect() #  Use the channel instance you put into a variable

What you were doing was grabbing the VoiceChannel class object, and not the actual VoiceChannel instance that the user was connected to. That's where your error came in, as it was trying to find a voice channel which didn't exist.

Glad to see the progress, keep it up!




回答2:


I'm no discord.py expert but try some of these links:

How a discord bot can join a voice channel in discord rewrite? AttributeError: 'NoneType' object has no attribute 'channels' https://github.com/Rapptz/discord.py/issues/585

I think the 3rd link is more relatable to you.



来源:https://stackoverflow.com/questions/61784807/how-to-make-a-bot-join-voice-channels-discord-py

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