2020-04-24 19:50:28 +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 ;
2020-02-01 12:03:02 +00:00
2020-06-13 14:03:57 +00:00
using Dapper ;
2020-04-24 19:50:28 +00:00
using DSharpPlus ;
using DSharpPlus.Entities ;
2020-02-01 12:03:02 +00:00
2019-06-13 18:33:17 +00:00
using NodaTime ;
using NodaTime.Text ;
using NodaTime.TimeZones ;
2019-10-05 05:41:00 +00:00
2019-07-09 22:19:18 +00:00
using PluralKit.Core ;
2019-04-19 18:48:37 +00:00
2020-02-12 14:16:19 +00:00
namespace PluralKit.Bot
2019-04-19 18:48:37 +00:00
{
2020-02-01 12:03:02 +00:00
public class SystemEdit
2019-04-19 18:48:37 +00:00
{
2019-10-26 17:45:30 +00:00
private IDataStore _data ;
2020-06-13 17:36:43 +00:00
private IDatabase _db ;
2019-10-05 05:41:00 +00:00
private EmbedService _embeds ;
2019-04-21 13:33:22 +00:00
2020-06-13 17:36:43 +00:00
public SystemEdit ( IDataStore data , EmbedService embeds , IDatabase db )
2019-10-05 05:41:00 +00:00
{
2020-02-01 12:03:02 +00:00
_data = data ;
2019-10-05 05:41:00 +00:00
_embeds = embeds ;
2020-06-13 14:03:57 +00:00
_db = db ;
2019-04-21 13:33:22 +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
2020-03-04 17:13:36 +00:00
if ( ctx . MatchFlag ( "c" , "clear" ) | | ctx . Match ( "clear" ) )
{
2020-06-29 12:39:19 +00:00
var clearPatch = new SystemPatch { Name = null } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , clearPatch ) ) ;
2020-03-04 17:13:36 +00:00
await ctx . Reply ( $"{Emojis.Success} System name cleared." ) ;
return ;
}
2019-10-05 05:41:00 +00:00
var newSystemName = ctx . RemainderOrNull ( ) ;
2020-03-04 17:13:36 +00:00
if ( newSystemName = = null )
{
if ( ctx . System . Name ! = null )
2020-06-20 15:36:03 +00:00
await ctx . Reply ( $"Your system's name is currently **{ctx.System.Name}**. Type `pk;system name -clear` to clear it." ) ;
2020-03-04 17:13:36 +00:00
else
await ctx . Reply ( "Your system currently does not have a name. Type `pk;system name <name>` to set one." ) ;
return ;
}
2020-06-29 12:39:19 +00:00
if ( newSystemName ! = null & & newSystemName . Length > Limits . MaxSystemNameLength )
throw Errors . SystemNameTooLongError ( newSystemName . Length ) ;
var patch = new SystemPatch { Name = newSystemName } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-03-04 17:13:36 +00:00
await ctx . Reply ( $"{Emojis.Success} System name changed." ) ;
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
2020-02-27 17:07:05 +00:00
if ( ctx . MatchFlag ( "c" , "clear" ) | | ctx . Match ( "clear" ) )
2020-02-23 11:45:05 +00:00
{
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { Description = null } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-02-23 11:45:05 +00:00
await ctx . Reply ( $"{Emojis.Success} System description cleared." ) ;
return ;
}
2020-02-20 21:57:37 +00:00
var newDescription = ctx . RemainderOrNull ( ) ? . NormalizeLineEndSpacing ( ) ;
2020-02-23 11:45:05 +00:00
if ( newDescription = = null )
{
if ( ctx . System . Description = = null )
await ctx . Reply ( "Your system does not have a description set. To set one, type `pk;s description <description>`." ) ;
else if ( ctx . MatchFlag ( "r" , "raw" ) )
2020-06-20 15:36:03 +00:00
await ctx . Reply ( $"```\n{ctx.System.Description}\n```" ) ;
2020-02-23 11:45:05 +00:00
else
2020-04-24 19:50:28 +00:00
await ctx . Reply ( embed : new DiscordEmbedBuilder ( )
2020-02-23 11:45:05 +00:00
. WithTitle ( "System description" )
. WithDescription ( ctx . System . Description )
. WithFooter ( "To print the description with formatting, type `pk;s description -raw`. To clear it, type `pk;s description -clear`. To change it, type `pk;s description <new description>`." )
. Build ( ) ) ;
}
else
{
if ( newDescription . Length > Limits . MaxDescriptionLength ) throw Errors . DescriptionTooLongError ( newDescription . Length ) ;
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { Description = newDescription } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-02-23 11:45:05 +00:00
await ctx . Reply ( $"{Emojis.Success} System description changed." ) ;
}
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
2020-02-27 17:07:05 +00:00
if ( ctx . MatchFlag ( "c" , "clear" ) | | ctx . Match ( "clear" ) )
2020-02-22 23:52:28 +00:00
{
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { Tag = null } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-02-22 23:52:28 +00:00
await ctx . Reply ( $"{Emojis.Success} System tag cleared." ) ;
} else if ( ! ctx . HasNext ( skipFlags : false ) )
{
if ( ctx . System . Tag = = null )
await ctx . Reply ( $"You currently have no system tag. To set one, type `pk;s tag <tag>`." ) ;
else
2020-06-20 15:36:03 +00:00
await ctx . Reply ( $"Your current system tag is `{ctx.System.Tag}`. To change it, type `pk;s tag <tag>`. To clear it, type `pk;s tag -clear`." ) ;
2020-02-22 23:52:28 +00:00
}
else
{
var newTag = ctx . RemainderOrNull ( skipFlags : false ) ;
if ( newTag ! = null )
if ( newTag . Length > Limits . MaxSystemTagLength )
throw Errors . SystemNameTooLongError ( newTag . Length ) ;
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { Tag = newTag } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-06-20 15:36:03 +00:00
await ctx . Reply ( $"{Emojis.Success} System tag changed. Member names will now end with `{newTag}` when proxied." ) ;
2020-02-22 23:52:28 +00:00
}
2019-04-19 18:48:37 +00:00
}
2019-07-15 21:50:32 +00:00
2020-02-01 12:03:02 +00:00
public async Task Avatar ( Context ctx )
2019-07-19 12:21:16 +00:00
{
2019-10-05 05:41:00 +00:00
ctx . CheckSystem ( ) ;
2019-12-28 11:47:31 +00:00
2020-03-04 17:13:36 +00:00
if ( ctx . Match ( "clear" ) | | ctx . MatchFlag ( "c" , "clear" ) )
{
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { AvatarUrl = null } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-03-04 17:13:36 +00:00
await ctx . Reply ( $"{Emojis.Success} System avatar cleared." ) ;
return ;
}
else if ( ctx . RemainderOrNull ( ) = = null & & ctx . Message . Attachments . Count = = 0 )
2019-12-28 11:47:31 +00:00
{
if ( ( ctx . System . AvatarUrl ? . Trim ( ) ? ? "" ) . Length > 0 )
{
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder ( )
2019-12-28 11:47:31 +00:00
. WithTitle ( $"System avatar" )
. WithImageUrl ( ctx . System . AvatarUrl )
. WithDescription ( $"To clear, use `pk;system avatar clear`." ) ;
await ctx . Reply ( embed : eb . Build ( ) ) ;
}
else
throw new PKSyntaxError ( $"This system does not have an avatar set. Set one by attaching an image to this command, or by passing an image URL or @mention." ) ;
return ;
}
2019-10-05 05:41:00 +00:00
var member = await ctx . MatchUser ( ) ;
if ( member ! = null )
{
2020-05-01 15:38:16 +00:00
if ( member . AvatarHash = = null ) throw Errors . UserHasNoAvatar ;
2020-06-29 12:39:19 +00:00
var newUrl = member . GetAvatarUrl ( ImageFormat . Png , size : 256 ) ;
var patch = new SystemPatch { AvatarUrl = newUrl } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
var embed = new DiscordEmbedBuilder ( ) . WithImageUrl ( newUrl ) . Build ( ) ;
2019-10-05 05:41:00 +00:00
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
{
2019-12-28 11:47:31 +00:00
// They can't both be null - otherwise we would've hit the conditional at the very top
2019-10-05 05:41:00 +00:00
string url = ctx . RemainderOrNull ( ) ? ? ctx . Message . Attachments . FirstOrDefault ( ) ? . ProxyUrl ;
2020-05-05 14:04:17 +00:00
if ( url ? . Length > Limits . MaxUriLength ) throw Errors . InvalidUrl ( url ) ;
2020-02-12 14:16:19 +00:00
await ctx . BusyIndicator ( ( ) = > AvatarUtils . VerifyAvatarOrThrow ( url ) ) ;
2019-10-05 05:41:00 +00:00
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { AvatarUrl = url } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2019-10-05 05:41:00 +00:00
2020-04-24 19:50:28 +00:00
var embed = url ! = null ? new DiscordEmbedBuilder ( ) . WithImageUrl ( url ) . Build ( ) : null ;
2019-12-28 11:47:31 +00:00
await ctx . Reply ( $"{Emojis.Success} System avatar changed." , embed : embed ) ;
2019-10-05 05:41:00 +00:00
}
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
2020-05-05 14:03:46 +00:00
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.**" ) ;
if ( ! await ctx . ConfirmWithReply ( 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
2020-06-29 12:39:19 +00:00
await _db . Execute ( conn = > conn . DeleteSystem ( ctx . System . Id ) ) ;
2019-10-05 05:41:00 +00:00
await ctx . Reply ( $"{Emojis.Success} System deleted." ) ;
2019-07-15 21:50:32 +00:00
}
2019-10-05 05:41:00 +00:00
2019-12-22 13:15:56 +00:00
public async Task SystemProxy ( Context ctx )
{
ctx . CheckSystem ( ) . CheckGuildContext ( ) ;
2020-06-13 14:03:57 +00:00
var gs = await _db . Execute ( c = > c . QueryOrInsertSystemGuildConfig ( ctx . Guild . Id , ctx . System . Id ) ) ;
2019-12-22 13:15:56 +00:00
bool newValue ;
if ( ctx . Match ( "on" , "enabled" , "true" , "yes" ) ) newValue = true ;
else if ( ctx . Match ( "off" , "disabled" , "false" , "no" ) ) newValue = false ;
else if ( ctx . HasNext ( ) ) throw new PKSyntaxError ( "You must pass either \"on\" or \"off\"." ) ;
2020-02-27 17:06:13 +00:00
else
{
if ( gs . ProxyEnabled )
await ctx . Reply ( "Proxying in this server is currently **enabled** for your system. To disable it, type `pk;system proxy off`." ) ;
else
await ctx . Reply ( "Proxying in this server is currently **disabled** for your system. To enable it, type `pk;system proxy on`." ) ;
return ;
}
2019-12-22 13:15:56 +00:00
2020-06-29 13:20:28 +00:00
var patch = new SystemGuildPatch { ProxyEnabled = newValue } ;
await _db . Execute ( conn = > conn . UpsertSystemGuild ( ctx . System . Id , ctx . Guild . Id , patch ) ) ;
2019-12-22 13:15:56 +00:00
if ( newValue )
await ctx . Reply ( $"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system." ) ;
else
await ctx . Reply ( $"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **disabled** for your system." ) ;
}
2019-10-05 05:41:00 +00:00
2020-02-01 12:03:02 +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 ;
2020-02-27 17:07:05 +00:00
if ( ctx . MatchFlag ( "c" , "clear" ) | | ctx . Match ( "clear" ) )
2019-06-13 18:33:17 +00:00
{
2020-06-29 12:39:19 +00:00
var clearPatch = new SystemPatch { UiTz = "UTC" } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , clearPatch ) ) ;
2020-03-04 17:13:36 +00:00
await ctx . Reply ( $"{Emojis.Success} System time zone cleared (set to UTC)." ) ;
2019-06-13 18:33:17 +00:00
return ;
}
2020-02-22 23:57:46 +00:00
var zoneStr = ctx . RemainderOrNull ( ) ;
if ( zoneStr = = null )
{
await ctx . Reply (
2020-06-21 14:05:04 +00:00
$"Your current system time zone is set to **{ctx.System.UiTz}**. It is currently **{SystemClock.Instance.GetCurrentInstant().FormatZoned(ctx.System)}** in that time zone. To change your system time zone, type `pk;s tz <zone>`." ) ;
2020-02-22 23:57:46 +00:00
return ;
}
2019-06-13 18:33:17 +00:00
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 (
2020-06-21 14:05:04 +00:00
$"This will change the system time zone to **{zone.Id}**. The current time is **{currentTime.FormatZoned()}**. Is this correct?" ) ;
2019-10-05 05:41:00 +00:00
if ( ! await ctx . PromptYesNo ( msg ) ) throw Errors . TimezoneChangeCancelled ;
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { UiTz = zone . Id } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2019-06-13 18:33:17 +00:00
2020-02-22 23:57:46 +00:00
await ctx . Reply ( $"System time zone changed to **{zone.Id}**." ) ;
2019-06-13 18:33:17 +00:00
}
2020-01-11 15:49:20 +00:00
public async Task SystemPrivacy ( Context ctx )
{
ctx . CheckSystem ( ) ;
if ( ! ctx . HasNext ( ) )
{
string PrivacyLevelString ( PrivacyLevel level ) = > level switch
{
PrivacyLevel . Private = > "**Private** (visible only when queried by you)" ,
PrivacyLevel . Public = > "**Public** (visible to everyone)" ,
_ = > throw new ArgumentOutOfRangeException ( nameof ( level ) , level , null )
} ;
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder ( )
2020-01-11 15:49:20 +00:00
. WithTitle ( "Current privacy settings for your system" )
. AddField ( "Description" , PrivacyLevelString ( ctx . System . DescriptionPrivacy ) )
. AddField ( "Member list" , PrivacyLevelString ( ctx . System . MemberListPrivacy ) )
. AddField ( "Current fronter(s)" , PrivacyLevelString ( ctx . System . FrontPrivacy ) )
. AddField ( "Front/switch history" , PrivacyLevelString ( ctx . System . FrontHistoryPrivacy ) )
2020-06-17 19:31:39 +00:00
. WithDescription ( "To edit privacy settings, use the command:\n`pk;system privacy <subject> <level>`\n\n- `subject` is one of `description`, `list`, `front`, `fronthistory`, or `all` \n- `level` is either `public` or `private`." ) ;
2020-01-11 15:49:20 +00:00
await ctx . Reply ( embed : eb . Build ( ) ) ;
return ;
}
PrivacyLevel PopPrivacyLevel ( string subject , out string levelStr , out string levelExplanation )
{
if ( ctx . Match ( "public" , "show" , "shown" , "visible" ) )
{
levelStr = "public" ;
levelExplanation = "be able to query" ;
return PrivacyLevel . Public ;
}
if ( ctx . Match ( "private" , "hide" , "hidden" ) )
{
levelStr = "private" ;
levelExplanation = "*not* be able to query" ;
return PrivacyLevel . Private ;
}
if ( ! ctx . HasNext ( ) )
throw new PKSyntaxError ( $"You must pass a privacy level for `{subject}` (`public` or `private`)" ) ;
2020-06-20 15:36:03 +00:00
throw new PKSyntaxError ( $"Invalid privacy level `{ctx.PopArgument()}` (must be `public` or `private`)." ) ;
2020-01-11 15:49:20 +00:00
}
string levelStr , levelExplanation , subjectStr ;
2020-06-17 19:31:39 +00:00
var subjectList = "`description`, `members`, `front`, `fronthistory`, or `all`" ;
2020-06-29 12:39:19 +00:00
SystemPatch patch = new SystemPatch ( ) ;
2020-01-11 15:49:20 +00:00
if ( ctx . Match ( "description" , "desc" , "text" , "info" ) )
{
subjectStr = "description" ;
2020-06-29 12:39:19 +00:00
patch . DescriptionPrivacy = PopPrivacyLevel ( "description" , out levelStr , out levelExplanation ) ;
2020-01-11 15:49:20 +00:00
}
else if ( ctx . Match ( "members" , "memberlist" , "list" , "mlist" ) )
{
subjectStr = "member list" ;
2020-06-29 12:39:19 +00:00
patch . MemberListPrivacy = PopPrivacyLevel ( "members" , out levelStr , out levelExplanation ) ;
2020-01-11 15:49:20 +00:00
}
else if ( ctx . Match ( "front" , "fronter" ) )
{
subjectStr = "fronter(s)" ;
2020-06-29 12:39:19 +00:00
patch . FrontPrivacy = PopPrivacyLevel ( "front" , out levelStr , out levelExplanation ) ;
2020-01-11 15:49:20 +00:00
}
else if ( ctx . Match ( "switch" , "switches" , "fronthistory" , "fh" ) )
{
subjectStr = "front history" ;
2020-06-29 12:39:19 +00:00
patch . FrontHistoryPrivacy = PopPrivacyLevel ( "fronthistory" , out levelStr , out levelExplanation ) ;
2020-01-11 15:49:20 +00:00
}
2020-06-17 19:31:39 +00:00
else if ( ctx . Match ( "all" ) ) {
subjectStr = "all" ;
PrivacyLevel level = PopPrivacyLevel ( "all" , out levelStr , out levelExplanation ) ;
2020-06-29 12:39:19 +00:00
patch . DescriptionPrivacy = level ;
patch . MemberListPrivacy = level ;
patch . FrontPrivacy = level ;
patch . FrontHistoryPrivacy = level ;
2020-06-17 19:31:39 +00:00
}
2020-01-11 15:49:20 +00:00
else
2020-06-20 15:36:03 +00:00
throw new PKSyntaxError ( $"Invalid privacy subject `{ctx.PopArgument()}` (must be {subjectList})." ) ;
2020-01-11 15:49:20 +00:00
2020-06-29 12:39:19 +00:00
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-06-17 19:31:39 +00:00
if ( subjectStr = = "all" ) {
if ( levelStr = = "private" )
await ctx . Reply ( $"All of your systems privacy settings have been set to **{levelStr}**. Other accounts will now see nothing on the member card." ) ;
else
await ctx . Reply ( $"All of your systems privacy have been set to **{levelStr}**. Other accounts will now see everything on the member card." ) ;
}
//Handle other subjects
else
2020-06-29 12:39:19 +00:00
await ctx . Reply ( $"System {subjectStr} privacy has been set to **{levelStr}**. Other accounts will now {levelExplanation} your system {subjectStr}." ) ;
2020-01-11 15:49:20 +00:00
}
2020-03-21 13:04:41 +00:00
public async Task SystemPing ( Context ctx )
{
2020-06-11 19:32:04 +00:00
ctx . CheckSystem ( ) ;
if ( ! ctx . HasNext ( ) )
{
if ( ctx . System . PingsEnabled ) { await ctx . Reply ( "Reaction pings are currently **enabled** for your system. To disable reaction pings, type `pk;s ping disable`." ) ; }
else { await ctx . Reply ( "Reaction pings are currently **disabled** for your system. To enable reaction pings, type `pk;s ping enable`." ) ; }
}
else {
if ( ctx . Match ( "on" , "enable" ) ) {
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { PingsEnabled = true } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-06-11 19:32:04 +00:00
await ctx . Reply ( "Reaction pings have now been enabled." ) ;
}
if ( ctx . Match ( "off" , "disable" ) ) {
2020-06-29 12:39:19 +00:00
var patch = new SystemPatch { PingsEnabled = false } ;
await _db . Execute ( conn = > conn . UpdateSystem ( ctx . System . Id , patch ) ) ;
2020-06-11 19:32:04 +00:00
await ctx . Reply ( "Reaction pings have now been disabled." ) ;
}
2020-03-21 13:04:41 +00:00
}
2020-06-11 19:32:04 +00:00
}
2020-03-21 13:04:41 +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.
2020-02-12 14:16:19 +00:00
zoneStr = Core . StringUtils . ExtractCountryFlag ( zoneStr ) ? ? zoneStr ;
2019-06-13 21:42:39 +00:00
// 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
}
}
2020-03-21 13:04:41 +00:00
}