Add basic switch model and migrate fronter command

This commit is contained in:
Ske 2018-10-11 12:54:40 +02:00
parent 31322f3fea
commit 4a48e43d13
4 changed files with 49 additions and 16 deletions

View File

@ -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)

View File

@ -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
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

11
src/pluralkit/switch.py Normal file
View File

@ -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)

View File

@ -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: