Compare commits

..

4 Commits

Author SHA1 Message Date
0cfc9b9986 fix: Fix typo in end_session. 2026-02-25 19:37:16 +10:00
e05dfbcc5f fix: Typos in end_session. 2026-02-25 19:34:45 +10:00
dff1eb3185 feat: Add db voice session tracking 2026-02-25 18:44:50 +10:00
4da04dc7d5 Fix typos 2025-10-24 19:08:22 +10:00
3 changed files with 77 additions and 5 deletions

View File

@@ -5,9 +5,20 @@ CREATE TABLE voicelog_guilds(
webhook_url TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
_timestamp TIMESTAMPTZ DEFAULT NOW()
)
);
CREATE TRIGGER voicelog_guilds_timestamp BEFORE UPDATE ON voicelog_guilds
FOR EACH ROW EXECUTE FUNCTION update_timestamp_column();
END;
CREATE TABLE voicelog_sessions(
sessionid INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
guildid BIGINT NOT NULL REFERENCES voicelog_guilds (guildid) ON DELETE CASCADE ON UPDATE CASCADE,
userid BIGINT NOT NULL,
channelid BIGINT NOT NULL,
joined_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
duration INTEGER
);
CREATE INDEX voicelog_sessions_guildid_userid ON voicelog_sessions (guildid, userid);
COMMIT;

View File

@@ -12,5 +12,18 @@ class VoiceLogGuild(RowModel):
_timestamp = Timestamp()
class VoiceLogSession(RowModel):
_tablename_ = "voicelog_sessions"
_cache_ = {}
sessionid = Integer(primary=True)
guildid = Integer()
userid = Integer()
channelid = Integer()
joined_at = Timestamp()
duration = Integer()
class VoiceLogData(Registry):
voicelog_guilds = VoiceLogGuild.table
voicelog_sessions = VoiceLogSession.table

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
@@ -39,9 +40,10 @@ class VoiceLogCog(LionCog):
if row is None or row.webhook_url is None:
return
hook = ThreadedWebhook.from_url(logger.webhook_url, client=self.bot)
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,54 @@ 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.",
)
else:
embed.add_field(
name="Session",
value=f"Voice session lasted {session.duration} seconds!",
)
if after_channel:
session = await self.start_voice_session(member, after_channel, now)
if session:
embed.set_footer(text=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.guild.id,
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"
)