Added early event start notifications (#5)

totally didn't take 2 painful days

Co-authored-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Macintxsh 2022-10-23 12:49:49 +03:00 committed by GitHub
parent 7fe6549bb3
commit 58eceab771
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 2 deletions

View file

@ -49,7 +49,8 @@ public static class Boyfriend {
{ "EventCreatedChannel", "0" }, { "EventCreatedChannel", "0" },
{ "EventStartedChannel", "0" }, { "EventStartedChannel", "0" },
{ "EventCancelledChannel", "0" }, { "EventCancelledChannel", "0" },
{ "EventCompletedChannel", "0" } { "EventCompletedChannel", "0" },
{ "EventEarlyNotificationOffset", "0" }
}; };
public static void Main() { public static void Main() {

View file

@ -132,6 +132,9 @@ public static class EventHandler {
scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), Utils.Wrap(scheduledEvent.Description)), scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), Utils.Wrap(scheduledEvent.Description)),
true); true);
} }
if (eventConfig["EventEarlyNotificationOffset"] != "0") {
_ = Utils.SendEarlyEventStartNotificationAsync(channel, scheduledEvent, Convert.ToInt32(eventConfig["EventEarlyNotificationOffset"]));
}
} }
private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) { private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) {

View file

@ -410,6 +410,15 @@ namespace Boyfriend {
} }
} }
/// <summary>
/// Looks up a localized string similar to {0}Event {1} will start &lt;t:{2}:R&gt;!.
/// </summary>
internal static string EventEarlyNotification {
get {
return ResourceManager.GetString("EventEarlyNotification", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to {0}Event {1} is starting at {2}!. /// Looks up a localized string similar to {0}Event {1} is starting at {2}!.
/// </summary> /// </summary>
@ -788,6 +797,15 @@ namespace Boyfriend {
} }
} }
/// <summary>
/// Looks up a localized string similar to .
/// </summary>
internal static string SettingsEventEarlyNotificationOffset {
get {
return ResourceManager.GetString("SettingsEventEarlyNotificationOffset", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Role for event creation notifications. /// Looks up a localized string similar to Role for event creation notifications.
/// </summary> /// </summary>

View file

@ -384,4 +384,10 @@
<data name="ServerBlacklisted" xml:space="preserve"> <data name="ServerBlacklisted" xml:space="preserve">
<value>This feature is unavailable because this guild is currently blacklisted.</value> <value>This feature is unavailable because this guild is currently blacklisted.</value>
</data> </data>
<data name="EventEarlyNotification" xml:space="preserve">
<value>{0}Event {1} will start &lt;t:{2}:R&gt;!</value>
</data>
<data name="SettingsEventEarlyNotificationOffset" xml:space="preserve">
<value>Early event start notification offset</value>
</data>
</root> </root>

View file

@ -375,4 +375,10 @@
<data name="ServerBlacklisted" xml:space="preserve"> <data name="ServerBlacklisted" xml:space="preserve">
<value>Эта функция недоступна потому что этот сервер находится в чёрном списке.</value> <value>Эта функция недоступна потому что этот сервер находится в чёрном списке.</value>
</data> </data>
<data name="EventEarlyNotification" xml:space="preserve">
<value>{0}Событие {1} начнется &lt;t:{2}:R&gt;!</value>
</data>
<data name="SettingsEventEarlyNotificationOffset" xml:space="preserve">
<value>Офсет отправки преждевременного уведомления о начале события</value>
</data>
</root> </root>

View file

@ -371,4 +371,10 @@
<data name="ServerBlacklisted" xml:space="preserve"> <data name="ServerBlacklisted" xml:space="preserve">
<value>упс, кажется ваш сервер в черном списке, и я вам ничем помочь не смогу)</value> <value>упс, кажется ваш сервер в черном списке, и я вам ничем помочь не смогу)</value>
</data> </data>
<data name="EventEarlyNotification" xml:space="preserve">
<value>{0}квест {1} начнется &lt;t:{2}:R&gt;!</value>
</data>
<data name="SettingsEventEarlyNotificationOffset" xml:space="preserve">
<value>заранее пнуть в минутах до начала квеста</value>
</data>
</root> </root>

View file

@ -167,4 +167,22 @@ public static class Utils {
return guild.GetUser(196160375593369600) != null && guild.OwnerId != 326642240229474304 && return guild.GetUser(196160375593369600) != null && guild.OwnerId != 326642240229474304 &&
guild.OwnerId != 504343489664909322; 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();
}
} }