From 58eceab771056006c1f67b72ff7b44858e572f84 Mon Sep 17 00:00:00 2001 From: mctaylors <95250141+mctaylors@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:49:49 +0300 Subject: [PATCH] Added early event start notifications (#5) totally didn't take 2 painful days Co-authored-by: Octol1ttle --- Boyfriend/Boyfriend.cs | 3 ++- Boyfriend/EventHandler.cs | 3 +++ Boyfriend/Messages.Designer.cs | 18 ++++++++++++++++++ Boyfriend/Messages.resx | 6 ++++++ Boyfriend/Messages.ru.resx | 6 ++++++ Boyfriend/Messages.tt-ru.resx | 8 +++++++- Boyfriend/Utils.cs | 18 ++++++++++++++++++ 7 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Boyfriend/Boyfriend.cs b/Boyfriend/Boyfriend.cs index e8f7ee2..4528e32 100644 --- a/Boyfriend/Boyfriend.cs +++ b/Boyfriend/Boyfriend.cs @@ -49,7 +49,8 @@ public static class Boyfriend { { "EventCreatedChannel", "0" }, { "EventStartedChannel", "0" }, { "EventCancelledChannel", "0" }, - { "EventCompletedChannel", "0" } + { "EventCompletedChannel", "0" }, + { "EventEarlyNotificationOffset", "0" } }; public static void Main() { diff --git a/Boyfriend/EventHandler.cs b/Boyfriend/EventHandler.cs index 02750bd..a67dde6 100644 --- a/Boyfriend/EventHandler.cs +++ b/Boyfriend/EventHandler.cs @@ -132,6 +132,9 @@ public static class EventHandler { scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), Utils.Wrap(scheduledEvent.Description)), true); } + if (eventConfig["EventEarlyNotificationOffset"] != "0") { + _ = Utils.SendEarlyEventStartNotificationAsync(channel, scheduledEvent, Convert.ToInt32(eventConfig["EventEarlyNotificationOffset"])); + } } private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) { diff --git a/Boyfriend/Messages.Designer.cs b/Boyfriend/Messages.Designer.cs index c4ab511..0e8babe 100644 --- a/Boyfriend/Messages.Designer.cs +++ b/Boyfriend/Messages.Designer.cs @@ -410,6 +410,15 @@ namespace Boyfriend { } } + /// + /// Looks up a localized string similar to {0}Event {1} will start <t:{2}:R>!. + /// + internal static string EventEarlyNotification { + get { + return ResourceManager.GetString("EventEarlyNotification", resourceCulture); + } + } + /// /// Looks up a localized string similar to {0}Event {1} is starting at {2}!. /// @@ -788,6 +797,15 @@ namespace Boyfriend { } } + /// + /// Looks up a localized string similar to . + /// + internal static string SettingsEventEarlyNotificationOffset { + get { + return ResourceManager.GetString("SettingsEventEarlyNotificationOffset", resourceCulture); + } + } + /// /// Looks up a localized string similar to Role for event creation notifications. /// diff --git a/Boyfriend/Messages.resx b/Boyfriend/Messages.resx index cbb20ea..6615656 100644 --- a/Boyfriend/Messages.resx +++ b/Boyfriend/Messages.resx @@ -384,4 +384,10 @@ This feature is unavailable because this guild is currently blacklisted. + + {0}Event {1} will start <t:{2}:R>! + + + Early event start notification offset + diff --git a/Boyfriend/Messages.ru.resx b/Boyfriend/Messages.ru.resx index 7fa1a22..5f92c9d 100644 --- a/Boyfriend/Messages.ru.resx +++ b/Boyfriend/Messages.ru.resx @@ -375,4 +375,10 @@ Эта функция недоступна потому что этот сервер находится в чёрном списке. + + {0}Событие {1} начнется <t:{2}:R>! + + + Офсет отправки преждевременного уведомления о начале события + diff --git a/Boyfriend/Messages.tt-ru.resx b/Boyfriend/Messages.tt-ru.resx index 0d676cd..d5ecf15 100644 --- a/Boyfriend/Messages.tt-ru.resx +++ b/Boyfriend/Messages.tt-ru.resx @@ -371,4 +371,10 @@ упс, кажется ваш сервер в черном списке, и я вам ничем помочь не смогу) - \ No newline at end of file + + {0}квест {1} начнется <t:{2}:R>! + + + заранее пнуть в минутах до начала квеста + + diff --git a/Boyfriend/Utils.cs b/Boyfriend/Utils.cs index f089435..fddfe76 100644 --- a/Boyfriend/Utils.cs +++ b/Boyfriend/Utils.cs @@ -167,4 +167,22 @@ public static class Utils { return guild.GetUser(196160375593369600) != null && guild.OwnerId != 326642240229474304 && guild.OwnerId != 504343489664909322; } + + public static async Task SendEarlyEventStartNotificationAsync(SocketTextChannel? channel, SocketGuildEvent scheduledEvent, int minuteOffset) { + await Task.Delay(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Subtract(TimeSpan.FromMinutes(minuteOffset))); + var guild = scheduledEvent.Guild; + if (guild.GetEvent(scheduledEvent.Id) is null) return; + var eventConfig = Boyfriend.GetGuildConfig(guild.Id); + + var receivers = eventConfig["EventStartedReceivers"]; + var role = guild.GetRole(Convert.ToUInt64(eventConfig["EventNotifyReceiverRole"])); + var mentions = Boyfriend.StringBuilder; + + if (receivers.Contains("role") && role != null) mentions.Append($"{role.Mention} "); + if (receivers.Contains("users") || receivers.Contains("interested")) + mentions = (await scheduledEvent.GetUsersAsync(15)).Aggregate(mentions, + (current, user) => current.Append($"{user.Mention} ")); + await channel?.SendMessageAsync(string.Format(Messages.EventEarlyNotification, mentions, Wrap(scheduledEvent.Name), scheduledEvent.StartTime.ToUnixTimeSeconds()))!; + mentions.Clear(); + } }