Update bots/maniac.py
Browse files- bots/maniac.py +45 -45
bots/maniac.py
CHANGED
|
@@ -25,51 +25,51 @@ def setup_bot(client):
|
|
| 25 |
|
| 26 |
async def play_next(ctx):
|
| 27 |
# 檢查播放清單是否有歌曲
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
|
| 75 |
# 播放音樂的命令
|
|
|
|
| 25 |
|
| 26 |
async def play_next(ctx):
|
| 27 |
# 檢查播放清單是否有歌曲
|
| 28 |
+
if ctx.guild.id in queues and len(queues[ctx.guild.id]['songs']) > 0:
|
| 29 |
+
# 移除並獲取播放列表中的第一首歌曲
|
| 30 |
+
song_info = queues[ctx.guild.id]['songs'].pop(0)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 使用 ytdl 提取音樂資訊
|
| 34 |
+
data = await asyncio.get_event_loop().run_in_executor(None, lambda: ytdl.extract_info(song_info['url'], download=False))
|
| 35 |
+
|
| 36 |
+
# 如果返回的是列表(可能是搜索結果),選擇第一個結果
|
| 37 |
+
if isinstance(data, list):
|
| 38 |
+
data = data[0]
|
| 39 |
+
|
| 40 |
+
# 提取歌曲資訊
|
| 41 |
+
title = data.get('title', 'Unknown Title')
|
| 42 |
+
uploader = data.get('uploader', 'Unknown User')
|
| 43 |
+
song_url = data.get('url')
|
| 44 |
+
|
| 45 |
+
if not song_url:
|
| 46 |
+
# 若沒有 url,顯示錯誤訊息
|
| 47 |
+
await ctx.send("無法獲取歌曲的播放鏈接。")
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
+
# 創建播放器
|
| 51 |
+
player = discord.FFmpegPCMAudio(song_url, **ffmpeg_options)
|
| 52 |
+
|
| 53 |
+
# 更新當前播放的歌曲訊息
|
| 54 |
+
if 'current_message' in queues[ctx.guild.id]:
|
| 55 |
+
message = queues[ctx.guild.id]['current_message']
|
| 56 |
+
await message.edit(content=f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 57 |
+
else:
|
| 58 |
+
# 若無現有的訊息,發送新的訊息顯示當前播放的歌曲
|
| 59 |
+
message = await ctx.send(f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 60 |
+
queues[ctx.guild.id]['current_message'] = message
|
| 61 |
+
|
| 62 |
+
# 播放音樂並設置播放完後調用 play_next
|
| 63 |
+
voice_clients[ctx.guild.id].play(player, after=lambda e: asyncio.run_coroutine_threadsafe(play_next(ctx), client.loop))
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# 捕捉錯誤並顯示訊息
|
| 67 |
+
await ctx.send(f"播放錯誤: {str(e)}")
|
| 68 |
+
else:
|
| 69 |
+
# 播放清單已空,機器人將離開語音頻道
|
| 70 |
+
await ctx.send("播放清單已經播放完畢,機器人將離開語音頻道。")
|
| 71 |
+
# 假設需要停止播放並斷開連接
|
| 72 |
+
await voice_clients[ctx.guild.id].disconnect()
|
| 73 |
|
| 74 |
|
| 75 |
# 播放音樂的命令
|