Skip to content

Commit

Permalink
fix logger message
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Jan 27, 2024
1 parent 6692823 commit 6dd7de3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Miha, a discord bot for Midnight Haven

- .NET 7 based, Redis backed
- .NET 8 based, Redis backed
- [Discord.NET based](https://github.com/discord-net/Discord.Net)
- Built & structured around dependency injection
- Rapidly integrated using GitHub Actions
Expand Down
21 changes: 11 additions & 10 deletions src/Miha.Discord/Services/Hosted/GuildEventMonitorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class GuildEventMonitorService(
ILogger<GuildEventMonitorService> logger) : DiscordClientService(client, logger)
{
private readonly DiscordOptions _discordOptions = discordOptions.Value;
private readonly ILogger<GuildEventMonitorService> _logger = logger;

private const string Schedule = "0,5,10,15,20,25,30,35,40,45,50,55 ? * * *"; // https://crontab.cronhub.io/

Expand All @@ -34,7 +35,7 @@ public partial class GuildEventMonitorService(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Waiting for client to be ready...");
_logger.LogInformation("Waiting for client to be ready...");

await Client.WaitForReadyAsync(stoppingToken);

Expand All @@ -47,14 +48,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

if (nextUtc is null)
{
logger.LogWarning("Next utc occurence is null");
_logger.LogWarning("Next utc occurence is null");
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
continue;
}

var next = nextUtc.Value - utcNow;

logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3));
_logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3));

await Task.Delay(nextUtc.Value - utcNow, stoppingToken);
}
Expand All @@ -69,7 +70,7 @@ private async Task CheckScheduledEventsAsync()
guild = Client.GetGuild(_discordOptions.Guild!.Value);
if (guild is null)
{
logger.LogCritical("Guild is null {GuildId}", _discordOptions.Guild.Value);
_logger.LogCritical("Guild is null {GuildId}", _discordOptions.Guild.Value);
return;
}
}
Expand All @@ -88,11 +89,11 @@ private async Task CheckScheduledEventsAsync()
{
if (announcementChannel.Reasons.Any(m => m.Message == "Announcement channel not set"))
{
logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
_logger.LogDebug("Guild announcement channel not set {GuildId} {EventId}", guildEvent.Guild.Id, guildEvent.Id);
return;
}

logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guild.Id, guildEvent.Id);
_logger.LogInformation("Failed getting announcement channel for guild {GuildId} {EventId}", guild.Id, guildEvent.Id);
continue;
}

Expand All @@ -104,17 +105,17 @@ private async Task CheckScheduledEventsAsync()
startsIn = new TimeSpan(startsIn.Days, startsIn.Hours, startsIn.Minutes + 1, 0);
}

logger.LogDebug("GuildEvent {EventId} starts in (rounded) {StartsInTotalMinutes}", guildEvent.Id, startsIn.TotalMinutes);
_logger.LogDebug("GuildEvent {EventId} starts in (rounded) {StartsInTotalMinutes}", guildEvent.Id, startsIn.TotalMinutes);

if (startsIn.TotalMinutes is < 5 or > 20 || _memoryCache.TryGetValue(guildEvent.Id, out bool notified) && notified)
{
logger.LogDebug("GuildEvent {EventId} starts too soon or is already notified", guildEvent.Id);
_logger.LogDebug("GuildEvent {EventId} starts too soon or is already notified", guildEvent.Id);
continue;
}

if (logger.IsEnabled(LogLevel.Debug))
if (_logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("GuildEvent {EventId} {GuildEventJson}", guildEvent.Id, JsonConvert.SerializeObject(new
_logger.LogDebug("GuildEvent {EventId} {GuildEventJson}", guildEvent.Id, JsonConvert.SerializeObject(new
{
guildEvent.StartTime,
guildEvent.Id,
Expand Down
31 changes: 16 additions & 15 deletions src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public partial class GuildEventScheduleService(
{
private readonly DiscordSocketClient _client = client;
private readonly DiscordOptions _discordOptions = discordOptions.Value;
private readonly ILogger<GuildEventScheduleService> _logger = logger;

private const string Schedule = "0,5,10,15,20,25,30,35,40,45,50,55 * * * *"; // https://crontab.cronhub.io/

private readonly CronExpression _cron = CronExpression.Parse(Schedule, CronFormat.Standard);

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Waiting for client to be ready...");
_logger.LogInformation("Waiting for client to be ready...");

await Client.WaitForReadyAsync(stoppingToken);

Expand All @@ -45,7 +46,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (HttpException e)
{
logger.LogWarning(e, "Discord dotnet http exception caught, likely caused by rate-limits, waiting a few minutes before continuing");
_logger.LogWarning(e, "Discord dotnet http exception caught, likely caused by rate-limits, waiting a few minutes before continuing");

await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken);

Expand All @@ -57,26 +58,26 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

if (nextUtc is null)
{
logger.LogWarning("Next utc occurence is null");
_logger.LogWarning("Next utc occurence is null");
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
continue;
}

var next = nextUtc.Value - utcNow;

logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3));
_logger.LogDebug("Waiting {Time} until next operation", next.Humanize(3));

await Task.Delay(nextUtc.Value - utcNow, stoppingToken);
}

logger.LogInformation("Hosted service ended");
_logger.LogInformation("Hosted service ended");
}

private async Task PostWeeklyScheduleAsync()
{
if (_discordOptions.Guild is null)
{
logger.LogWarning("Guild isn't configured");
_logger.LogWarning("Guild isn't configured");
return;
}

Expand All @@ -85,13 +86,13 @@ private async Task PostWeeklyScheduleAsync()

if (guildResult.IsFailed || guild is null)
{
logger.LogWarning("Guild doc failed, or the guild is null for some reason {Errors}", guildResult.Errors);
_logger.LogWarning("Guild doc failed, or the guild is null for some reason {Errors}", guildResult.Errors);
return;
}

if (guild.WeeklyScheduleChannel is null)
{
logger.LogDebug("Guild doesn't have a configured weekly schedule channel");
_logger.LogDebug("Guild doesn't have a configured weekly schedule channel");
return;
}

Expand All @@ -100,7 +101,7 @@ private async Task PostWeeklyScheduleAsync()

if (eventsThisWeekResult.IsFailed || eventsThisWeek is null)
{
logger.LogWarning("Fetching this weeks events failed, or is null {Errors}", eventsThisWeekResult.Errors);
_logger.LogWarning("Fetching this weeks events failed, or is null {Errors}", eventsThisWeekResult.Errors);
return;
}

Expand All @@ -109,7 +110,7 @@ private async Task PostWeeklyScheduleAsync()

if (weeklyScheduleChannelResult.IsFailed || weeklyScheduleChannel is null)
{
logger.LogWarning("Fetching the guilds weekly schedule channel failed, or is null {Errors}", weeklyScheduleChannelResult.Errors);
_logger.LogWarning("Fetching the guilds weekly schedule channel failed, or is null {Errors}", weeklyScheduleChannelResult.Errors);
return;
}

Expand All @@ -127,7 +128,7 @@ private async Task PostWeeklyScheduleAsync()
}
}

logger.LogInformation("Updating weekly schedule");
_logger.LogInformation("Updating weekly schedule");

var postedHeader = false;
var postedFooter = false;
Expand Down Expand Up @@ -158,15 +159,15 @@ private async Task PostWeeklyScheduleAsync()
{
var deletedMessages = 0;

logger.LogInformation("Wiping posted messages");
_logger.LogInformation("Wiping posted messages");

foreach (var message in messagesToDelete)
{
await message.DeleteAsync();
deletedMessages++;
}

logger.LogInformation("Deleted {DeletedMessages} messages", deletedMessages);
_logger.LogInformation("Deleted {DeletedMessages} messages", deletedMessages);

// Update the messages list
messages = (await weeklyScheduleChannel
Expand Down Expand Up @@ -268,7 +269,7 @@ private async Task PostWeeklyScheduleAsync()

if (lastPostedMessage is null)
{
logger.LogInformation("Posting new message");
_logger.LogInformation("Posting new message");
await weeklyScheduleChannel.SendMessageAsync(embed: embed.Build());
}
else
Expand All @@ -280,7 +281,7 @@ await weeklyScheduleChannel.ModifyMessageAsync(lastPostedMessage.Id, props =>
}
}

logger.LogInformation("Finished updating weekly schedule");
_logger.LogInformation("Finished updating weekly schedule");
}

[LoggerMessage(EventId = 1, Level = LogLevel.Error, Message = "Exception occurred")]
Expand Down
1 change: 1 addition & 0 deletions src/Miha.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Dockerfile = Dockerfile
..\docker-compose.dev.yml = ..\docker-compose.dev.yml
appsettings.edge.json = appsettings.edge.json
..\readme.md = ..\readme.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Miha.Redis", "Miha.Redis\Miha.Redis.csproj", "{51E2A7AC-9B3D-4453-89E7-EF81DAE37E41}"
Expand Down

0 comments on commit 6dd7de3

Please sign in to comment.