所以我有一个不和谐的机器人,里面有音乐指令。今天我补充说,如果机器人在5分钟内不播放任何东西,它就会离开语音频道。
def search(arg):
global last_played
last_played = arg
try:
requests.get("".join(arg))
except:
arg = " ".join(arg)
else:
arg = "".join(arg)
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
return {'source': info['formats'][0]['url'], 'title': info['title']}
async def check_timer(ctx):
global last_played
guild = ctx.message.guild
voice_client = guild.voice_client
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
music_check = last_played
await asyncio.sleep(300)
if not voice.is_playing() and music_check == last_played:
await voice_client.disconnect()
await ctx.send("No song is queued for 5 minutes. Left the voice channel!")
def play_next(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if len(song_queue) > 1:
del song_queue[0]
voice.play(discord.FFmpegPCMAudio(song_queue[0]['source'], **FFMPEG_OPTIONS), after=lambda e: play_next(ctx))
voice.is_playing()
else:
asyncio.create_task(check_timer(ctx))
@client.command()
async def play(ctx, *arg):
channel = ctx.message.author.voice.channel
if channel:
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
song = search(arg)
song_queue.append(song)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
if not voice.is_playing():
voice.play(discord.FFmpegPCMAudio(song_queue[0]['source'], **FFMPEG_OPTIONS), after=lambda e: play_next(ctx))
voice.is_playing()
else:
await ctx.send("Added to queue")
else:
await ctx.send("You're not connected to any channel!")问题是当我管理机器人的时候。我知道这个错误
2021-01-02T14:49:20.462802+00:00 app[worker.1]: Traceback (most recent call last):
2021-01-02T14:49:20.462823+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/discord/player.py", line 611, in _call_after
2021-01-02T14:49:20.462824+00:00 app[worker.1]: self.after(error)
2021-01-02T14:49:20.462825+00:00 app[worker.1]: File "/app/main.py", line 219, in <lambda>
2021-01-02T14:49:20.462834+00:00 app[worker.1]: voice.play(discord.FFmpegPCMAudio(song_queue[0]['source'], **FFMPEG_OPTIONS), after=lambda e: play_next(ctx))
2021-01-02T14:49:20.462837+00:00 app[worker.1]: File "/app/main.py", line 201, in play_next
2021-01-02T14:49:20.462838+00:00 app[worker.1]: asyncio.create_task(check_timer(ctx))
2021-01-02T14:49:20.462838+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/asyncio/tasks.py", line 360, in create_task
2021-01-02T14:49:20.462839+00:00 app[worker.1]: loop = events.get_running_loop()
2021-01-02T14:49:20.462859+00:00 app[worker.1]: RuntimeError: no running event loop
2021-01-02T14:49:20.470913+00:00 app[worker.1]: /app/.heroku/python/lib/python3.9/site-packages/discord/player.py:615: RuntimeWarning: coroutine 'check_timer' was never awaited
2021-01-02T14:49:20.470914+00:00 app[worker.1]: traceback.print_exception(type(exc), exc, exc.__traceback__)
2021-01-02T14:49:20.470915+00:00 app[worker.1]: RuntimeWarning: Enable tracemalloc to get the object allocation traceback我在网上搜索,看到一些人犯了同样的错误,但他们的解决方案是把await放在某个地方,我看不到任何需要等待的地方。
PS我正在使用heroku托管这个机器人。
发布于 2021-01-02 15:32:39
这是导致错误的行
asyncio.create_task(check_timer(ctx))要解决这个问题:
task = client.loop.create_task(check_timer(ctx))
# or
loop = asyncio.get_event_loop()
task = loop.create_task(check_timer(ctx))
await task参考资料:
https://stackoverflow.com/questions/65540558
复制相似问题