python - 带有一组工作音乐命令的小型不和谐机器人,大约 6 天前,播放功能完全停止运行(更多下文)
问题描述
这是一个用于我的服务器的小型不和谐 python 机器人,具有各种功能,其中包括音乐命令,直到它们突然停止而没有显示任何问题错误。它使用 FFMpeg 和 youtubeDl 以及 pytube 来收集歌曲并将其存储在本地以播放它,我已经更新了所有这些,并且它们肯定在当前版本上,因为我已经在线确保了这一点。任何人都可以提供的任何帮助或见解将不胜感激。很抱歉,如果代码的编写方式令人费解,我对编码还是很陌生,这是我第一个真正的大型项目之一。
如果您需要任何信息,我很乐意尽我所能提供帮助。
以下是播放命令的代码:
@client.command()
async def play(ctx, *args):
global queu
#global autom
if not args:
voice = get(client.voice_clients, guild=ctx.guild)
if not voice.is_playing():
server = ctx.message.guild
voice_channel = server.voice_client
if queu:
async with ctx.typing():
player = await YTDLSource.from_url(queu[0], loop=client.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('**Now playing:** {}'.format(player.title))
del(queu[0])
# while autom == True:
# try:
# a = client.get_command('auto')
# await ctx.invoke(a)
# except:
# print('')
elif not queu:
await ctx.send("You can't play if there isn't anything in the queue\nIf auto mode was on it has now been disabled, to use it gain please add to the queue and run ``;auto on``")
autom = False
if args:
global gueu
search_keywords = ""
print(args)
for word in args:
search_keywords += word
search_keywords += '+'
link = "https://www.youtube.com/results?search_query="
link += search_keywords
#print(link)
html = urllib.request.urlopen(link)
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
url = ("https://www.youtube.com/watch?v=" + video_ids[0])
#print(url)
queu.append(url)
#print(queu)
await ctx.send("``{}`` added to queue!\n If the song doesn't start please either let the current song end and run ``;play``/``;next`` again or run ``;next`` to play now".format(url))
try:
p = client.get_command('play')
await ctx.invoke(p)
except:
print('failed')
解决方案
我知道这段代码不能解决你的特定问题,但我有一些用于音乐机器人的 python 代码,而不是在你的设备上下载 mp3 文件,只是流式传输你想要播放的 youtube 歌曲 - 有点像比如 Groovy 和 Rhythm 机器人。它要快得多,而且代码对我来说非常好。干得好:
import asyncio
import discord
import youtube_dl
from discord.ext import commands
# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
class Music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command(description="joins a voice channel")
async def join(self, ctx):
if ctx.author.voice is None or ctx.author.voice.channel is None:
return await ctx.send('You need to be in a voice channel to use this command!')
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
vc = await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
vc = ctx.voice_client
@commands.command(description="streams music")
async def play(self, ctx, *, url):
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.client.loop, stream=True)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
embed = discord.Embed(title="Now playing", description=f"[{player.title}]({player.url}) [{ctx.author.mention}]")
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Music(bot))
首先,确保您已下载ffmpeg
并youtube_dl
更新到最新版本。另外,请注意,这段代码是写在一个cog
文件中的,所以如果你决定像我一样格式化你的项目,然后cogs
在你的项目目录中创建一个名为的文件夹,在文件夹中添加一个新文件(你可以随意命名,但它需要以.py
) 结尾,并将上面的代码复制粘贴到此文件中。然后,在您的主.py
文件中,添加以下代码以加载 cog:
# loading all cogs
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
cogs
或者,您可以选择简单地将音乐代码添加到主文件中,而不是创建文件夹.py
,在这种情况下,您将不需要上面的代码。
推荐阅读
- python-3.x - Snakemake 在规则 A 是规则 B 的依赖项时拒绝解包输入函数,但在规则 A 是最终规则时接受它
- apache-spark - 使用 spark 读取 sql server 日志文件(ldf)
- python - 在 MetPy 中计算多个垂直水平的涡度
- pharo - 必须声明临时变量而不是绑定的原因是什么?
- html - 当“顶部”值更改时,文本不会留在父 div 中
- c++ - Qt5对QInputDialog和QMessageBox的未识别引用..错误
- python - 使用 Python 获取 SVN 存储库的根目录
- ansible - 如何遍历列表的字典并将变量传递给 Ansible 角色?
- windows - 在 Buildbot 中设置和获取属性
- xamarin - Xamarin 表单 - 动态更改网格行可见性