Added Disconnect
This commit is contained in:
26
bot.py
26
bot.py
@@ -1,6 +1,7 @@
|
|||||||
import numba_config
|
import numba_config
|
||||||
import asyncio
|
import asyncio
|
||||||
import io
|
import io
|
||||||
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
@@ -13,6 +14,10 @@ from config import Config
|
|||||||
from voice_manager import VoiceManager
|
from voice_manager import VoiceManager
|
||||||
|
|
||||||
|
|
||||||
|
# Inactivity timeout in seconds (10 minutes)
|
||||||
|
INACTIVITY_TIMEOUT = 10 * 60
|
||||||
|
|
||||||
|
|
||||||
class TTSBot(commands.Bot):
|
class TTSBot(commands.Bot):
|
||||||
"""Discord bot that reads messages aloud using Pocket TTS."""
|
"""Discord bot that reads messages aloud using Pocket TTS."""
|
||||||
|
|
||||||
@@ -24,6 +29,7 @@ class TTSBot(commands.Bot):
|
|||||||
|
|
||||||
self.voice_manager = VoiceManager(Config.VOICES_DIR, Config.DEFAULT_VOICE)
|
self.voice_manager = VoiceManager(Config.VOICES_DIR, Config.DEFAULT_VOICE)
|
||||||
self.message_queue: asyncio.Queue[tuple[discord.Message, str]] = asyncio.Queue()
|
self.message_queue: asyncio.Queue[tuple[discord.Message, str]] = asyncio.Queue()
|
||||||
|
self.last_activity: float = 0.0
|
||||||
|
|
||||||
self._setup_slash_commands()
|
self._setup_slash_commands()
|
||||||
|
|
||||||
@@ -201,6 +207,7 @@ class TTSBot(commands.Bot):
|
|||||||
await asyncio.to_thread(self.voice_manager.get_voice_state, default)
|
await asyncio.to_thread(self.voice_manager.get_voice_state, default)
|
||||||
|
|
||||||
self.loop.create_task(self.process_queue())
|
self.loop.create_task(self.process_queue())
|
||||||
|
self.loop.create_task(self.check_inactivity())
|
||||||
|
|
||||||
# Sync slash commands
|
# Sync slash commands
|
||||||
print("Syncing slash commands...")
|
print("Syncing slash commands...")
|
||||||
@@ -295,6 +302,7 @@ class TTSBot(commands.Bot):
|
|||||||
self.loop.call_soon_threadsafe(play_complete.set)
|
self.loop.call_soon_threadsafe(play_complete.set)
|
||||||
|
|
||||||
voice_client.play(audio_source, after=after_playing)
|
voice_client.play(audio_source, after=after_playing)
|
||||||
|
self.last_activity = time.time()
|
||||||
print(f"Playing audio in {voice_channel.name}")
|
print(f"Playing audio in {voice_channel.name}")
|
||||||
|
|
||||||
await play_complete.wait()
|
await play_complete.wait()
|
||||||
@@ -321,6 +329,23 @@ class TTSBot(commands.Bot):
|
|||||||
wav_buffer.seek(0)
|
wav_buffer.seek(0)
|
||||||
return wav_buffer.read()
|
return wav_buffer.read()
|
||||||
|
|
||||||
|
async def check_inactivity(self) -> None:
|
||||||
|
"""Periodically check for inactivity and disconnect from voice channels."""
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(60) # Check every minute
|
||||||
|
|
||||||
|
if self.last_activity == 0.0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
elapsed = time.time() - self.last_activity
|
||||||
|
if elapsed >= INACTIVITY_TIMEOUT:
|
||||||
|
# Disconnect from all voice channels
|
||||||
|
for guild in self.guilds:
|
||||||
|
if guild.voice_client is not None:
|
||||||
|
print(f"Disconnecting from {guild.name} due to inactivity")
|
||||||
|
await guild.voice_client.disconnect()
|
||||||
|
self.last_activity = 0.0
|
||||||
|
|
||||||
async def ensure_voice_connection(self, channel: discord.VoiceChannel) -> discord.VoiceClient | None:
|
async def ensure_voice_connection(self, channel: discord.VoiceChannel) -> discord.VoiceClient | None:
|
||||||
"""Ensure we're connected to the specified voice channel."""
|
"""Ensure we're connected to the specified voice channel."""
|
||||||
guild = channel.guild
|
guild = channel.guild
|
||||||
@@ -333,6 +358,7 @@ class TTSBot(commands.Bot):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
voice_client = await channel.connect(timeout=10.0)
|
voice_client = await channel.connect(timeout=10.0)
|
||||||
|
self.last_activity = time.time()
|
||||||
return voice_client
|
return voice_client
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to connect to voice channel: {e}")
|
print(f"Failed to connect to voice channel: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user