【问题标题】:discord.py: musicbot gives NoneType Errordiscord.py:musicbot 给出 NoneType 错误
【发布时间】:2022-01-22 11:34:43
【问题描述】:

我试图为不和谐制作一个音乐机器人。它运行但在使用他给我的命令和错误之后:

错误:

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'

代码如下:

导入不和谐 从 discord.ext 导入命令 从不和谐导入 FFmpegPCMAudio 导入 youtube_dl

类音乐(commands.Cog): def init(自我,客户): 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 设置(客户端): client.add_cog(音乐(客户端))

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    访问ctx.voice_client时需要确保它不是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()
    

    如果您的机器人在线时 ct​​x.voice_client 为 None,您应该在 discord API 门户上启用意图在您的代码中将它们提供给您的机器人。欲了解更多信息:https://discordpy.readthedocs.io/en/stable/intents.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多