问题
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