所以我试图在 discord.py 中制作一个音乐机器人,但我无法解决这个错误
"AttributeError: 'Bot' 对象没有属性 'add_Cog'"
这是我的代码:
import discord
from discord.ext import commands
import youtube_dl
class musik(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('get in a vc to use this')
voice_channel = ctx.author.voice.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 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))
如果有什么不同,我将使用 replit 来运行代码。 感谢您的回答。
答案 0 :(得分:2)
Bot.add_cog()
是正确的方法(c 是小写)。