AttributeError: 'tuple' object has no attribute 'add_cog'

AttributeError: 'tuple' object has no attribute 'add_cog'

我正在尝试制作一个不和谐的音乐机器人,但我收到了这个错误:

    Traceback (most recent call last):
      File "main.py", line 11, in <module>
        cogs[i].setup(client)
      File "/home/runner/MOMOSNACK-BOT/music.py", line 48, in setup
        client.add_cog(music(client))
    AttributeError: 'tuple' object has no attribute 'add_cog'`

我正在学习本教程: https://www.youtube.com/watch?v=jHZlvRr9KxM&ab_channel=MaxA

我已尝试将 client.add_cog(music(client)) 替换为:

client.add.cog(音乐(客户端))

bot.add_cog(音乐(客户端))

bot.add_cog(音乐(机器人))

连同大小写的所有变化,但其中 none 似乎解决了我的问题。在此先感谢您的帮助!

这是我的music.py:

    import discord
    from discord.ext import commands
    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("PLEASE JOIN A VC FIRST")
            voice_channel = ctx.author.voice.channel
            if ctx.voice_channel is None:
                await voice_channel.connect()
            else:
                await ctx.voice_client.move_to(voice_channel)
    
        @commands.command()
        async def disconnect(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("PAUSED")
    
        @commands.command()
        async def resume(self,ctx):
            await ctx.voice_client.resume()
            await ctx.send("RESUMED")


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

这是我的 main.py 减去机器人的令牌。该机器人还在 main.py 中做了一些其他事情,但我不认为那是搞砸它的原因:

    import discord
    from discord.ext import commands
    import os
    import music
    
    cogs = [music]
    
    client = discord.Client(), commands.Bot(command_prefix='please ', intents = discord.Intents.all())

    for i in range(len(cogs)):
        cogs[i].setup(client)
      
    thankWords = ["Thank You", "Thank you", "thank you", "THANK YOU", "Thanks", "thanks", "THANKS", "Thx", "thx", "THX", "Tysm", "tysm", "TYSM"]

    @client.event
    async def on_ready():
      print('We have logged in as {0.user}'.format(client))

    @client.event
    async def on_message(message):
      if message.author == client.user:
        return

      msg = message.content
  
      if any(word in msg for word in thankWords):
        user = discord.utils.get(message.mentions)  
        await message.channel.send(user.mention + ' has received a **momosnack**!')

    client.run('[BOT TOKEN]')

说明

在 python 中,当您将两个 comma-separated 值分配给一个变量时,该变量将包含一个包含这两个值的元组。

在您的例子中,您将客户端设置为 discord.Clientcommands.Bot 的元组。从您使用命令的事实来看,我假设您只想使用 commands.Bot.

代码

替换

client = discord.Client(), commands.Bot(command_prefix='please ', intents = discord.Intents.all())

client = commands.Bot(command_prefix='please ', intents = discord.Intents.all())

参考

Tuple packing/Unpacking