-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Discord; | ||
using Discord.WebSocket; | ||
using FluentResults; | ||
using Microsoft.Extensions.Logging; | ||
using Miha.Discord.Services.Interfaces; | ||
using Miha.Shared; | ||
using NodaTime; | ||
using NodaTime.Calendars; | ||
using NodaTime.Extensions; | ||
|
||
namespace Miha.Discord.Services; | ||
|
||
public class GuildScheduledEventService : IGuildScheduledEventService | ||
{ | ||
private readonly DiscordSocketClient _discordClient; | ||
private readonly ILogger<GuildScheduledEventService> _logger; | ||
|
||
public GuildScheduledEventService( | ||
DiscordSocketClient discordClient, | ||
ILogger<GuildScheduledEventService> logger) | ||
{ | ||
_discordClient = discordClient; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<Result<IEnumerable<IGuildScheduledEvent>>> GetScheduledWeeklyEventsAsync(ulong guildId, LocalDate dateOfTheWeek) | ||
{ | ||
var weekNumberInYear = WeekYearRules.Iso.GetWeekOfWeekYear(dateOfTheWeek); | ||
|
||
var guild = _discordClient.GetGuild(guildId); | ||
|
||
if (guild is null) | ||
{ | ||
return Result.Fail<IEnumerable<IGuildScheduledEvent>>("Failed to fetch discord guild"); | ||
} | ||
|
||
var events = await guild.GetEventsAsync(); | ||
|
||
var eventsThisWeek = events.Where(guildEvent => | ||
{ | ||
var estDate = guildEvent.StartTime.ToZonedDateTime() | ||
.WithZone(DateTimeZoneProviders.Tzdb[Timezones.IanaEasternTime]).Date; | ||
var weekOfDate = WeekYearRules.Iso.GetWeekOfWeekYear(estDate); | ||
|
||
return weekOfDate == weekNumberInYear; | ||
}).Cast<IGuildScheduledEvent>(); | ||
|
||
return Result.Ok(eventsThisWeek); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Cronos; | ||
using Discord.Addons.Hosting; | ||
using Discord.Addons.Hosting.Util; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Miha.Discord.Services.Interfaces; | ||
using Miha.Shared.ZonedClocks.Interfaces; | ||
|
||
namespace Miha.Discord.Services.Hosted; | ||
|
||
public class GuildEventScheduleService : DiscordClientService | ||
{ | ||
private readonly DiscordSocketClient _client; | ||
private readonly IEasternStandardZonedClock _easternStandardZonedClock; | ||
private readonly IGuildScheduledEventService _scheduledEventService; | ||
private readonly DiscordOptions _discordOptions; | ||
private readonly ILogger<GuildEventScheduleService> _logger; | ||
private const string Schedule = "0,5,10,15,20,25,30,35,40,45,50,55 8-19 * * *"; // https://crontab.cronhub.io/ | ||
|
||
private readonly CronExpression _cron; | ||
|
||
public GuildEventScheduleService( | ||
DiscordSocketClient client, | ||
IEasternStandardZonedClock easternStandardZonedClock, | ||
IGuildScheduledEventService scheduledEventService, | ||
IOptions<DiscordOptions> discordOptions, | ||
ILogger<GuildEventScheduleService> logger) : base(client, logger) | ||
{ | ||
_client = client; | ||
_easternStandardZonedClock = easternStandardZonedClock; | ||
_scheduledEventService = scheduledEventService; | ||
_discordOptions = discordOptions.Value; | ||
_logger = logger; | ||
|
||
_cron = CronExpression.Parse(Schedule, CronFormat.Standard); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await Client.WaitForReadyAsync(stoppingToken); | ||
|
||
var eventsThisWeek = await _scheduledEventService.GetScheduledWeeklyEventsAsync(_discordOptions.Guild.Value, _easternStandardZonedClock.GetCurrentDate()); | ||
|
||
_logger.LogInformation("Events this week {events}", eventsThisWeek.Value); | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
var utcNow = _easternStandardZonedClock.GetCurrentInstant().ToDateTimeUtc(); | ||
var nextUtc = _cron.GetNextOccurrence(DateTimeOffset.UtcNow, _easternStandardZonedClock.GetTimeZoneInfo()); | ||
|
||
if (nextUtc is null) | ||
{ | ||
_logger.LogWarning("Next utc occurence is null"); | ||
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); | ||
continue; | ||
} | ||
|
||
await Task.Delay(nextUtc.Value - utcNow, stoppingToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Miha.Discord/Services/Interfaces/IGuildScheduledEventService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Discord; | ||
using FluentResults; | ||
using NodaTime; | ||
|
||
namespace Miha.Discord.Services.Interfaces; | ||
|
||
public interface IGuildScheduledEventService | ||
{ | ||
Task<Result<IEnumerable<IGuildScheduledEvent>>> GetScheduledWeeklyEventsAsync(ulong guildId, LocalDate dateOfTheWeek); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters