fixed youtube's api interaction, fixed discord connection handshake errors
This commit is contained in:
@@ -3,6 +3,10 @@ from discord.ext.commands.context import Context
|
||||
from discord.ext.commands.converter import CommandError
|
||||
import config
|
||||
from . import queue
|
||||
import asyncio
|
||||
|
||||
# Track last activity time for each server
|
||||
last_activity = {}
|
||||
|
||||
# Joining/moving to the user's vc in a guild
|
||||
async def join_vc(ctx: Context):
|
||||
@@ -20,11 +24,14 @@ async def join_vc(ctx: Context):
|
||||
|
||||
# Join or move to the user's vc
|
||||
if ctx.voice_client is None:
|
||||
vc = await vc.connect()
|
||||
vc = await vc.connect(reconnect=True, timeout=60.0)
|
||||
else:
|
||||
# Safe to ignore type error for now
|
||||
vc = await ctx.voice_client.move_to(vc)
|
||||
|
||||
# Update last activity
|
||||
last_activity[ctx.guild.id] = asyncio.get_event_loop().time()
|
||||
|
||||
return vc
|
||||
|
||||
|
||||
@@ -45,8 +52,60 @@ async def leave_vc(ctx: Context):
|
||||
if author_vc is None or vc != author_vc:
|
||||
raise CommandError("You are not in this voice channel")
|
||||
|
||||
# Disconnect
|
||||
await ctx.voice_client.disconnect(force=False)
|
||||
# Clear the queue for this server
|
||||
await queue.clear(ctx.guild.id)
|
||||
|
||||
# Stop any currently playing audio
|
||||
if ctx.voice_client.is_playing():
|
||||
ctx.voice_client.stop()
|
||||
|
||||
# Disconnect with force to ensure it actually leaves
|
||||
try:
|
||||
await ctx.voice_client.disconnect(force=True)
|
||||
except Exception as e:
|
||||
print(f"Error disconnecting: {e}")
|
||||
# If regular disconnect fails, try cleanup
|
||||
await ctx.voice_client.cleanup()
|
||||
|
||||
# Remove from activity tracker
|
||||
if ctx.guild.id in last_activity:
|
||||
del last_activity[ctx.guild.id]
|
||||
|
||||
|
||||
# Auto-disconnect if inactive
|
||||
async def check_inactivity(bot):
|
||||
"""Background task to check for inactive voice connections"""
|
||||
while True:
|
||||
try:
|
||||
current_time = asyncio.get_event_loop().time()
|
||||
|
||||
for guild_id, last_time in list(last_activity.items()):
|
||||
# If inactive for more than 5 minutes
|
||||
if current_time - last_time > 300: # 300 seconds = 5 minutes
|
||||
# Find the guild and voice client
|
||||
guild = bot.get_guild(guild_id)
|
||||
if guild and guild.voice_client:
|
||||
# Check if not playing
|
||||
if not guild.voice_client.is_playing():
|
||||
print(f"Auto-disconnecting from {guild.name} due to inactivity")
|
||||
await queue.clear(guild_id)
|
||||
try:
|
||||
await guild.voice_client.disconnect(force=True)
|
||||
except:
|
||||
pass
|
||||
del last_activity[guild_id]
|
||||
|
||||
# Check every 30 seconds
|
||||
await asyncio.sleep(30)
|
||||
except Exception as e:
|
||||
print(f"Error in inactivity checker: {e}")
|
||||
await asyncio.sleep(30)
|
||||
|
||||
|
||||
# Update activity timestamp when playing
|
||||
def update_activity(guild_id):
|
||||
"""Call this when a song starts playing"""
|
||||
last_activity[guild_id] = asyncio.get_event_loop().time()
|
||||
|
||||
|
||||
# Build a display message for queuing a new song
|
||||
@@ -71,7 +130,9 @@ async def display_server_queue(ctx: Context, songs, n):
|
||||
title=f"{server.name}'s Queue!",
|
||||
color=config.get_color("main"))
|
||||
|
||||
display = f"🔊 Currently playing: ``{await queue.get_current_song(ctx.guild.id)}``\n\n"
|
||||
current_song = await queue.get_current_song(ctx.guild.id)
|
||||
display = f"🔊 Currently playing: ``{current_song}``\n\n"
|
||||
|
||||
for i, song in enumerate(songs):
|
||||
|
||||
# If text is not avaialable do not display
|
||||
|
||||
Reference in New Issue
Block a user