From f8235370c71c4b1b99be587a093081712d5ceb2d Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 16 Sep 2018 19:43:13 +0200 Subject: [PATCH] Refactor more db methods into the models --- src/pluralkit/bot/embeds.py | 14 ++++++-------- src/pluralkit/member.py | 9 ++++++++- src/pluralkit/system.py | 8 +++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pluralkit/bot/embeds.py b/src/pluralkit/bot/embeds.py index dc323f81..3f431944 100644 --- a/src/pluralkit/bot/embeds.py +++ b/src/pluralkit/bot/embeds.py @@ -69,9 +69,10 @@ async def system_card(conn, client: discord.Client, system: System) -> discord.E card.add_field(name="Current fronter" if len(fronters) == 1 else "Current fronters", value=fronter_val) account_names = [] - for account_id in await db.get_linked_accounts(conn, system_id=system.id): - account = await client.get_user_info(account_id) + for account_id in await system.get_linked_account_ids(conn): + account = await client.get_user_info(str(account_id)) account_names.append("{}#{}".format(account.name, account.discriminator)) + card.add_field(name="Linked accounts", value="\n".join(account_names)) if system.description: @@ -80,7 +81,7 @@ async def system_card(conn, client: discord.Client, system: System) -> discord.E # Get names of all members member_texts = [] - for member in await db.get_all_members(conn, system_id=system.id): + for member in await system.get_members(conn): member_texts.append("{} (`{}`)".format(escape(member.name), member.hid)) if len(member_texts) > 0: @@ -92,7 +93,7 @@ async def system_card(conn, client: discord.Client, system: System) -> discord.E async def member_card(conn, member: Member) -> discord.Embed: - system = await db.get_system(conn, system_id=member.system) + system = await member.fetch_system(conn) card = discord.Embed() card.colour = discord.Colour.blue() @@ -105,9 +106,6 @@ async def member_card(conn, member: Member) -> discord.Embed: if member.avatar_url: card.set_thumbnail(url=member.avatar_url) - # Get system name and hid - system = await db.get_system(conn, system_id=member.system) - if member.color: card.colour = int(member.color, 16) @@ -120,7 +118,7 @@ async def member_card(conn, member: Member) -> discord.Embed: if member.pronouns: card.add_field(name="Pronouns", value=member.pronouns) - message_count = await db.get_member_message_count(conn, member.id) + message_count = await member.message_count(conn) if message_count > 0: card.add_field(name="Message Count", value=str(message_count), inline=True) diff --git a/src/pluralkit/member.py b/src/pluralkit/member.py index 677aca99..a02f4c5b 100644 --- a/src/pluralkit/member.py +++ b/src/pluralkit/member.py @@ -124,4 +124,11 @@ class Member(namedtuple("Member", async def delete(self, conn): """Delete this member from the database.""" - await db.delete_member(conn, self.id) \ No newline at end of file + await db.delete_member(conn, self.id) + + async def fetch_system(self, conn) -> "System": + """Fetch the member's system from the database""" + return await db.get_system(conn, self.system) + + async def message_count(self, conn) -> int: + return await db.get_member_message_count(conn, self.id) \ No newline at end of file diff --git a/src/pluralkit/system.py b/src/pluralkit/system.py index e1d5d231..e9febd9f 100644 --- a/src/pluralkit/system.py +++ b/src/pluralkit/system.py @@ -1,7 +1,7 @@ from datetime import datetime from collections.__init__ import namedtuple -from typing import Optional +from typing import Optional, List from pluralkit import db, errors from pluralkit.member import Member @@ -85,6 +85,9 @@ class System(namedtuple("System", ["id", "hid", "name", "description", "tag", "a await db.unlink_account(conn, self.id, account_id) + async def get_linked_account_ids(self, conn) -> List[int]: + return await db.get_linked_accounts(conn, self.id) + async def delete(self, conn): await db.remove_system(conn, self.id) @@ -98,6 +101,9 @@ class System(namedtuple("System", ["id", "hid", "name", "description", "tag", "a member = await db.create_member(conn, self.id, member_name, new_hid) return member + async def get_members(self, conn) -> List[Member]: + return await db.get_all_members(conn, self.id) + 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: