feat: Add db voice session tracking

This commit is contained in:
2026-02-25 18:44:50 +10:00
parent 4da04dc7d5
commit dff1eb3185
3 changed files with 69 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
from data.queries import ORDER
import discord
from discord.ext import commands as cmds
from discord import app_commands as appcmds
@@ -7,7 +8,7 @@ from meta.logger import log_wrap
from utils.lib import utc_now
from . import logger
from ..data import VoiceLogData, VoiceLogGuild
from ..data import VoiceLogData, VoiceLogGuild, VoiceLogSession
from .lib import ThreadedWebhook
@@ -41,7 +42,8 @@ class VoiceLogCog(LionCog):
hook = ThreadedWebhook.from_url(row.webhook_url, client=self.bot)
embed = discord.Embed(timestamp=utc_now())
now = utc_now()
embed = discord.Embed(timestamp=now)
if before_channel is None:
embed.title = "User Joined Voice Channel"
@@ -53,8 +55,49 @@ class VoiceLogCog(LionCog):
embed.title = "User Switched Voice Channel"
embed.description = f"{member.mention} moved from {before_channel.mention} to {after_channel.mention}"
session = None
if before_channel:
session = await self.end_voice_session(member, before_channel, now)
if not session:
embed.add_field(
name="Warning",
value="Could not find voice session to end, statistics may be incorrect.",
)
if after_channel:
session = await self.start_voice_session(member, after_channel, now)
if session:
embed.add_footer(f"Session #{session.sessionid}")
await hook.send(embed=embed)
async def start_voice_session(self, member, channel, started_at):
session = await VoiceLogSession.create(
guildid=member.guild.id,
userid=member.id,
channelid=channel.id,
joined_at=started_at,
)
return session
async def end_voice_session(self, member, channel, ended_at):
# Get the last open voice session to close it
open_sessions = await VoiceLogSession.fetch_where(
guildid=member.guildid,
userid=member.id,
channelid=channel.id,
duration=None,
).order_by("joined_at", ORDER.DESC)
if not open_sessions:
session = None
else:
session = open_sessions[0]
await session.update(
duration=int((ended_at - session.joined_at).total_seconds())
)
return session
@cmds.hybrid_group(
name="voicelog", description="Base command group for the voice logging system"
)