Update bots/maniac.py
Browse files- bots/maniac.py +68 -38
bots/maniac.py
CHANGED
|
@@ -23,51 +23,81 @@ def setup_bot(client):
|
|
| 23 |
async def on_ready():
|
| 24 |
print(f'{client.user} is now jamming')
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
uploader = data.get('uploader', 'Unknown User')
|
| 32 |
-
song_url = data['url']
|
| 33 |
-
player = discord.FFmpegPCMAudio(song_url, **ffmpeg_options)
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
if 'current_message' in queues[ctx.guild.id]:
|
| 38 |
-
message = queues[ctx.guild.id]['current_message']
|
| 39 |
-
await message.edit(content=f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@client.command(name="play")
|
| 48 |
async def play_music(ctx, *, link):
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
voice_client = await ctx.author.voice.channel.connect()
|
| 52 |
-
voice_clients[ctx.guild.id] = voice_client
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"Connection error: {e}")
|
| 55 |
-
return
|
| 56 |
-
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
query_string = urllib.parse.urlencode({'search_query': link})
|
| 61 |
-
content = urllib.request.urlopen(youtube_results_url + query_string)
|
| 62 |
-
search_results = re.findall(r'/watch\?v=(.{11})', content.read().decode())
|
| 63 |
-
link = youtube_watch_url + search_results[0]
|
| 64 |
-
|
| 65 |
-
data = await asyncio.get_event_loop().run_in_executor(None, lambda: ytdl.extract_info(link, download=False))
|
| 66 |
-
song = data['url']
|
| 67 |
-
player = discord.FFmpegPCMAudio(song, **ffmpeg_options)
|
| 68 |
-
voice_clients[ctx.guild.id].play(player, after=lambda e: asyncio.run_coroutine_threadsafe(play_next(ctx), client.loop))
|
| 69 |
except Exception as e:
|
| 70 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
@client.command(name="clear_queue")
|
| 73 |
async def clear_queue(ctx):
|
|
|
|
| 23 |
async def on_ready():
|
| 24 |
print(f'{client.user} is now jamming')
|
| 25 |
|
| 26 |
+
import discord
|
| 27 |
+
import asyncio
|
| 28 |
+
import urllib.parse
|
| 29 |
+
import urllib.request
|
| 30 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# 假設 ytdl 和 ffmpeg_options 等已經正確設置
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
async def play_next(ctx):
|
| 35 |
+
if queues.get(ctx.guild.id):
|
| 36 |
+
link = queues[ctx.guild.id].pop(0)
|
| 37 |
+
data = await asyncio.get_event_loop().run_in_executor(None, lambda: ytdl.extract_info(link, download=False))
|
| 38 |
+
title = data.get('title', 'Unknown Title')
|
| 39 |
+
uploader = data.get('uploader', 'Unknown User')
|
| 40 |
+
song_url = data['url']
|
| 41 |
+
player = discord.FFmpegPCMAudio(song_url, **ffmpeg_options)
|
| 42 |
+
|
| 43 |
+
# 更新當前歌曲的訊息,顯示歌曲名稱和點播人
|
| 44 |
+
if 'current_message' in queues[ctx.guild.id]:
|
| 45 |
+
message = queues[ctx.guild.id]['current_message']
|
| 46 |
+
await message.edit(content=f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 47 |
+
|
| 48 |
+
# 播放下一首歌曲
|
| 49 |
+
voice_clients[ctx.guild.id].play(player, after=lambda e: asyncio.run_coroutine_threadsafe(play_next(ctx), client.loop))
|
| 50 |
+
else:
|
| 51 |
+
# 播放清單空了,機器人將離開語音頻道
|
| 52 |
+
await ctx.send("播放清單已經播放完畢,機器人將離開語音頻道。")
|
| 53 |
|
| 54 |
@client.command(name="play")
|
| 55 |
async def play_music(ctx, *, link):
|
| 56 |
+
if ctx.guild.id not in voice_clients:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
+
voice_client = await ctx.author.voice.channel.connect()
|
| 59 |
+
voice_clients[ctx.guild.id] = voice_client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"Connection error: {e}")
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
# 判斷如果鏈接是YouTube的搜索鏈接,則進行處理
|
| 66 |
+
if any(not link.startswith(prefix) for prefix in url):
|
| 67 |
+
if youtube_base_url not in link:
|
| 68 |
+
query_string = urllib.parse.urlencode({'search_query': link})
|
| 69 |
+
content = urllib.request.urlopen(youtube_results_url + query_string)
|
| 70 |
+
search_results = re.findall(r'/watch\?v=(.{11})', content.read().decode())
|
| 71 |
+
link = youtube_watch_url + search_results[0]
|
| 72 |
+
|
| 73 |
+
# 獲取歌曲資訊
|
| 74 |
+
data = await asyncio.get_event_loop().run_in_executor(None, lambda: ytdl.extract_info(link, download=False))
|
| 75 |
+
title = data.get('title', 'Unknown Title')
|
| 76 |
+
uploader = data.get('uploader', 'Unknown User')
|
| 77 |
+
song_url = data['url']
|
| 78 |
+
player = discord.FFmpegPCMAudio(song_url, **ffmpeg_options)
|
| 79 |
+
|
| 80 |
+
# 儲存並顯示歌曲名稱和點播人
|
| 81 |
+
if ctx.guild.id not in queues:
|
| 82 |
+
queues[ctx.guild.id] = []
|
| 83 |
+
queues[ctx.guild.id].append(link)
|
| 84 |
+
|
| 85 |
+
# 如果有當前的播放訊息,更新顯示當前播放歌曲的標題和點播人
|
| 86 |
+
if 'current_message' in queues[ctx.guild.id]:
|
| 87 |
+
message = queues[ctx.guild.id]['current_message']
|
| 88 |
+
await message.edit(content=f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 89 |
+
else:
|
| 90 |
+
# 發送新的消息顯示當前歌曲
|
| 91 |
+
message = await ctx.send(f"Now playing: **{title}** by {uploader} requested by {ctx.author.mention}")
|
| 92 |
+
queues[ctx.guild.id]['current_message'] = message
|
| 93 |
+
|
| 94 |
+
# 播放歌曲
|
| 95 |
+
voice_clients[ctx.guild.id].play(player, after=lambda e: asyncio.run_coroutine_threadsafe(play_next(ctx), client.loop))
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Playback error: {e}")
|
| 99 |
+
await ctx.send(f"播放錯誤: {e}")
|
| 100 |
+
|
| 101 |
|
| 102 |
@client.command(name="clear_queue")
|
| 103 |
async def clear_queue(ctx):
|