【问题标题】:Discord.py - AttributeError: 'NoneType' object has no attribute 'connect'Discord.py - AttributeError: 'NoneType' 对象没有属性 'connect'
【发布时间】:2021-11-07 01:51:44
【问题描述】:

我正在尝试使用 discord.py 编写一个简单的音乐机器人来使用 youtube_dl 播放音乐。我的“加入”命令有问题。当我尝试在不和谐上使用它时会出现此错误

Ignoring exception in command join:
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/Discord-Bot-2/commands.py", line 44, in join
    await ctx.voice_client.connect(ctx.author.voice_channel)
AttributeError: 'NoneType' object has no attribute 'connect'

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 'connect'

当我在语音频道中时会发生此错误。如果我不是,机器人会按照我编写的代码发送一条不和谐的消息“你没有连接到语音通道”。但是当我在语音频道中时,它会给出错误。

join命令的代码是

  @commands.command()
  async def join(self, ctx):
    if ctx.author.voice is None:
      return await ctx.send("You are not connected to a voice channel")
    
    if ctx.voice_client is not None:
      await ctx.voice_client.disconnect()
    
    await ctx.voice_client.connect(ctx.author.voice_channel)
    await ctx.send(f"Connected to {ctx.author.voice_channel}")

我很确定我已正确设置所有内容,但这是我为命令设置 cog 的 join 命令上方的所有代码

import discord
from discord.ext import commands
import youtube_dl
import pafy
import asyncio

class Commands(commands.Cog):
  def __init__(self, client):
    self.client = client
    self.song_queue = {}

  @commands.Cog.listener()
  async def on_ready(self):
    for guild in self.client.guilds:
      self.song_queue[guild.id] = []
    print("Cog 'commands' loaded")
  
  async def check_queue(self, ctx):
    if len(self.song_queue[ctx.guild.id]) > 0:
      ctx.voice_client.stop()
      await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
      self.song_queue[ctx.guild.id].pop(0)

  async def search_song(self, amount, song, get_url=False):
    info = await self.client.loop.run_in_executor(None, lambda: youtube_dl.YoutubeDL({"format": "bestaudio", "quiet" : True}).extract_info(f"ytsearch{amount}:{song}", download=False, ie_key="YoutubeSearch"))
    if len(info["entries"]) == 0: 
      return None

    return [entry["webpage_url"] for entry in info["entries"]] if get_url else info
  
  async def play_song(self, ctx, song):
    url = pafy.new(song).getbestaudio().url
    ctx.voice_client.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(url)), after=lambda error: self.client.loop.create_task(self.check_queue(ctx)))
    ctx.voice_client.source.volume = 0.5

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    我使用的是过时的方法(voice_client)

      @commands.command()
      async def join(self, ctx):
        if ctx.author.voice is None:
          return await ctx.send("You are not connected to a voice channel")
        else:
          channel = ctx.author.voice.channel
          await channel.connect()
          await ctx.send(f"Connected to voice channel: '{channel}'")
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2020-09-15
      • 1970-01-01
      • 2021-07-27
      • 2021-01-05
      相关资源
      最近更新 更多