【发布时间】:2021-11-14 09:42:37
【问题描述】:
我尝试找出一个不和谐的 python 音乐机器人,它在某些方面可以工作,例如,当我告诉它加入或断开连接时它会这样做,但每当我尝试播放音乐时,它都会告诉我这样的命令不存在。我还放了一个错误处理程序,所以它在终端中告诉我没有这样的命令。我观看视频以便我可以帮助自己的那个人没有这个问题。
这是我的代码:
<main.py>
import discord
from discord.ext import commands
#import os
import music
cogs = [music]
client = commands.Bot(command_prefix='$', intents = discord.Intents.all())
for i in range(len(cogs)):
cogs[i].setup(client)
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
em = discord.Embed(title=f"Error!!!", description=f"Command not found.", color=ctx.author.color)
await ctx.send(embed=em)
@client.event
async def on_ready():
print('Bot is on!')
#@client.event
#async def on_message(message):
#if message.author == client.user:
#return
#if message.content.startswith('$hello'):
#await message.channel.send('Hello, sir!')
client.run('Token here')
<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('You need to be in a vc in order to play music!')
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 disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command(name="play")
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 pausel(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))
【问题讨论】:
-
你能提供你看过的视频吗?我经常看到这段代码,想知道它来自哪里
-
很抱歉回答得太晚了,绝对!这是链接:youtube.com/watch?v=jHZlvRr9KxM&t=326s
标签: python discord discord.py bots