Dycia
Dycia

Reputation: 15

discord.py: musicbot gives NoneType Error

i tryed to make a music bot for discord. it runs but after using the commands he gives me and error:

ERROR:

Ignoring exception in command play:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/CrypBot/music.py", line 25, in play
    ctx.voice_client.stop()
AttributeError: 'NoneType' object has no attribute 'stop'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'stop'

Code is down below:

import discord from discord.ext import commands from discord import FFmpegPCMAudio import youtube_dl

class music (commands.Cog): def init(self, client): self.client=client

@commands.command()
async def join(self,ctx):
    if ctx.author.voice is None:
        await ctx.send("Du bist in keinem VC!")
    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voice_channel.connect()
    else:
        await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def stop(self,ctx):
    await ctx.voice_client.disconnect()

@commands.command()
async def play(self,ctx,url):
    ctx.voice_client.stop()
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    YDL_OPTIONS = {'format':"bestaudio"}
    vc = ctx.voice_client

    with youtube_dl.YoutubeDL(YDL_Options) as ydl:
        info = ydl.extract_info(url, download=False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2,
        **FFMPEG_OPTIONS)
        vc.play(source)

@commands.command()
async def pause(self,ctx):
    await ctx.voice_client.pause()
    await ctx.send("Pausiert")

@commands.command()
async def resume(self,ctx):
    await ctx.voice_client.resume()
    await ctx.send("wird Weitergespielt...")

def setup(client): client.add_cog(music(client))

Upvotes: 1

Views: 126

Answers (1)

Nathan Marotte
Nathan Marotte

Reputation: 816

When accessing ctx.voice_client you need to make sure it is not None

if ctx.voice_client is None:
    return  # Nothing to stop, the bot is not connected to a voice chat
else:
    ctx.voice_client.stop()

If ctx.voice_client is None while your bot is online, you should enable the intents on the discord API portal AND give them to your bot in your code. For more info : https://discordpy.readthedocs.io/en/stable/intents.html

Upvotes: 0

Related Questions