2018-08-02 11:10:09 +02:00
import dateparser
2018-07-24 22:47:57 +02:00
import humanize
2018-09-16 19:20:08 +02:00
from datetime import datetime , timedelta
2018-07-24 22:47:57 +02:00
2018-09-16 19:36:50 +02:00
import pluralkit . bot . embeds
2018-08-02 00:36:50 +02:00
import pluralkit . utils
2018-09-07 17:34:38 +02:00
from pluralkit . bot import help
2018-07-24 22:47:57 +02:00
from pluralkit . bot . commands import *
2018-09-09 20:50:53 +02:00
from pluralkit . errors import ExistingSystemError , UnlinkingLastAccountError , PluralKitError , AccountAlreadyLinkedError
2018-07-24 22:47:57 +02:00
logger = logging . getLogger ( " pluralkit.commands " )
2018-09-07 17:34:38 +02:00
async def system_info ( ctx : CommandContext ) :
if ctx . has_next ( ) :
system = await ctx . pop_system ( )
else :
system = await ctx . ensure_system ( )
2018-08-02 11:10:09 +02:00
2018-09-16 19:36:50 +02:00
await ctx . reply ( embed = await pluralkit . bot . embeds . system_card ( ctx . conn , ctx . client , system ) )
2018-07-24 22:47:57 +02:00
2018-09-07 17:34:38 +02:00
async def new_system ( ctx : CommandContext ) :
system_name = ctx . remaining ( ) or None
2018-07-24 22:47:57 +02:00
2018-09-09 20:38:57 +02:00
try :
await System . create_system ( ctx . conn , ctx . message . author . id , system_name )
2018-09-09 20:50:53 +02:00
except ExistingSystemError as e :
2018-11-15 21:05:13 +01:00
raise CommandError ( e . message )
2018-07-24 22:47:57 +02:00
2018-11-15 21:05:13 +01:00
await ctx . reply_ok ( " System registered! To begin adding members, use `pk;member new <name>`. " )
2018-09-07 17:34:38 +02:00
2018-07-24 22:47:57 +02:00
2018-09-07 17:34:38 +02:00
async def system_set ( ctx : CommandContext ) :
2018-11-30 21:42:01 +01:00
raise CommandError (
" `pk;system set` has been retired. Please use the new member modifying commands: `pk;system [name|description|avatar|tag]`. " )
2018-11-23 21:55:47 +01:00
async def system_name ( ctx : CommandContext ) :
2018-09-07 17:34:38 +02:00
system = await ctx . ensure_system ( )
2018-11-23 21:55:47 +01:00
new_name = ctx . remaining ( ) or None
2018-09-07 17:34:38 +02:00
2018-11-23 21:55:47 +01:00
await system . set_name ( ctx . conn , new_name )
await ctx . reply_ok ( " System name {} . " . format ( " updated " if new_name else " cleared " ) )
2018-07-24 22:47:57 +02:00
2018-09-16 13:46:22 +02:00
2018-11-23 21:55:47 +01:00
async def system_description ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
new_description = ctx . remaining ( ) or None
2018-09-09 20:38:57 +02:00
2018-11-23 21:55:47 +01:00
await system . set_description ( ctx . conn , new_description )
await ctx . reply_ok ( " System description {} . " . format ( " updated " if new_description else " cleared " ) )
2018-07-24 22:47:57 +02:00
2018-11-23 21:55:47 +01:00
async def system_tag ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
new_tag = ctx . remaining ( ) or None
2018-09-09 20:38:57 +02:00
2018-11-23 21:55:47 +01:00
await system . set_tag ( ctx . conn , new_tag )
await ctx . reply_ok ( " System tag {} . " . format ( " updated " if new_tag else " cleared " ) )
2018-09-09 20:38:57 +02:00
2018-11-30 21:42:01 +01:00
# System class is immutable, update the tag so get_member_name_limit works
system = system . _replace ( tag = new_tag )
members = await system . get_members ( ctx . conn )
# Certain members might not be able to be proxied with this new tag, show a warning for those
members_exceeding = [ member for member in members if
len ( member . name ) > system . get_member_name_limit ( ) ]
if members_exceeding :
member_names = " , " . join ( [ member . name for member in members_exceeding ] )
await ctx . reply_warn (
" Due to the length of this tag, the following members will not be able to be proxied: {} . Please use a shorter tag to prevent this. " . format (
member_names ) )
# Edge case: members with name length 1 and no new tag
if not new_tag :
one_length_members = [ member for member in members if len ( member . name ) == 1 ]
if one_length_members :
member_names = " , " . join ( [ member . name for member in one_length_members ] )
await ctx . reply_warn (
" Without a system tag, you will not be able to proxy members with a one-character name: {} . To prevent this, please add a system tag or lengthen their name. " . format (
member_names ) )
2018-11-23 21:55:47 +01:00
async def system_avatar ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
new_avatar_url = ctx . remaining ( ) or None
if new_avatar_url :
user = await utils . parse_mention ( ctx . client , new_avatar_url )
if user :
new_avatar_url = user . avatar_url_as ( format = " png " )
await system . set_avatar ( ctx . conn , new_avatar_url )
await ctx . reply_ok ( " System avatar {} . " . format ( " updated " if new_avatar_url else " cleared " ) )
2018-07-24 22:47:57 +02:00
2018-09-07 17:34:38 +02:00
async def system_link ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
account_name = ctx . pop_str ( CommandError ( " You must pass an account to link this system to. " , help = help . link_account ) )
2018-07-24 22:47:57 +02:00
2018-11-23 21:55:47 +01:00
# Do the sanity checking here too (despite it being done in System.link_account)
# Because we want it to be done before the confirmation dialog is shown
2018-07-24 22:47:57 +02:00
# Find account to link
2018-09-07 17:34:38 +02:00
linkee = await utils . parse_mention ( ctx . client , account_name )
2018-07-24 22:47:57 +02:00
if not linkee :
2018-11-15 21:05:13 +01:00
raise CommandError ( " Account not found. " )
2018-07-24 22:47:57 +02:00
# Make sure account doesn't already have a system
2018-09-09 20:38:57 +02:00
account_system = await System . get_by_account ( ctx . conn , linkee . id )
2018-07-24 22:47:57 +02:00
if account_system :
2018-11-15 21:05:13 +01:00
raise CommandError ( AccountAlreadyLinkedError ( account_system ) . message )
2018-07-24 22:47:57 +02:00
2018-11-30 21:51:57 +01:00
msg = await ctx . reply ( " {} , please confirm the link by clicking the \u2705 reaction on this message. " . format ( linkee . mention ) )
if not await ctx . confirm_react ( linkee , msg ) :
2018-11-15 21:05:13 +01:00
raise CommandError ( " Account link cancelled. " )
2018-07-24 22:47:57 +02:00
2018-09-09 20:38:57 +02:00
await system . link_account ( ctx . conn , linkee . id )
2018-11-15 21:05:13 +01:00
await ctx . reply_ok ( " Account linked to system. " )
2018-09-07 17:34:38 +02:00
async def system_unlink ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
2018-07-24 22:47:57 +02:00
2018-09-09 20:38:57 +02:00
try :
await system . unlink_account ( ctx . conn , ctx . message . author . id )
2018-09-09 20:50:53 +02:00
except UnlinkingLastAccountError as e :
2018-11-15 21:05:13 +01:00
raise CommandError ( e . message )
2018-07-24 22:47:57 +02:00
2018-11-15 21:05:13 +01:00
await ctx . reply_ok ( " Account unlinked. " )
2018-07-24 22:47:57 +02:00
2018-08-02 11:10:09 +02:00
2018-09-07 17:34:38 +02:00
async def system_fronter ( ctx : CommandContext ) :
if ctx . has_next ( ) :
system = await ctx . pop_system ( )
else :
system = await ctx . ensure_system ( )
2018-08-02 11:10:09 +02:00
2018-10-11 12:54:40 +02:00
embed = await embeds . front_status ( await system . get_latest_switch ( ctx . conn ) , ctx . conn )
2018-09-07 17:34:38 +02:00
await ctx . reply ( embed = embed )
2018-08-02 11:10:09 +02:00
2018-09-07 17:34:38 +02:00
async def system_fronthistory ( ctx : CommandContext ) :
if ctx . has_next ( ) :
system = await ctx . pop_system ( )
else :
system = await ctx . ensure_system ( )
2018-08-02 11:10:09 +02:00
2018-07-24 22:47:57 +02:00
lines = [ ]
2018-08-02 00:36:50 +02:00
front_history = await pluralkit . utils . get_front_history ( ctx . conn , system . id , count = 10 )
2018-07-24 22:47:57 +02:00
for i , ( timestamp , members ) in enumerate ( front_history ) :
# Special case when no one's fronting
if len ( members ) == 0 :
name = " (no fronter) "
else :
name = " , " . join ( [ member . name for member in members ] )
# Make proper date string
time_text = timestamp . isoformat ( sep = " " , timespec = " seconds " )
2018-09-07 17:34:38 +02:00
rel_text = humanize . naturaltime ( pluralkit . utils . fix_time ( timestamp ) )
2018-07-24 22:47:57 +02:00
delta_text = " "
if i > 0 :
2018-09-07 17:34:38 +02:00
last_switch_time = front_history [ i - 1 ] [ 0 ]
2018-07-24 22:47:57 +02:00
delta_text = " , for {} " . format ( humanize . naturaldelta ( timestamp - last_switch_time ) )
lines . append ( " ** {} ** ( {} , {} {} ) " . format ( name , time_text , rel_text , delta_text ) )
2018-09-07 17:34:38 +02:00
embed = embeds . status ( " \n " . join ( lines ) or " (none) " )
2018-07-24 22:47:57 +02:00
embed . title = " Past switches "
2018-09-07 17:34:38 +02:00
await ctx . reply ( embed = embed )
2018-07-24 22:47:57 +02:00
2018-09-07 17:34:38 +02:00
async def system_delete ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
2018-09-09 20:38:57 +02:00
delete_confirm_msg = " Are you sure you want to delete your system? If so, reply to this message with the system ' s ID (` {} `). " . format (
system . hid )
2018-09-08 13:48:18 +02:00
if not await ctx . confirm_text ( ctx . message . author , ctx . message . channel , system . hid , delete_confirm_msg ) :
2018-11-15 21:05:13 +01:00
raise CommandError ( " System deletion cancelled. " )
2018-09-07 17:34:38 +02:00
2018-09-09 20:38:57 +02:00
await system . delete ( ctx . conn )
2018-11-15 21:05:13 +01:00
await ctx . reply_ok ( " System deleted. " )
2018-09-08 13:48:18 +02:00
2018-08-02 11:10:09 +02:00
2018-09-07 17:34:38 +02:00
async def system_frontpercent ( ctx : CommandContext ) :
system = await ctx . ensure_system ( )
2018-08-02 11:10:09 +02:00
# Parse the time limit (will go this far back)
2018-09-16 19:20:08 +02:00
if ctx . remaining ( ) :
before = dateparser . parse ( ctx . remaining ( ) , languages = [ " en " ] , settings = {
" TO_TIMEZONE " : " UTC " ,
" RETURN_AS_TIMEZONE_AWARE " : False
} )
if not before :
2018-11-15 21:05:13 +01:00
raise CommandError ( " Could not parse ' {} ' as a valid time. " . format ( ctx . remaining ( ) ) )
2018-09-16 19:20:08 +02:00
# If time is in the future, just kinda discard
if before and before > datetime . utcnow ( ) :
before = None
else :
before = datetime . utcnow ( ) - timedelta ( days = 30 )
2018-08-02 11:10:09 +02:00
# Fetch list of switches
2018-09-07 17:34:38 +02:00
all_switches = await pluralkit . utils . get_front_history ( ctx . conn , system . id , 99999 )
2018-08-02 11:10:09 +02:00
if not all_switches :
2018-11-15 21:05:13 +01:00
raise CommandError ( " No switches registered to this system. " )
2018-08-02 11:10:09 +02:00
# Cull the switches *ending* before the limit, if given
# We'll need to find the first switch starting before the limit, then cut off every switch *before* that
if before :
for last_stamp , _ in all_switches :
if last_stamp < before :
break
all_switches = [ ( stamp , members ) for stamp , members in all_switches if stamp > = last_stamp ]
start_times = [ stamp for stamp , _ in all_switches ]
end_times = [ datetime . utcnow ( ) ] + start_times
switch_members = [ members for _ , members in all_switches ]
# Gonna save a list of members by ID for future lookup too
members_by_id = { }
# Using the ID as a key here because it's a simple number that can be hashed and used as a key
member_times = { }
for start_time , end_time , members in zip ( start_times , end_times , switch_members ) :
# Cut off parts of the switch that occurs before the time limit (will only happen if this is the last switch)
if before and start_time < before :
start_time = before
# Calculate length of the switch
switch_length = end_time - start_time
2018-09-07 17:34:38 +02:00
def add_switch ( id , length ) :
if id not in member_times :
member_times [ id ] = length
2018-08-02 11:10:09 +02:00
else :
2018-09-07 17:34:38 +02:00
member_times [ id ] + = length
2018-08-02 11:10:09 +02:00
for member in members :
# Add the switch length to the currently registered time for that member
add_switch ( member . id , switch_length )
# Also save the member in the ID map for future reference
members_by_id [ member . id ] = member
# Also register a no-fronter switch with the key None
if not members :
add_switch ( None , switch_length )
# Find the total timespan of the range
span_start = max ( start_times [ - 1 ] , before ) if before else start_times [ - 1 ]
total_time = datetime . utcnow ( ) - span_start
2018-09-01 19:41:35 +02:00
embed = embeds . status ( " " )
2018-08-02 11:10:09 +02:00
for member_id , front_time in sorted ( member_times . items ( ) , key = lambda x : x [ 1 ] , reverse = True ) :
member = members_by_id [ member_id ] if member_id else None
# Calculate percent
fraction = front_time / total_time
2018-09-16 19:20:08 +02:00
percent = round ( fraction * 100 )
2018-08-02 11:10:09 +02:00
embed . add_field ( name = member . name if member else " (no fronter) " ,
value = " {} % ( {} ) " . format ( percent , humanize . naturaldelta ( front_time ) ) )
2018-11-30 21:42:01 +01:00
embed . set_footer ( text = " Since {} ( {} ) " . format ( span_start . isoformat ( sep = " " , timespec = " seconds " ) ,
humanize . naturaltime ( pluralkit . utils . fix_time ( span_start ) ) ) )
2018-09-07 17:34:38 +02:00
await ctx . reply ( embed = embed )