diff --git a/PluralKit.Bot/Commands/SystemCommands.cs b/PluralKit.Bot/Commands/SystemCommands.cs index 491879d4..2de9d0b3 100644 --- a/PluralKit.Bot/Commands/SystemCommands.cs +++ b/PluralKit.Bot/Commands/SystemCommands.cs @@ -20,7 +20,10 @@ namespace PluralKit.Bot.Commands public SystemStore Systems {get; set;} public MemberStore Members {get; set;} + + public SwitchStore Switches {get; set;} public EmbedService EmbedService {get; set;} + [Command] public async Task Query(PKSystem system = null) { @@ -145,8 +148,22 @@ namespace PluralKit.Bot.Commands } } + [Command("fronter")] + public async Task SystemFronter() + { + var system = ContextEntity ?? Context.SenderSystem; + if (system == null) throw Errors.NoSystemError; + + var sw = await Switches.GetLatestSwitch(system); + if (sw == null) throw Errors.NoRegisteredSwitches; + + var members = await Switches.GetSwitchMembers(sw); + await Context.Channel.SendMessageAsync(embed: EmbedService.CreateFronterEmbed(sw, members.ToList())); + } + [Command("timezone")] [Remarks("system timezone [timezone]")] + [MustHaveSystem] public async Task SystemTimezone([Remainder] string zoneStr = null) { if (zoneStr == null) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 3c1af3f3..edbf0fad 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -1,7 +1,9 @@ +using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; using Discord; +using NodaTime; namespace PluralKit.Bot { public class EmbedService { @@ -50,8 +52,7 @@ namespace PluralKit.Bot { var name = member.Name; if (system.Name != null) name = $"{member.Name} ({system.Name})"; - var color = Color.Default; - if (member.Color != null) color = new Color(uint.Parse(member.Color, NumberStyles.HexNumber)); + var color = member.Color?.ToDiscordColor() ?? Color.Default; var messageCount = await _members.MessageCount(member); @@ -69,5 +70,15 @@ namespace PluralKit.Bot { return eb.Build(); } + + public Embed CreateFronterEmbed(PKSwitch sw, ICollection members) + { + var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp; + return new EmbedBuilder() + .WithColor(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? Color.Blue) + .AddField("Current fronter", members.Count > 0 ? string.Join(", ", members.Select(m => m.Name)) : "*(no fronter)*", true) + .AddField("Since", $"{sw.Timestamp.ToString(Formats.DateTimeFormat, null)} ({timeSinceSwitch.ToString(Formats.DurationFormat, null)} ago)", true) + .Build(); + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Utils.cs b/PluralKit.Bot/Utils.cs index 726737b6..606ad81d 100644 --- a/PluralKit.Bot/Utils.cs +++ b/PluralKit.Bot/Utils.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -20,6 +21,13 @@ namespace PluralKit.Bot return $"{user.Username}#{user.Discriminator} ({user.Mention})"; } + public static Color? ToDiscordColor(this string color) + { + if (uint.TryParse(color, NumberStyles.HexNumber, null, out var colorInt)) + return new Color(colorInt); + throw new ArgumentException($"Invalid color string '{color}'."); + } + public static async Task VerifyAvatarOrThrow(string url) { // List of MIME types we consider acceptable