Move system/member card embeds to embeds.py

This commit is contained in:
Ske 2018-09-16 19:36:50 +02:00
parent 345e8baab6
commit 6951c15167
4 changed files with 102 additions and 94 deletions

View File

@ -1,5 +1,6 @@
from datetime import datetime from datetime import datetime
import pluralkit.bot.embeds
from pluralkit.bot import help from pluralkit.bot import help
from pluralkit.bot.commands import * from pluralkit.bot.commands import *
from pluralkit.errors import PluralKitError from pluralkit.errors import PluralKitError
@ -10,7 +11,7 @@ logger = logging.getLogger("pluralkit.commands")
async def member_info(ctx: CommandContext): async def member_info(ctx: CommandContext):
member = await ctx.pop_member( member = await ctx.pop_member(
error=CommandError("You must pass a member name or ID.", help=help.lookup_member), system_only=False) 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): async def new_member(ctx: CommandContext):

View File

@ -2,6 +2,7 @@ import dateparser
import humanize import humanize
from datetime import datetime, timedelta from datetime import datetime, timedelta
import pluralkit.bot.embeds
import pluralkit.utils import pluralkit.utils
from pluralkit.bot import help from pluralkit.bot import help
from pluralkit.bot.commands import * from pluralkit.bot.commands import *
@ -16,7 +17,7 @@ async def system_info(ctx: CommandContext):
else: else:
system = await ctx.ensure_system() 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): async def new_system(ctx: CommandContext):

View File

@ -1,7 +1,14 @@
import humanize
from typing import Tuple from typing import Tuple
import discord 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: def success(text: str) -> discord.Embed:
embed = 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)", server_id if server_id else "(DMs)",
channel_id channel_id
)) ))
return embed 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

View File

@ -2,12 +2,10 @@ import logging
import re import re
import discord import discord
import humanize
from pluralkit import db from pluralkit import db
from pluralkit.system import System from pluralkit.system import System
from pluralkit.member import Member from pluralkit.member import Member
from pluralkit.utils import get_fronters
logger = logging.getLogger("pluralkit.utils") 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: if system_id:
member = await db.get_member_by_name(conn, system_id=system_id, member_name=key) member = await db.get_member_by_name(conn, system_id=system_id, member_name=key)
if member is not None: if member is not None:
return member 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