Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - 'NoneType' object has no attribute 'is_connected'

i have wrote a bot to play music, but it always print and error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'is_connected'

This is my function:

async def play(ctx, url):
if ctx.message.author.voice:
    channel = ctx.message.author.voice.channel
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice.is_connected():
        await channel.connect()

I think the error is generated by this function:

voice.is_connected()

Dont know why it doesn't work ...

question from:https://stackoverflow.com/questions/65852029/nonetype-object-has-no-attribute-is-connected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this would work

channel = ctx.message.author.voice.channel

        voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)

        voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)

        if voice_client == None:
            await voice.connect()
        else:
            await voice_client.move_to(channel)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...