Add avatars for systems

This commit is contained in:
Ske 2018-07-15 16:41:21 +02:00
parent b8539ec525
commit cdf1ab89dc
4 changed files with 34 additions and 6 deletions

View File

@ -62,11 +62,12 @@ async def system_set(conn, message, args):
if system is None: if system is None:
return False, "No system is registered to this account." return False, "No system is registered to this account."
allowed_properties = ["name", "description", "tag"] allowed_properties = ["name", "description", "tag", "avatar"]
db_properties = { db_properties = {
"name": "name", "name": "name",
"description": "description", "description": "description",
"tag": "tag" "tag": "tag",
"avatar": "avatar_url"
} }
prop = args[0] prop = args[0]
@ -86,6 +87,20 @@ async def system_set(conn, message, args):
for member in members_exceeding]) for member in members_exceeding])
logger.debug("Members exceeding combined length with tag '{}': {}".format(value, member_names)) logger.debug("Members exceeding combined length with tag '{}': {}".format(value, member_names))
return False, "The maximum length of a name plus the system tag is 32 characters. The following members would exceed the limit: {}. Please reduce the length of the tag, or rename the members.".format(member_names) return False, "The maximum length of a name plus the system tag is 32 characters. The following members would exceed the limit: {}. Please reduce the length of the tag, or rename the members.".format(member_names)
if prop == "avatar":
user = await parse_mention(value)
if user:
# Set the avatar to the mentioned user's avatar
# Discord doesn't like webp, but also hosts png alternatives
value = user.avatar_url.replace(".webp", ".png")
else:
# Validate URL
u = urlparse(value)
if u.scheme in ["http", "https"] and u.netloc and u.path:
value = value
else:
return False, "Invalid URL."
else: else:
# Clear from DB # Clear from DB
value = None value = None
@ -93,7 +108,11 @@ async def system_set(conn, message, args):
db_prop = db_properties[prop] db_prop = db_properties[prop]
await db.update_system_field(conn, system_id=system["id"], field=db_prop, value=value) await db.update_system_field(conn, system_id=system["id"], field=db_prop, value=value)
return True, "{} system {}.".format("Updated" if value else "Cleared", prop) if prop == "avatar" and value:
response = make_default_embed("Updated system avatar.").set_image(url=value)
else:
response = "{} system {}.".format("Updated" if value else "Cleared", prop)
return True, response
@command(cmd="system link", usage="<account>", description="Links another account to your system.", category="System commands") @command(cmd="system link", usage="<account>", description="Links another account to your system.", category="System commands")
async def system_link(conn, message, args): async def system_link(conn, message, args):
@ -326,7 +345,7 @@ async def member_set(conn, message, member, args):
db_prop = db_properties[prop] db_prop = db_properties[prop]
await db.update_member_field(conn, member_id=member["id"], field=db_prop, value=value) await db.update_member_field(conn, member_id=member["id"], field=db_prop, value=value)
if prop == "avatar": if prop == "avatar" and value:
response = make_default_embed("Updated {}'s avatar.".format(member["name"])).set_image(url=value) response = make_default_embed("Updated {}'s avatar.".format(member["name"])).set_image(url=value)
else: else:
response = "{} {}'s {}.".format("Updated" if value else "Cleared", member["name"], prop) response = "{} {}'s {}.".format("Updated" if value else "Cleared", member["name"], prop)

View File

@ -231,6 +231,7 @@ async def create_tables(conn):
name text, name text,
description text, description text,
tag text, tag text,
avatar_url text,
created timestamp not null default current_timestamp created timestamp not null default current_timestamp
)""") )""")
await conn.execute("""create table if not exists members ( await conn.execute("""create table if not exists members (

View File

@ -28,7 +28,10 @@ For example:
For example: For example:
`pk;system set name My System` - sets your system name to "My System". `pk;system set name My System` - sets your system name to "My System".
`pk;system set description A really cool system.` - sets your system description. `pk;system set description A really cool system.` - sets your system description.
`pk;system set tag [MS]` - Sets the tag (which will be displayed after member names in messages) to "[MS]"."""), `pk;system set tag [MS]` - Sets the tag (which will be displayed after member names in messages) to "[MS]".
`pk;system set avatar https://placekitten.com/400/400` - Changes your system's avatar to a linked image.
If you don't specify any value, the property will be cleared."""),
("Linking accounts", ("Linking accounts",
"""If your system has multiple accounts, you can link all of them to your system, and you can use the bot from all of those accounts. """If your system has multiple accounts, you can link all of them to your system, and you can use the bot from all of those accounts.
@ -64,7 +67,9 @@ For example:
`pk;member set John birthdate 1996-02-27` - Changes John's birthdate to Feb 27, 1996. (Must be YYYY-MM-DD format). `pk;member set John birthdate 1996-02-27` - Changes John's birthdate to Feb 27, 1996. (Must be YYYY-MM-DD format).
`pk;member set John birthdate 02-27` - Changes John's birthdate to February 27th, with no year. `pk;member set John birthdate 02-27` - Changes John's birthdate to February 27th, with no year.
`pk;member set John avatar https://placekitten.com/400/400` - Changes John's avatar to a linked image. `pk;member set John avatar https://placekitten.com/400/400` - Changes John's avatar to a linked image.
`pk;member set John avatar @JohnsAccount` - Changes John's avatar to the avatar of the mentioned account."""), `pk;member set John avatar @JohnsAccount` - Changes John's avatar to the avatar of the mentioned account.
If you don't specify any value, the property will be cleared."""),
("Removing a member", ("Removing a member",
"""If you want to delete a member, you can use the `pk;member delete` command. """If you want to delete a member, you can use the `pk;member delete` command.

View File

@ -204,6 +204,9 @@ async def generate_system_info_card(conn, system: asyncpg.Record) -> discord.Emb
if system["name"]: if system["name"]:
card.title = system["name"] card.title = system["name"]
if system["avatar_url"]:
card.set_thumbnail(url=system["avatar_url"])
if system["tag"]: if system["tag"]:
card.add_field(name="Tag", value=system["tag"]) card.add_field(name="Tag", value=system["tag"])