我正在制作一个音乐机器人,但我遇到了这个问题,即运行python 3.6.8并将其托管在heroku上
我听说我需要使用discord.opus.LoadOpus或类似的东西,但我不知道如何将其添加到我的代码中,请帮助
继承我的代码
import discord
import asyncio
from discord.ext import commands
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
@client.event
async def on_ready():
print('client ready')
async def audio_player_task():
while True:
play_next_song.clear()
current = await songs.get()
current.start()
await play_next_song.wait()
def toggle_next():
client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
if not client.is_voice_connected(ctx.message.server):
voice = await client.join_voice_channel(ctx.message.author.voice_channel)
else:
voice = client.voice_client_in(ctx.message.server)
player = await voice.create_ytdl_player(url, after=toggle_next)
await songs.put(player)
client.loop.create_task(audio_player_task())
client.run('TOKEN')
我有这个错误:
Ignoring exception in command play
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "Draco.py", line 30, in play
voice = await client.join_voice_channel(ctx.message.author.voice_channel)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 3209, in join_voice_channel
voice = VoiceClient(**kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/voice_client.py", line 230, in __init__
self.encoder = opus.Encoder(48000, 2)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/opus.py", line 197, in __init__
raise OpusNotLoaded()
discord.opus.OpusNotLoaded
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OpusNotLoaded:
尝试使用以下命令加载opus模块:
discord.opus.load_opus()
在尝试使用机器人进行与语音通道相关的任何操作之前的任何地方。
根据discord.py docs你不应该在Windows环境中使用它,这可能是为什么它在你的本地机器上工作而不是在heroku上(这是基于unix的)。
另外,我会做以下事情:
discord.opus.load_opus()
if not discord.opus.is_loaded():
raise RunTimeError('Opus failed to load')
因此,如果未正确加载并知道立即查看的位置,您确定会引发异常。如果加载时发生异常,它仍将传播。