【发布时间】:2023-03-22 19:07:01
【问题描述】:
我正在尝试为我的服务器制作一个音乐机器人。 一切正常,就像机器人连接到频道一样,但加入频道后显示此错误。
我的代码如下所示。
import discord
from discord.ext import commands
from random import choice
import youtube_dl
import asyncio
import urllib.parse, urllib.request, re
client = commands.Bot(command_prefix=commands.when_mentioned_or(">"), description='Relatively simple music bot example')
def colour():
l = [
1752220, 3066993, 3447003, 10181046, 15844367,
15105570, 15158332, 3426654, 1146986, 2067276,
2123412, 7419530, 12745742, 11027200, 10038562,
2899536, 16580705, 12320855
]
return choice(l)
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'
}
ffmpeg_options = {'options': '-vn'}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=1):
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=True):
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,
before_options= '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'),
data=data
)
async def join(ctx):
author = ctx.author.voice
if not author:
em = discord.Embed(
title = 'Opps! ????',
description = 'You are not connected to any voice channel. \nTry again after connecting to a voice channel.',
colour = discord.Colour.red()
)
await ctx.send(embed=em)
return 1
else:
author = ctx.author.voice.channel
voiceClient = ctx.voice_client
if not voiceClient:
await author.connect()
else:
await ctx.send("**Already connected to :**" + author)
return 0
async def search(ctx, url=""):
if 'http://www.youtube.com' in url:
return url
else:
l = url.split(' ')
j = ''
for i in l:
j += i
j += '+'
url = j
htm_content = urllib.request.urlopen(
'http://www.youtube.com/results?search_query=' + url
)
search_results = re.findall(r"watch\?v=(\S{11})", htm_content.read().decode())
std = 'http://www.youtube.com/watch?v='
url = str(std) + str(search_results[0])
return url
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=["p"])
async def play(self, ctx, *, url=""):
w = await join(ctx)
if w == 1:
print("User not connected")
else:
if url == '':
embed = discord.Embed(
title = 'Opps! ????',
description = "You didn't specified any song to play. \nPlease try again but this time specify a song name or url to play.",
colour = discord.Colour.orange()
)
await ctx.send(embed=embed)
else:
url = await search(ctx, url)
async with ctx.typing():
player = await YTDLSource.from_url(url, loop= ctx.bot.loop, stream=True) # before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
client.add_cog(Music(client))
client.run(os.environ['token'])
我不知道是什么问题,因为我在 repl.it 上编写了这个机器人,当我尝试从它的 shell 运行它时,一切正常,但是当我将它托管到 heroku 时,这个错误即将到来。
谁能帮忙。 提前感谢您的帮助????
【问题讨论】:
-
您是否在 heroku 上为您的代码添加了
ffmepg插件? -
问题说得很清楚。它没有找到 ffmpeg,这意味着你要么没有安装它,要么你的机器人找不到它。
-
是的,我已经在 requirements.txt 文件中添加了 ffmepg。如果可能的话,你们中的任何人都可以在我的服务器上帮助我,所以这里是链接。 discord.gg/hRa5gfeUGW
标签: python discord discord.py bots