Refactor more db methods into the models

This commit is contained in:
Ske 2018-09-16 19:43:13 +02:00
parent f4f312fd9a
commit f8235370c7
3 changed files with 21 additions and 10 deletions

View File

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

View File

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

View File

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