-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonBot.py
More file actions
466 lines (379 loc) · 18.6 KB
/
pythonBot.py
File metadata and controls
466 lines (379 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import asyncio
import random
import discord
import os
from dotenv import load_dotenv
import requests
import json
import yt_dlp
import re
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
import logging
# Per-guild async queues and player tasks
guild_queues = {}
guild_inactivity_tasks = {}
guild_text_channels = {}
player_tasks = {}
def get_meme():
response = requests.get('https://meme-api.com/gimme')
json_data = json.loads(response.text)
print(json_data)
return json_data['url']
def get_dad():
URL = "https://icanhazdadjoke.com/"
headers = {
"Accept": "application/json", # get JSON instead of HTML
"User-Agent": "Discord Python Bot" # identify your app
}
response = requests.get(URL, headers=headers)
json_data = response.json()
return json_data['joke']
def get_yeah_nah():
URL="https://yesno.wtf/api"
response = requests.get(URL)
json_data = response.json()
response_json = [json_data['answer'],json_data['image']]
return response_json
async def _ensure_guild_queue(guild_id: int):
if guild_id not in guild_queues:
guild_queues[guild_id] = asyncio.Queue()
return guild_queues[guild_id]
async def check_inactivity_and_schedule(guild: discord.Guild, text_channel: discord.TextChannel = None):
"""Check if bot is alone and schedule disconnect if needed"""
guild_id = guild.id
vc = guild.voice_client
# Store text_channel if provided
if text_channel is not None:
guild_text_channels[guild_id] = text_channel
# If no voice client, cancel any existing timer and return
if vc is None:
if guild_id in guild_inactivity_tasks:
guild_inactivity_tasks[guild_id].cancel()
del guild_inactivity_tasks[guild_id]
return
# Count users in the voice channel
users_in_vc = [m for m in vc.channel.members if not m.bot]
# If users are present, cancel any existing timer
if len(users_in_vc) > 0:
if guild_id in guild_inactivity_tasks:
guild_inactivity_tasks[guild_id].cancel()
del guild_inactivity_tasks[guild_id]
return
# Bot is alone - start timer if not already running
if guild_id in guild_inactivity_tasks and not guild_inactivity_tasks[guild_id].done():
return # Timer already running, don't restart
async def disconnect_after_timeout():
try:
await asyncio.sleep(300) # 5 minutes
# Double-check before disconnecting
current_vc = guild.voice_client
if current_vc is None:
return
users_still_in_vc = [m for m in current_vc.channel.members if not m.bot]
if len(users_still_in_vc) == 0:
# Try to use stored text_channel first, then provided one, then search
channel_to_use = guild_text_channels.get(guild_id) or text_channel
if channel_to_use is None:
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
channel_to_use = channel
break
if channel_to_use:
await channel_to_use.send("I've been alone for 5 minutes... I guess nobody wants to listen to me anymore. I'm leaving now, baka!")
await current_vc.disconnect()
# Clean up stored text channel when disconnecting
if guild_id in guild_text_channels:
del guild_text_channels[guild_id]
except asyncio.CancelledError:
pass # Timer was cancelled, normal behavior
except Exception:
logging.exception('Error in inactivity disconnect')
finally:
# Clean up task reference
if guild_id in guild_inactivity_tasks:
del guild_inactivity_tasks[guild_id]
guild_inactivity_tasks[guild_id] = asyncio.create_task(disconnect_after_timeout())
async def _run_yt_dlp_info(url: str):
"""Extract metadata only, not the streaming URL"""
def extract():
ydl_opts = {
'format': 'bestaudio/best',
'noplaylist': True,
'skip_download': True, # Don't need the URL yet
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
if '_type' in info:
if info['_type'] == 'playlist':
info = info['entries'][0]
# Only store metadata, not the streaming URL
return {
'id': info.get('id', ''),
'duration': info.get('duration', 0),
'title': info.get('title'),
'webpage_url': info.get('webpage_url', url)
}
return await asyncio.to_thread(extract)
async def _get_fresh_stream_url(video_id_or_url: str):
"""Extract fresh streaming URL right before playback"""
def extract():
ydl_opts = {
'format': 'bestaudio[ext=m4a]/bestaudio[ext=opus]/bestaudio/best',
'noplaylist': True,
'quiet': True,
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'extractor_args': {
'youtube': {
'player_client': ['ios', 'android', 'web'],
'skip': ['hls']
}
}
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# If it's just an ID, construct the URL
url_to_extract = video_id_or_url
# If it's just an ID, construct the URL
if not url_to_extract.startswith('http'):
url_to_extract = f"https://www.youtube.com/watch?v={url_to_extract}"
info = ydl.extract_info(url_to_extract, download=False)
if '_type' in info and info['_type'] == 'playlist':
info = info['entries'][0]
return info['url']
return await asyncio.to_thread(extract)
async def startup(vc: discord.VoiceClient):
startupVoices = ["./joining voicelines/1.m4a","./joining voicelines/2.m4a","./joining voicelines/3.m4a","./joining voicelines/special.m4a"]
weights = [33,33,33,1]
choice = random.choices(startupVoices, weights=weights, k=1)[0]
vc.play(discord.FFmpegPCMAudio(choice))
while vc.is_playing():
await asyncio.sleep(0.5)
return
async def start_player_task_if_needed(guild: discord.Guild, voice_client: discord.VoiceClient, text_channel: discord.TextChannel):
# Start a background player loop per guild if not already running
if guild.id in player_tasks and not player_tasks[guild.id].done():
return
async def player_loop():
q = await _ensure_guild_queue(guild.id)
while True:
try:
item = await q.get()
except asyncio.CancelledError:
break
try:
vc = guild.voice_client
if vc is None:
logging.warning(f"No voice client for guild {guild.id}, skipping playback")
continue
# Extract fresh streaming URL right before playing
try:
stream_url = await _get_fresh_stream_url(item.get('id') or item.get('webpage_url'))
except Exception as e:
logging.exception('Failed to get streaming URL')
await text_channel.send(f"Failed to play: {item.get('title', 'Unknown')} - Could not retrieve stream")
continue
source = discord.FFmpegOpusAudio(stream_url, bitrate=128, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', options='-vn')
await text_channel.send(f"Now Playing: {item['title']}")
vc.play(source)
# Wait for playback to finish without blocking the loop
while vc.is_playing() or vc.is_paused():
await asyncio.sleep(1)
except Exception as e:
logging.exception('Error during playback')
await text_channel.send(f"Failed to play: {item.get('title', 'Unknown')}")
finally:
q.task_done()
player_tasks[guild.id] = asyncio.create_task(player_loop())
class MyClient(discord.Client):
async def on_ready(self):
await tree.sync()
print('Tree synced')
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):
if message.author == self.user:
return
async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
"""Monitor voice channel activity"""
voice_client = member.guild.voice_client
# Only care if bot is connected and the change involves the bot's channel
if voice_client is None:
return
# Check if the state change involves the bot's current channel
bot_channel = voice_client.channel
if before.channel == bot_channel or after.channel == bot_channel:
# Something changed in bot's channel, re-evaluate inactivity
await check_inactivity_and_schedule(member.guild)
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
tree = discord.app_commands.CommandTree(client)
@tree.command(name="meme", description="Sends a random meme")
async def meme(interaction: discord.Interaction):
await interaction.response.send_message(get_meme())
@tree.command(name="ping", description="Replies with pong!")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message("pong!")
@tree.command(name="dadjoke", description="Sends a random dad joke")
async def dadjoke(interaction: discord.Interaction):
await interaction.response.send_message(get_dad())
@tree.command(name="yayornay", description="Sends a random yes or no with gif")
async def yayornay(interaction: discord.Interaction):
answerArray = get_yeah_nah()
await interaction.response.send_message(answerArray[0])
await interaction.followup.send(answerArray[1])
@tree.command(name="leave", description="Disconnects the bot from the voice channel")
async def leave(interaction: discord.Interaction):
voice_client = interaction.guild.voice_client
if voice_client is not None:
await voice_client.disconnect()
if interaction.user.name == "mossv":
await interaction.response.send_message("I have retreated from the discussion chambers, My Creator")
else:
await interaction.response.send_message(f"Disconnected from the voice channel, {interaction.user.name} Onii Sama.")
else:
await interaction.response.send_message("I am not connected to any voice channel, are you dumb?")
@tree.command(name="skip", description="Skips the current audio track")
async def skip(interaction: discord.Interaction):
voice_client = interaction.guild.voice_client
if voice_client is None or not voice_client.is_playing():
await interaction.response.send_message("No audio is currently playing.")
return
voice_client.stop()
await interaction.response.send_message(f"Does this mean you want me to play the next track, {interaction.user.name} Onii Sama?")
return
@tree.command(name="queue", description="Shows the current audio queue")
async def show_queue(interaction: discord.Interaction):
q = await _ensure_guild_queue(interaction.guild.id)
if q.empty():
await interaction.response.send_message("The audio queue is currently empty.")
return
queue_list = []
temp_queue = asyncio.Queue()
total_duration = 0
while not q.empty():
item = await q.get()
total_duration += item.get('duration', 0)
queue_list.append(item.get('title', 'Unknown Title'))
await temp_queue.put(item)
q.task_done()
# Restore the original queue
while not temp_queue.empty():
item = await temp_queue.get()
await q.put(item)
temp_queue.task_done()
if total_duration >= 3600:
hours = total_duration // 3600
minutes = (total_duration % 3600) // 60
seconds = total_duration % 60
duration_str = f"{hours}h {minutes}m {seconds}s in queue"
elif total_duration >= 60:
minutes = total_duration // 60
seconds = total_duration % 60
duration_str = f"{minutes}m {seconds}s in queue"
else:
duration_str = f"{total_duration}s in queue"
queue_message = "Current Audio Queue waiting:\n" + "\n".join(f"{idx + 1}. {title}" for idx, title in enumerate(queue_list)) + f"\nTotal Duration: {duration_str}"
await interaction.response.send_message(queue_message)
@tree.command(name="pause", description="Pauses the current audio track")
async def pause(interaction: discord.Interaction):
voice_client = interaction.guild.voice_client
if voice_client is None or not voice_client.is_playing():
await interaction.response.send_message("No audio is currently playing.")
return
voice_client.pause()
await interaction.response.send_message(f"Audio paused, {interaction.user.name} Onii Sama.")
return
@tree.command(name="resume", description="Resumes the paused audio track")
async def resume(interaction: discord.Interaction):
voice_client = interaction.guild.voice_client
if voice_client is None or not voice_client.is_paused():
await interaction.response.send_message("No audio is currently paused.")
return
voice_client.resume()
await interaction.response.send_message(f"Audio resumed, {interaction.user.name} Onii Sama.")
return
#TODO: implement search functionality.
# Milestone 4: Add Search-Specific Features (Optional Enhancements)
# Description: Once basic search works, add niceties like multiple result options or search limits.
# Key Tasks:
# Modify yt_dlp options to limit search results (e.g., 'default_search': 'ytsearch5' for top 5).
# Optionally, add a new /search command that lists results (e.g., "1. Title - URL\n2. ...") and lets users choose via reactions or a follow-up command.
# Integrate playlist support if yt_dlp finds one (e.g., enqueue all tracks from a search result).
# Effort: Medium-High (4-8 hours). Requires UI changes for selection.
# Testing: Test with popular queries and ensure enqueuing works for multiple tracks.
# Milestone 5: Full Integration and Edge Case Testing
# Description: Ensure searching works across guilds, handles concurrent searches, and integrates with the queue/player system.
# Key Tasks:
# Test per-guild queues with searches (e.g., multiple users searching simultaneously).
# Handle edge cases: Very long queries, special characters, non-YouTube sources (if yt_dlp supports them), or rate limits.
# Add rate limiting or caching if needed (e.g., avoid duplicate searches).
# Effort: Medium (2-4 hours). Focus on real-world scenarios.
# Testing: Run the bot in a test server, add multiple searches quickly, and monitor for heartbeat issues or crashes.
# Milestone 6: Documentation and Deployment
# Description: Finalize and document the feature.
# Key Tasks:
# Update readme.md with examples (e.g., "/play never gonna give you up").
# Add comments in code for the search logic.
# Deploy and monitor in production for any yt_dlp updates or API changes.
# Effort: Low (1 hour).
# Testing: Share with a small group and gather feedback.
@tree.command(name="play", description="Plays audio from a YouTube URL or search query, URLs can be stacked with spaces in between")
async def play(interaction: discord.Interaction, search_or_url: str):
await interaction.response.defer() # Acknowledge the command to avoid timeout
user = interaction.user
text_channel = interaction.channel
# await join_channel(user)
currentChannel = user.voice
if currentChannel is not None:
voice_channel = currentChannel.channel
if interaction.guild.voice_client is None:
await voice_channel.connect()
await startup(voice_channel.guild.voice_client)
voice_client = interaction.guild.voice_client
if voice_client is None:
await interaction.followup.send("Bot is not connected to a voice channel.")
return
q = await _ensure_guild_queue(interaction.guild.id)
urls = re.findall(r'https?://\S+', search_or_url)
title_of_urls = []
# Extract info using yt_dlp in a thread so we don't block the event loop
if urls:
for url in urls:
try:
info = await _run_yt_dlp_info(url)
except Exception:
logging.exception('Failed to extract info')
await interaction.followup.send(f'Failed to retrieve info for: {url}')
return
title_of_urls.append(info.get('title'))
# Ensure guild queue exists and enqueue the track
await q.put(info)
else:
# Treat search_or_url as a search query
search_query = f"ytsearch:{search_or_url}"
try:
info = await _run_yt_dlp_info(search_query)
except Exception:
logging.exception('Failed to extract info for search query')
await interaction.followup.send(f'Failed to retrieve info for search query: {search_or_url}')
return
# Ensure guild queue exists and enqueue the track
await q.put(info)
if info.get('id', '') == '':
url = f"{user.name} Onii Sama, has asked me to search this up: {search_or_url}"
else:
url = f"https://www.youtube.com/watch?v={info.get('id', '')}"
guild_text_channels[interaction.guild.id] = text_channel
# Start background player task for this guild if not running
await start_player_task_if_needed(interaction.guild, voice_client, text_channel)
# Respond to user
title = info.get('title') or url
if len(urls) > 1:
await interaction.followup.send(f'My Onii Sama {user.name} wants me to play the following tracks, gosh Onii Sama, you\' re so annoying.\n\n' + '\n'.join(title_of_urls))
else:
await interaction.followup.send(f'My Onii Sama {user.name} wants {title}, its not like I wanted to play it or anything.\n{url}')
await check_inactivity_and_schedule(interaction.guild, text_channel)
if DISCORD_TOKEN is None:
raise ValueError("DISCORD_TOKEN environment variable is not set")
client.run(DISCORD_TOKEN)