diff --git a/src/pluralkit/bot/commands/system_commands.py b/src/pluralkit/bot/commands/system_commands.py index 2333fc61..76be807a 100644 --- a/src/pluralkit/bot/commands/system_commands.py +++ b/src/pluralkit/bot/commands/system_commands.py @@ -109,21 +109,7 @@ async def system_fronter(ctx: CommandContext): else: system = await ctx.ensure_system() - fronters, timestamp = await pluralkit.utils.get_fronters(ctx.conn, system_id=system.id) - fronter_names = [member.name for member in fronters] - - embed = embeds.status("") - - if len(fronter_names) == 0: - embed.add_field(name="Current fronter", value="(no fronter)") - elif len(fronter_names) == 1: - embed.add_field(name="Current fronter", value=fronter_names[0]) - else: - embed.add_field(name="Current fronters", value=", ".join(fronter_names)) - - if timestamp: - embed.add_field(name="Since", value="{} ({})".format(timestamp.isoformat(sep=" ", timespec="seconds"), - humanize.naturaltime(pluralkit.utils.fix_time(timestamp)))) + embed = await embeds.front_status(await system.get_latest_switch(ctx.conn), ctx.conn) await ctx.reply(embed=embed) diff --git a/src/pluralkit/bot/embeds.py b/src/pluralkit/bot/embeds.py index a1bdd967..b134bb1c 100644 --- a/src/pluralkit/bot/embeds.py +++ b/src/pluralkit/bot/embeds.py @@ -3,9 +3,11 @@ from typing import Tuple import discord +import pluralkit from pluralkit import db from pluralkit.bot.utils import escape from pluralkit.member import Member +from pluralkit.switch import Switch from pluralkit.system import System from pluralkit.utils import get_fronters @@ -149,4 +151,25 @@ async def member_card(conn, member: Member) -> discord.Embed: value=member.description, inline=False) card.set_footer(text="System ID: {} | Member ID: {}".format(system.hid, member.hid)) - return card \ No newline at end of file + return card + + +async def front_status(switch: Switch, conn) -> discord.Embed: + if switch: + embed = status("") + fronter_names = [member.name for member in await switch.fetch_members(conn)] + + if len(fronter_names) == 0: + embed.add_field(name="Current fronter", value="(no fronter)") + elif len(fronter_names) == 1: + embed.add_field(name="Current fronter", value=fronter_names[0]) + else: + embed.add_field(name="Current fronters", value=", ".join(fronter_names)) + + if switch.timestamp: + embed.add_field(name="Since", + value="{} ({})".format(switch.timestamp.isoformat(sep=" ", timespec="seconds"), + humanize.naturaltime(pluralkit.utils.fix_time(switch.timestamp)))) + else: + embed = error("No switches logged.") + return embed diff --git a/src/pluralkit/switch.py b/src/pluralkit/switch.py new file mode 100644 index 00000000..025691df --- /dev/null +++ b/src/pluralkit/switch.py @@ -0,0 +1,11 @@ +from collections import namedtuple + +from typing import List + +from pluralkit import db +from pluralkit.member import Member + + +class Switch(namedtuple("Switch", ["id", "system", "timestamp", "members"])): + async def fetch_members(self, conn) -> List[Member]: + return await db.get_members(conn, self.members) diff --git a/src/pluralkit/system.py b/src/pluralkit/system.py index e9febd9f..70e47f69 100644 --- a/src/pluralkit/system.py +++ b/src/pluralkit/system.py @@ -5,6 +5,7 @@ from typing import Optional, List from pluralkit import db, errors from pluralkit.member import Member +from pluralkit.switch import Switch from pluralkit.utils import generate_hid, contains_custom_emoji, validate_avatar_url_or_raise @@ -104,6 +105,18 @@ class System(namedtuple("System", ["id", "hid", "name", "description", "tag", "a async def get_members(self, conn) -> List[Member]: return await db.get_all_members(conn, self.id) + async def get_switches(self, conn, count) -> List[Switch]: + """Returns the latest `count` switches logged for this system, ordered latest to earliest.""" + return [Switch(**s) for s in await db.front_history(conn, self.id, count)] + + async def get_latest_switch(self, conn) -> Optional[Switch]: + """Returns the latest switch logged for this system, or None if no switches have been logged""" + switches = await self.get_switches(conn, 1) + if switches: + return switches[0] + else: + return None + def get_member_name_limit(self) -> int: """Returns the maximum length a member's name or nickname is allowed to be. Depends on the system tag.""" if self.tag: