From 6951c1516745379bd06dd9f39a574291e6656cd6 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 16 Sep 2018 19:36:50 +0200 Subject: [PATCH] Move system/member card embeds to embeds.py --- src/pluralkit/bot/commands/member_commands.py | 3 +- src/pluralkit/bot/commands/system_commands.py | 3 +- src/pluralkit/bot/embeds.py | 98 ++++++++++++++++++- src/pluralkit/bot/utils.py | 92 +---------------- 4 files changed, 102 insertions(+), 94 deletions(-) diff --git a/src/pluralkit/bot/commands/member_commands.py b/src/pluralkit/bot/commands/member_commands.py index b8577300..e5dad360 100644 --- a/src/pluralkit/bot/commands/member_commands.py +++ b/src/pluralkit/bot/commands/member_commands.py @@ -1,5 +1,6 @@ from datetime import datetime +import pluralkit.bot.embeds from pluralkit.bot import help from pluralkit.bot.commands import * from pluralkit.errors import PluralKitError @@ -10,7 +11,7 @@ logger = logging.getLogger("pluralkit.commands") async def member_info(ctx: CommandContext): member = await ctx.pop_member( error=CommandError("You must pass a member name or ID.", help=help.lookup_member), system_only=False) - await ctx.reply(embed=await utils.generate_member_info_card(ctx.conn, member)) + await ctx.reply(embed=await pluralkit.bot.embeds.member_card(ctx.conn, member)) async def new_member(ctx: CommandContext): diff --git a/src/pluralkit/bot/commands/system_commands.py b/src/pluralkit/bot/commands/system_commands.py index 2dfbb04f..2333fc61 100644 --- a/src/pluralkit/bot/commands/system_commands.py +++ b/src/pluralkit/bot/commands/system_commands.py @@ -2,6 +2,7 @@ import dateparser import humanize from datetime import datetime, timedelta +import pluralkit.bot.embeds import pluralkit.utils from pluralkit.bot import help from pluralkit.bot.commands import * @@ -16,7 +17,7 @@ async def system_info(ctx: CommandContext): else: system = await ctx.ensure_system() - await ctx.reply(embed=await utils.generate_system_info_card(ctx.conn, ctx.client, system)) + await ctx.reply(embed=await pluralkit.bot.embeds.system_card(ctx.conn, ctx.client, system)) async def new_system(ctx: CommandContext): diff --git a/src/pluralkit/bot/embeds.py b/src/pluralkit/bot/embeds.py index 9c48df8e..dc323f81 100644 --- a/src/pluralkit/bot/embeds.py +++ b/src/pluralkit/bot/embeds.py @@ -1,7 +1,14 @@ +import humanize from typing import Tuple import discord +from pluralkit import db +from pluralkit.bot.utils import escape +from pluralkit.member import Member +from pluralkit.system import System +from pluralkit.utils import get_fronters + def success(text: str) -> discord.Embed: embed = discord.Embed() @@ -39,4 +46,93 @@ def exception_log(message_content, author_name, author_discriminator, server_id, server_id if server_id else "(DMs)", channel_id )) - return embed \ No newline at end of file + return embed + + +async def system_card(conn, client: discord.Client, system: System) -> discord.Embed: + card = discord.Embed() + card.colour = discord.Colour.blue() + + if system.name: + card.title = system.name + + if system.avatar_url: + card.set_thumbnail(url=system.avatar_url) + + if system.tag: + card.add_field(name="Tag", value=system.tag) + + fronters, switch_time = await get_fronters(conn, system.id) + if fronters: + names = ", ".join([member.name for member in fronters]) + fronter_val = "{} (for {})".format(names, humanize.naturaldelta(switch_time)) + 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) + account_names.append("{}#{}".format(account.name, account.discriminator)) + card.add_field(name="Linked accounts", value="\n".join(account_names)) + + if system.description: + card.add_field(name="Description", + value=system.description, inline=False) + + # Get names of all members + member_texts = [] + for member in await db.get_all_members(conn, system_id=system.id): + member_texts.append("{} (`{}`)".format(escape(member.name), member.hid)) + + if len(member_texts) > 0: + card.add_field(name="Members", value="\n".join( + member_texts), inline=False) + + card.set_footer(text="System ID: {}".format(system.hid)) + return card + + +async def member_card(conn, member: Member) -> discord.Embed: + system = await db.get_system(conn, system_id=member.system) + + card = discord.Embed() + card.colour = discord.Colour.blue() + + name_and_system = member.name + if system.name: + name_and_system += " ({})".format(system.name) + + card.set_author(name=name_and_system, icon_url=member.avatar_url or discord.Embed.Empty) + 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) + + if member.birthday: + bday_val = member.birthday.strftime("%b %d, %Y") + if member.birthday.year == 1: + bday_val = member.birthday.strftime("%b %d") + card.add_field(name="Birthdate", value=bday_val) + + if member.pronouns: + card.add_field(name="Pronouns", value=member.pronouns) + + message_count = await db.get_member_message_count(conn, member.id) + if message_count > 0: + card.add_field(name="Message Count", value=str(message_count), inline=True) + + if member.prefix or member.suffix: + prefix = member.prefix or "" + suffix = member.suffix or "" + card.add_field(name="Proxy Tags", + value="{}text{}".format(prefix, suffix)) + + if member.description: + card.add_field(name="Description", + 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 diff --git a/src/pluralkit/bot/utils.py b/src/pluralkit/bot/utils.py index f74cb21a..f4cf2d8c 100644 --- a/src/pluralkit/bot/utils.py +++ b/src/pluralkit/bot/utils.py @@ -2,12 +2,10 @@ import logging import re import discord -import humanize from pluralkit import db from pluralkit.system import System from pluralkit.member import Member -from pluralkit.utils import get_fronters logger = logging.getLogger("pluralkit.utils") @@ -76,92 +74,4 @@ async def get_member_fuzzy(conn, system_id: int, key: str, system_only=True) -> if system_id: member = await db.get_member_by_name(conn, system_id=system_id, member_name=key) if member is not None: - return member - -async def generate_system_info_card(conn, client: discord.Client, system: System) -> discord.Embed: - card = discord.Embed() - card.colour = discord.Colour.blue() - - if system.name: - card.title = system.name - - if system.avatar_url: - card.set_thumbnail(url=system.avatar_url) - - if system.tag: - card.add_field(name="Tag", value=system.tag) - - fronters, switch_time = await get_fronters(conn, system.id) - if fronters: - names = ", ".join([member.name for member in fronters]) - fronter_val = "{} (for {})".format(names, humanize.naturaldelta(switch_time)) - 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) - account_names.append("{}#{}".format(account.name, account.discriminator)) - card.add_field(name="Linked accounts", value="\n".join(account_names)) - - if system.description: - card.add_field(name="Description", - value=system.description, inline=False) - - # Get names of all members - member_texts = [] - for member in await db.get_all_members(conn, system_id=system.id): - member_texts.append("{} (`{}`)".format(escape(member.name), member.hid)) - - if len(member_texts) > 0: - card.add_field(name="Members", value="\n".join( - member_texts), inline=False) - - card.set_footer(text="System ID: {}".format(system.hid)) - return card - - -async def generate_member_info_card(conn, member: Member) -> discord.Embed: - system = await db.get_system(conn, system_id=member.system) - - card = discord.Embed() - card.colour = discord.Colour.blue() - - name_and_system = member.name - if system.name: - name_and_system += " ({})".format(system.name) - - card.set_author(name=name_and_system, icon_url=member.avatar_url or discord.Embed.Empty) - 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) - - if member.birthday: - bday_val = member.birthday.strftime("%b %d, %Y") - if member.birthday.year == 1: - bday_val = member.birthday.strftime("%b %d") - card.add_field(name="Birthdate", value=bday_val) - - if member.pronouns: - card.add_field(name="Pronouns", value=member.pronouns) - - message_count = await db.get_member_message_count(conn, member.id) - if message_count > 0: - card.add_field(name="Message Count", value=str(message_count), inline=True) - - if member.prefix or member.suffix: - prefix = member.prefix or "" - suffix = member.suffix or "" - card.add_field(name="Proxy Tags", - value="{}text{}".format(prefix, suffix)) - - if member.description: - card.add_field(name="Description", - value=member.description, inline=False) - - card.set_footer(text="System ID: {} | Member ID: {}".format(system.hid, member.hid)) - return card + return member \ No newline at end of file