Skip to content

Commit

Permalink
Update GuildEventScheduleService.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Nov 14, 2023
1 parent 1c60220 commit 921472d
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/Miha.Discord/Services/Hosted/GuildEventScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public GuildEventScheduleService(

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

await Client.WaitForReadyAsync(stoppingToken);

while (!stoppingToken.IsCancellationRequested)
Expand All @@ -64,6 +66,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

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

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

private async Task PostWeeklyScheduleAsync()
Expand Down Expand Up @@ -125,12 +129,23 @@ private async Task PostWeeklyScheduleAsync()

eventsByDay[day].Add(guildScheduledEvent);
}

var postedHeader = false;
var postedFooter = false;

foreach (var (day, events) in eventsByDay)
{
var builder = new StringBuilder();
var embed = new EmbedBuilder();
var description = new StringBuilder();

if (!postedHeader && day == eventsByDay.First().Key)
{
embed.WithAuthor(string.Empty, _client.CurrentUser.GetAvatarUrl());
description.AppendLine("# Weekly event schedule");
postedHeader = true;
}

builder.AppendLine("### " + day + " - " + DiscordTimestampExtensions.ToDiscordTimestamp(events.First().StartTime.Date, TimestampTagStyles.ShortDate));
description.AppendLine("### " + day + " - " + DiscordTimestampExtensions.ToDiscordTimestamp(events.First().StartTime.Date, TimestampTagStyles.ShortDate));

foreach (var guildEvent in events)
{
Expand All @@ -142,18 +157,27 @@ private async Task PostWeeklyScheduleAsync()
location = "Discord";
}

builder.AppendLine($"- [{location} - {guildEvent.Name}]({url})");
builder.AppendLine($" - {guildEvent.StartTime.ToDiscordTimestamp(TimestampTagStyles.ShortTime)} - {guildEvent.StartTime.ToDiscordTimestamp(TimestampTagStyles.Relative)}");
description.AppendLine($"- [{location} - {guildEvent.Name}]({url})");
description.AppendLine($" - {guildEvent.StartTime.ToDiscordTimestamp(TimestampTagStyles.ShortTime)} - {guildEvent.StartTime.ToDiscordTimestamp(TimestampTagStyles.Relative)}");

if (guildEvent.Creator is not null)
{
builder.AppendLine($" - Hosted by {guildEvent.Creator.Mention}");
description.AppendLine($" - Hosted by {guildEvent.Creator.Mention}");
}
}

var embed = new EmbedBuilder()
.WithColor(Color.Magenta)
.WithDescription(builder.ToString());
if (!postedFooter && day == eventsByDay.Last().Key)
{
embed
.WithVersionFooter()
.WithCurrentTimestamp();

postedFooter = true;
}

embed
.WithColor(new Color(255, 43, 241))
.WithDescription(description.ToString());

await weeklyScheduleChannel.SendMessageAsync(embed: embed.Build());

Expand Down

0 comments on commit 921472d

Please sign in to comment.