2019-04-19 18:48:37 +00:00
using System ;
2019-04-26 15:14:20 +00:00
using System.Linq ;
2019-04-19 18:48:37 +00:00
using System.Threading.Tasks ;
2019-07-15 21:50:32 +00:00
using Discord ;
2019-08-04 11:43:56 +00:00
using Humanizer ;
2019-06-13 18:33:17 +00:00
using NodaTime ;
using NodaTime.Text ;
using NodaTime.TimeZones ;
2019-10-05 05:41:00 +00:00
using PluralKit.Bot.CommandSystem ;
2019-07-09 22:19:18 +00:00
using PluralKit.Core ;
2019-04-19 18:48:37 +00:00
2019-04-21 13:33:22 +00:00
namespace PluralKit.Bot.Commands
2019-04-19 18:48:37 +00:00
{
2019-10-05 05:41:00 +00:00
public class SystemCommands
2019-04-19 18:48:37 +00:00
{
2019-10-05 05:41:00 +00:00
private SystemStore _systems ;
private MemberStore _members ;
2019-04-27 14:30:34 +00:00
2019-10-05 05:41:00 +00:00
private SwitchStore _switches ;
private EmbedService _embeds ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
private ProxyCacheService _proxyCache ;
2019-04-21 13:33:22 +00:00
2019-10-05 05:41:00 +00:00
public SystemCommands ( SystemStore systems , MemberStore members , SwitchStore switches , EmbedService embeds , ProxyCacheService proxyCache )
{
_systems = systems ;
_members = members ;
_switches = switches ;
_embeds = embeds ;
_proxyCache = proxyCache ;
2019-04-21 13:33:22 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Query ( Context ctx , PKSystem system ) {
if ( system = = null ) throw Errors . NoSystemError ;
2019-04-21 13:33:22 +00:00
2019-10-05 05:41:00 +00:00
await ctx . Reply ( embed : await _embeds . CreateSystemEmbed ( system ) ) ;
}
public async Task New ( Context ctx )
2019-04-19 18:48:37 +00:00
{
2019-10-05 05:41:00 +00:00
ctx . CheckNoSystem ( ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
var system = await _systems . Create ( ctx . RemainderOrNull ( ) ) ;
await _systems . Link ( system , ctx . Author . Id ) ;
await ctx . Reply ( $"{Emojis.Success} Your system has been created. Type `pk;system` to view it, and type `pk;help` for more information about commands you can use now." ) ;
2019-04-19 18:48:37 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Name ( Context ctx )
{
ctx . CheckSystem ( ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
var newSystemName = ctx . RemainderOrNull ( ) ;
2019-04-29 18:28:53 +00:00
if ( newSystemName ! = null & & newSystemName . Length > Limits . MaxSystemNameLength ) throw Errors . SystemNameTooLongError ( newSystemName . Length ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
ctx . System . Name = newSystemName ;
await _systems . Save ( ctx . System ) ;
await ctx . Reply ( $"{Emojis.Success} System name {(newSystemName != null ? " changed " : " cleared ")}." ) ;
2019-04-19 18:48:37 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Description ( Context ctx ) {
ctx . CheckSystem ( ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
var newDescription = ctx . RemainderOrNull ( ) ;
2019-04-29 18:28:53 +00:00
if ( newDescription ! = null & & newDescription . Length > Limits . MaxDescriptionLength ) throw Errors . DescriptionTooLongError ( newDescription . Length ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
ctx . System . Description = newDescription ;
await _systems . Save ( ctx . System ) ;
await ctx . Reply ( $"{Emojis.Success} System description {(newDescription != null ? " changed " : " cleared ")}." ) ;
2019-04-19 18:48:37 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Tag ( Context ctx )
{
ctx . CheckSystem ( ) ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
var newTag = ctx . RemainderOrNull ( ) ;
ctx . System . Tag = newTag ;
2019-04-19 18:48:37 +00:00
2019-07-10 11:44:03 +00:00
if ( newTag ! = null )
{
if ( newTag . Length > Limits . MaxSystemTagLength ) throw Errors . SystemNameTooLongError ( newTag . Length ) ;
// Check unproxyable messages *after* changing the tag (so it's seen in the method) but *before* we save to DB (so we can cancel)
2019-10-05 05:41:00 +00:00
var unproxyableMembers = await _members . GetUnproxyableMembers ( ctx . System ) ;
2019-07-10 11:44:03 +00:00
if ( unproxyableMembers . Count > 0 )
{
2019-10-05 05:41:00 +00:00
var msg = await ctx . Reply (
2019-08-14 05:16:48 +00:00
$"{Emojis.Warn} Changing your system tag to '{newTag}' will result in the following members being unproxyable, since the tag would bring their name over {Limits.MaxProxyNameLength} characters:\n**{string.Join(" , ", unproxyableMembers.Select((m) => m.Name))}**\nDo you want to continue anyway?" ) ;
2019-10-05 05:41:00 +00:00
if ( ! await ctx . PromptYesNo ( msg ) ) throw new PKError ( "Tag change cancelled." ) ;
2019-07-10 11:44:03 +00:00
}
2019-04-26 15:14:20 +00:00
}
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
await _systems . Save ( ctx . System ) ;
await ctx . Reply ( $"{Emojis.Success} System tag {(newTag != null ? " changed " : " cleared ")}." ) ;
2019-08-12 15:49:07 +00:00
2019-10-05 05:41:00 +00:00
await _proxyCache . InvalidateResultsForSystem ( ctx . System ) ;
2019-04-19 18:48:37 +00:00
}
2019-07-15 21:50:32 +00:00
2019-10-05 05:41:00 +00:00
public async Task SystemAvatar ( Context ctx )
2019-07-19 12:21:16 +00:00
{
2019-10-05 05:41:00 +00:00
ctx . CheckSystem ( ) ;
var member = await ctx . MatchUser ( ) ;
if ( member ! = null )
{
if ( member . AvatarId = = null ) throw Errors . UserHasNoAvatar ;
ctx . System . AvatarUrl = member . GetAvatarUrl ( ImageFormat . Png , size : 256 ) ;
await _systems . Save ( ctx . System ) ;
2019-07-19 12:21:16 +00:00
2019-10-05 05:41:00 +00:00
var embed = new EmbedBuilder ( ) . WithImageUrl ( ctx . System . AvatarUrl ) . Build ( ) ;
await ctx . Reply (
$"{Emojis.Success} System avatar changed to {member.Username}'s avatar! {Emojis.Warn} Please note that if {member.Username} changes their avatar, the system's avatar will need to be re-set." , embed : embed ) ;
}
else
{
string url = ctx . RemainderOrNull ( ) ? ? ctx . Message . Attachments . FirstOrDefault ( ) ? . ProxyUrl ;
if ( url ! = null ) await ctx . BusyIndicator ( ( ) = > Utils . VerifyAvatarOrThrow ( url ) ) ;
ctx . System . AvatarUrl = url ;
await _systems . Save ( ctx . System ) ;
var embed = url ! = null ? new EmbedBuilder ( ) . WithImageUrl ( url ) . Build ( ) : null ;
await ctx . Reply ( $"{Emojis.Success} System avatar {(url == null ? " cleared " : " changed ")}." , embed : embed ) ;
}
2019-08-12 15:49:07 +00:00
2019-10-05 05:41:00 +00:00
await _proxyCache . InvalidateResultsForSystem ( ctx . System ) ;
2019-07-19 12:21:16 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task Delete ( Context ctx ) {
ctx . CheckSystem ( ) ;
2019-07-15 21:50:32 +00:00
2019-10-05 05:41:00 +00:00
var msg = await ctx . Reply ( $"{Emojis.Warn} Are you sure you want to delete your system? If so, reply to this message with your system's ID (`{ctx.System.Hid}`).\n**Note: this action is permanent.**" ) ;
var reply = await ctx . AwaitMessage ( ctx . Channel , ctx . Author , timeout : TimeSpan . FromMinutes ( 1 ) ) ;
if ( reply . Content ! = ctx . System . Hid ) throw new PKError ( $"System deletion cancelled. Note that you must reply with your system ID (`{ctx.System.Hid}`) *verbatim*." ) ;
2019-07-15 21:50:32 +00:00
2019-10-05 05:41:00 +00:00
await _systems . Delete ( ctx . System ) ;
await ctx . Reply ( $"{Emojis.Success} System deleted." ) ;
2019-08-12 15:49:07 +00:00
2019-10-05 05:41:00 +00:00
await _proxyCache . InvalidateResultsForSystem ( ctx . System ) ;
2019-07-15 21:50:32 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task MemberShortList ( Context ctx , PKSystem system ) {
if ( system = = null ) throw Errors . NoSystemError ;
2019-04-19 18:48:37 +00:00
2019-10-05 05:41:00 +00:00
var members = await _members . GetBySystem ( system ) ;
var embedTitle = system . Name ! = null ? $"Members of {system.Name} (`{system.Hid}`)" : $"Members of `{system.Hid}`" ;
await ctx . Paginate < PKMember > (
members . OrderBy ( m = > m . Name ) . ToList ( ) ,
25 ,
embedTitle ,
( eb , ms ) = > eb . Description = string . Join ( "\n" , ms . Select ( ( m ) = > {
if ( m . HasProxyTags ) return $"[`{m.Hid}`] **{m.Name}** *({m.ProxyString})*" ;
return $"[`{m.Hid}`] **{m.Name}**" ;
} ) )
) ;
2019-04-26 16:15:25 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task MemberLongList ( Context ctx , PKSystem system ) {
if ( system = = null ) throw Errors . NoSystemError ;
2019-04-29 15:42:09 +00:00
2019-10-05 05:41:00 +00:00
var members = await _members . GetBySystem ( system ) ;
var embedTitle = system . Name ! = null ? $"Members of {system.Name} (`{system.Hid}`)" : $"Members of `{system.Hid}`" ;
await ctx . Paginate < PKMember > (
members . OrderBy ( m = > m . Name ) . ToList ( ) ,
5 ,
embedTitle ,
( eb , ms ) = > {
foreach ( var m in ms ) {
var profile = $"**ID**: {m.Hid}" ;
if ( m . Pronouns ! = null ) profile + = $"\n**Pronouns**: {m.Pronouns}" ;
if ( m . Birthday ! = null ) profile + = $"\n**Birthdate**: {m.BirthdayString}" ;
if ( m . Prefix ! = null | | m . Suffix ! = null ) profile + = $"\n**Proxy tags**: {m.ProxyString}" ;
if ( m . Description ! = null ) profile + = $"\n\n{m.Description}" ;
eb . AddField ( m . Name , profile . Truncate ( 1024 ) ) ;
2019-04-29 15:42:09 +00:00
}
2019-10-05 05:41:00 +00:00
}
) ;
2019-04-29 15:42:09 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task SystemFronter ( Context ctx , PKSystem system )
2019-06-15 10:19:44 +00:00
{
if ( system = = null ) throw Errors . NoSystemError ;
2019-10-05 05:41:00 +00:00
var sw = await _switches . GetLatestSwitch ( system ) ;
2019-06-15 10:19:44 +00:00
if ( sw = = null ) throw Errors . NoRegisteredSwitches ;
2019-10-05 05:41:00 +00:00
await ctx . Reply ( embed : await _embeds . CreateFronterEmbed ( sw , system . Zone ) ) ;
2019-06-15 10:43:35 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task SystemFrontHistory ( Context ctx , PKSystem system )
2019-06-15 10:43:35 +00:00
{
if ( system = = null ) throw Errors . NoSystemError ;
2019-10-05 05:41:00 +00:00
var sws = ( await _switches . GetSwitches ( system , 10 ) ) . ToList ( ) ;
2019-06-15 10:43:35 +00:00
if ( sws . Count = = 0 ) throw Errors . NoRegisteredSwitches ;
2019-10-05 05:41:00 +00:00
await ctx . Reply ( embed : await _embeds . CreateFrontHistoryEmbed ( sws , system . Zone ) ) ;
2019-06-15 10:19:44 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task SystemFrontPercent ( Context ctx , PKSystem system )
2019-06-30 21:41:01 +00:00
{
if ( system = = null ) throw Errors . NoSystemError ;
2019-10-05 05:41:00 +00:00
string durationStr = ctx . RemainderOrNull ( ) ? ? "30d" ;
2019-06-30 21:41:01 +00:00
2019-07-17 11:37:43 +00:00
var now = SystemClock . Instance . GetCurrentInstant ( ) ;
var rangeStart = PluralKit . Utils . ParseDateTime ( durationStr ) ;
if ( rangeStart = = null ) throw Errors . InvalidDateTime ( durationStr ) ;
if ( rangeStart . Value . ToInstant ( ) > now ) throw Errors . FrontPercentTimeInFuture ;
2019-06-30 21:41:01 +00:00
2019-10-05 05:41:00 +00:00
var frontpercent = await _switches . GetPerMemberSwitchDuration ( system , rangeStart . Value . ToInstant ( ) , now ) ;
await ctx . Reply ( embed : await _embeds . CreateFrontPercentEmbed ( frontpercent , system . Zone ) ) ;
2019-06-30 21:41:01 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task SystemTimezone ( Context ctx )
2019-06-13 18:33:17 +00:00
{
2019-10-05 05:41:00 +00:00
if ( ctx . System = = null ) throw Errors . NoSystemError ;
var zoneStr = ctx . RemainderOrNull ( ) ;
2019-06-13 18:33:17 +00:00
if ( zoneStr = = null )
{
2019-10-05 05:41:00 +00:00
ctx . System . UiTz = "UTC" ;
await _systems . Save ( ctx . System ) ;
await ctx . Reply ( $"{Emojis.Success} System time zone cleared." ) ;
2019-06-13 18:33:17 +00:00
return ;
}
2019-10-05 05:41:00 +00:00
var zone = await FindTimeZone ( ctx , zoneStr ) ;
2019-06-13 18:33:17 +00:00
if ( zone = = null ) throw Errors . InvalidTimeZone ( zoneStr ) ;
var currentTime = SystemClock . Instance . GetCurrentInstant ( ) . InZone ( zone ) ;
2019-10-05 05:41:00 +00:00
var msg = await ctx . Reply (
2019-06-15 10:33:24 +00:00
$"This will change the system time zone to {zone.Id}. The current time is {Formats.ZonedDateTimeFormat.Format(currentTime)}. Is this correct?" ) ;
2019-10-05 05:41:00 +00:00
if ( ! await ctx . PromptYesNo ( msg ) ) throw Errors . TimezoneChangeCancelled ;
ctx . System . UiTz = zone . Id ;
await _systems . Save ( ctx . System ) ;
2019-06-13 18:33:17 +00:00
2019-10-05 05:41:00 +00:00
await ctx . Reply ( $"System time zone changed to {zone.Id}." ) ;
2019-06-13 18:33:17 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task < DateTimeZone > FindTimeZone ( Context ctx , string zoneStr ) {
2019-06-13 21:42:39 +00:00
// First, if we're given a flag emoji, we extract the flag emoji code from it.
zoneStr = PluralKit . Utils . ExtractCountryFlag ( zoneStr ) ? ? zoneStr ;
// Then, we find all *locations* matching either the given country code or the country name.
var locations = TzdbDateTimeZoneSource . Default . Zone1970Locations ;
var matchingLocations = locations . Where ( l = > l . Countries . Any ( c = >
string . Equals ( c . Code , zoneStr , StringComparison . InvariantCultureIgnoreCase ) | |
string . Equals ( c . Name , zoneStr , StringComparison . InvariantCultureIgnoreCase ) ) ) ;
// Then, we find all (unique) time zone IDs that match.
var matchingZones = matchingLocations . Select ( l = > DateTimeZoneProviders . Tzdb . GetZoneOrNull ( l . ZoneId ) )
. Distinct ( ) . ToList ( ) ;
// If the set of matching zones is empty (ie. we didn't find anything), we try a few other things.
if ( matchingZones . Count = = 0 )
{
// First, we try to just find the time zone given directly and return that.
var givenZone = DateTimeZoneProviders . Tzdb . GetZoneOrNull ( zoneStr ) ;
if ( givenZone ! = null ) return givenZone ;
// If we didn't find anything there either, we try parsing the string as an offset, then
// find all possible zones that match that offset. For an offset like UTC+2, this doesn't *quite*
// work, since there are 57(!) matching zones (as of 2019-06-13) - but for less populated time zones
// this could work nicely.
var inputWithoutUtc = zoneStr . Replace ( "UTC" , "" ) . Replace ( "GMT" , "" ) ;
var res = OffsetPattern . CreateWithInvariantCulture ( "+H" ) . Parse ( inputWithoutUtc ) ;
if ( ! res . Success ) res = OffsetPattern . CreateWithInvariantCulture ( "+H:mm" ) . Parse ( inputWithoutUtc ) ;
// If *this* didn't parse correctly, fuck it, bail.
if ( ! res . Success ) return null ;
var offset = res . Value ;
// To try to reduce the count, we go by locations from the 1970+ database instead of just the full database
// This elides regions that have been identical since 1970, omitting small distinctions due to Ancient History(tm).
var allZones = TzdbDateTimeZoneSource . Default . Zone1970Locations . Select ( l = > l . ZoneId ) . Distinct ( ) ;
matchingZones = allZones . Select ( z = > DateTimeZoneProviders . Tzdb . GetZoneOrNull ( z ) )
. Where ( z = > z . GetUtcOffset ( SystemClock . Instance . GetCurrentInstant ( ) ) = = offset ) . ToList ( ) ;
}
// If we have a list of viable time zones, we ask the user which is correct.
// If we only have one, return that one.
if ( matchingZones . Count = = 1 )
return matchingZones . First ( ) ;
// Otherwise, prompt and return!
2019-10-05 05:41:00 +00:00
return await ctx . Choose ( "There were multiple matches for your time zone query. Please select the region that matches you the closest:" , matchingZones ,
2019-06-13 21:42:39 +00:00
z = >
{
if ( TzdbDateTimeZoneSource . Default . Aliases . Contains ( z . Id ) )
return $"**{z.Id}**, {string.Join(" , ", TzdbDateTimeZoneSource.Default.Aliases[z.Id])}" ;
return $"**{z.Id}**" ;
} ) ;
2019-04-19 18:48:37 +00:00
}
}
}