Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Nellebot/BotOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Nellebot;
namespace Nellebot;

public class BotOptions
{
Expand All @@ -27,19 +25,21 @@ public class BotOptions
/// <summary>
/// Gets discord roles with access to some admin commands.
/// </summary>
public ulong[] TrustedRoleIds { get; init; } = Array.Empty<ulong>();
public ulong[] TrustedRoleIds { get; init; } = [];

/// <summary>
/// Gets channel id where Trusted roles have access to.
/// </summary>
public ulong TrustedChannelId { get; init; }

public ulong ActivityLogChannelId { get; init; }
public ulong ModAlertsChannelId { get; init; }

public ulong ExtendedActivityLogChannelId { get; init; }
public ulong ActivityLogChannelId { get; init; }

public ulong GreetingsChannelId { get; init; }

public ulong OperationLogChannelId { get; init; }

public ulong ErrorLogChannelId { get; init; }

public ulong SuggestionsChannelId { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void LogActivityMessage(
DiscordMessageBuilder activityMessageBuilder = new DiscordMessageBuilder()
.WithContent(activityMessageText)
.AddEmbed(EmbedBuilderHelper.BuildSimpleEmbed(encodedMessageText));
_discordLogger.LogExtendedActivityMessage(activityMessageBuilder);
_discordLogger.LogActivityMessage(activityMessageBuilder);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task Handle(EditMetaMessageCommand request, CancellationToken cance
DiscordMessageBuilder activityMessageBuilder = new DiscordMessageBuilder()
.WithContent(activityMessageText)
.AddEmbed(EmbedBuilderHelper.BuildSimpleEmbed(encodedMessageText));
_discordLogger.LogExtendedActivityMessage(activityMessageBuilder);
_discordLogger.LogActivityMessage(activityMessageBuilder);

DiscordFollowupMessageBuilder followup = new DiscordFollowupMessageBuilder()
.WithContent($"Message edited successfully! [Jump to message]({message.JumpLink})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<TResponse> Handle(
}
catch (Exception ex) when (request is BotCommandCommand commandCommand)
{
await HandeCommandCommandException(commandCommand, ex);
await HandleCommandCommandException(commandCommand, ex);
return default!;
}
catch (Exception ex) when (request is IRequest)
Expand Down Expand Up @@ -104,7 +104,7 @@ private async Task HandleSlashCommandException(BotSlashCommand request, Exceptio
_logger.LogError(ex, nameof(HandleSlashCommandException));
}

private async Task HandeCommandCommandException(BotCommandCommand request, Exception ex)
private async Task HandleCommandCommandException(BotCommandCommand request, Exception ex)
{
CommandContext ctx = request.Ctx;

Expand All @@ -126,6 +126,6 @@ private async Task HandeCommandCommandException(BotCommandCommand request, Excep

_discordErrorLogger.LogCommandError(ctx, ex.ToString());

_logger.LogError(ex, nameof(HandeCommandCommandException));
_logger.LogError(ex, nameof(HandleCommandCommandException));
}
}
6 changes: 3 additions & 3 deletions src/Nellebot/Infrastructure/NoWayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public NoWayGateway(EventQueueChannel eventQueue, DiscordLogger discordLogger)

public async Task ZombiedAsync(IGatewayClient client)
{
_discordLogger.LogExtendedActivityMessage("Connection to gateway zombied. Reconnecting...");
_discordLogger.LogOperationMessage("Connection to gateway zombied. Reconnecting...");
await client.ReconnectAsync();
}

Expand All @@ -46,13 +46,13 @@ public Task ReconnectRequestedAsync(IGatewayClient client)

public Task ReconnectFailedAsync(IGatewayClient client)
{
_discordLogger.LogExtendedActivityMessage("Reconnect failed.");
_discordLogger.LogOperationMessage("Reconnect failed.");
return Task.CompletedTask;
}

public Task SessionInvalidatedAsync(IGatewayClient client)
{
_discordLogger.LogExtendedActivityMessage("Session invalidated.");
_discordLogger.LogOperationMessage("Session invalidated.");
return Task.CompletedTask;
}
}
33 changes: 15 additions & 18 deletions src/Nellebot/Jobs/MigrateResourcesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class MigrateResourcesJob : IJob

private static readonly ResourceChannel[] SourceResourcesChannelIds =
[
new(1333043499200679947, null), // #lang-res
new(1333048476950466570, NynorskTag), // #nn-res
new(1333043544553820222, MediaTag), // #media-res
new(DiscordChannelId: 1333043499200679947, Tag: null), // #lang-res
new(DiscordChannelId: 1333048476950466570, NynorskTag), // #nn-res
new(DiscordChannelId: 1333043544553820222, MediaTag), // #media-res
];
#else
private const ulong ResourcesForumChannelId = 1289968921331634231;
Expand Down Expand Up @@ -74,18 +74,17 @@ public async Task Execute(IJobExecutionContext context)

context.MergedJobDataMap.TryGetBooleanValue("dryRun", out bool isDryRun);

_discordLogger.LogExtendedActivityMessage($"Running job {Key}{(isDryRun ? " (dry run)" : string.Empty)}");
_discordLogger.LogOperationMessage($"Running job {Key}{(isDryRun ? " (dry run)" : string.Empty)}");

var forumPosts = new List<ForumPost>();

DiscordForumChannel resourcesChannel =
await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordForumChannel
?? throw new InvalidOperationException(
$"Could not resolve channel {ResourcesForumChannelId}");
?? throw new InvalidOperationException($"Could not resolve channel {ResourcesForumChannelId}");

IReadOnlyList<DiscordForumTag> channelTags = resourcesChannel.AvailableTags;

_discordLogger.LogExtendedActivityMessage("Collecting messages...");
_discordLogger.LogOperationMessage("Collecting messages...");

var successCount = 0;
var failureCount = 0;
Expand All @@ -103,7 +102,7 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
ForumPost? currentPost = null;

await foreach (DiscordMessage message in discordChannel.GetMessagesAfterAsync(
0,
after: 0,
short.MaxValue,
cancellationToken))
{
Expand Down Expand Up @@ -165,7 +164,7 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
}
}

_discordLogger.LogExtendedActivityMessage("Done collecting messages");
_discordLogger.LogOperationMessage("Done collecting messages");

// Post messages in forum channel in chronological order
List<ForumPost> orderedForumPosts =
Expand All @@ -174,10 +173,9 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
.OrderBy(x => x.FirstContentMessage.Id)
.ToList();

_discordLogger.LogExtendedActivityMessage(
$"{orderedForumPosts.Count} resource forum posts will be created");
_discordLogger.LogOperationMessage($"{orderedForumPosts.Count} resource forum posts will be created");

_discordLogger.LogExtendedActivityMessage($"{skippedCount} will be skipped");
_discordLogger.LogOperationMessage($"{skippedCount} will be skipped");

foreach (ForumPost forumPost in orderedForumPosts)
{
Expand Down Expand Up @@ -220,8 +218,7 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
forumPostBuilder = tags.Aggregate(forumPostBuilder, (current, tag) => current.AddTag(tag));

var sb = new StringBuilder();
sb.AppendLineLF(
$"Creating post for message {forumPost.FirstContentMessage.JumpLink}")
sb.AppendLineLF($"Creating post for message {forumPost.FirstContentMessage.JumpLink}")
.AppendLineLF($"Title: {title}")
.AppendLineLF($"Content: {content.Sum(x => x.Length)} chars in {content.Count} message(s)")
.AppendLineLF(
Expand All @@ -230,7 +227,7 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
.AppendLineLF($"Images: {imageAttachments.Count}")
.AppendLineLF($"Non image files: {forumPost.NonImageAttachmentCount}");

_discordLogger.LogExtendedActivityMessage(sb.ToString().TrimEnd());
_discordLogger.LogOperationMessage(sb.ToString().TrimEnd());

// Gives us some time to follow along and to cancel if something goes wrong
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
Expand Down Expand Up @@ -279,8 +276,8 @@ await _discordResolver.ResolveChannelAsync(ResourcesForumChannelId) as DiscordFo
}
}

_discordLogger.LogExtendedActivityMessage($"Successful forum posts: {successCount}");
_discordLogger.LogExtendedActivityMessage($"Failed forum posts: {failureCount}");
_discordLogger.LogOperationMessage($"Successful forum posts: {successCount}");
_discordLogger.LogOperationMessage($"Failed forum posts: {failureCount}");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -385,7 +382,7 @@ public List<string> GetTextContent()
sb.Clear();

// Split text into chunks of max 2000 chars each, split by newlines
string[] textLines = allText.Split('\n').ToArray();
string[] textLines = allText.Split('\n');

var result = new List<string>();

Expand Down
31 changes: 13 additions & 18 deletions src/Nellebot/Jobs/RoleMaintenanceJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class RoleMaintenanceJob : IJob
public static readonly JobKey Key = new("role-maintenance", "default");

private readonly DiscordClient _client;
private readonly DiscordLogger _discordLogger;
private readonly IDiscordErrorLogger _discordErrorLogger;
private readonly DiscordLogger _discordLogger;
private readonly BotOptions _options;

public RoleMaintenanceJob(
Expand All @@ -37,7 +37,7 @@ public async Task Execute(IJobExecutionContext context)
{
try
{
_discordLogger.LogExtendedActivityMessage($"Job started: {Key}");
_discordLogger.LogOperationMessage($"Job started: {Key}");

CancellationToken cancellationToken = context.CancellationToken;

Expand All @@ -49,11 +49,11 @@ public async Task Execute(IJobExecutionContext context)

DiscordGuild guild = _client.Guilds[guildId];

_discordLogger.LogExtendedActivityMessage("Downloading guild members.");
_discordLogger.LogOperationMessage("Downloading guild members.");

List<DiscordMember> allMembers = await guild.GetAllMembersAsync(cancellationToken).ToListAsync();

_discordLogger.LogExtendedActivityMessage($"Downloaded {allMembers.Count} guild members.");
_discordLogger.LogOperationMessage($"Downloaded {allMembers.Count} guild members.");

DiscordRole memberRole = guild.Roles[memberRoleId]
?? throw new Exception($"Could not find member role with id {memberRoleId}");
Expand All @@ -68,7 +68,7 @@ public async Task Execute(IJobExecutionContext context)

await RemoveUnneededGhostRoles(allMembers, ghostRole, cancellationToken);

_discordLogger.LogExtendedActivityMessage($"Job finished: {Key}");
_discordLogger.LogOperationMessage($"Job finished: {Key}");
}
catch (Exception ex)
{
Expand All @@ -92,16 +92,15 @@ private async Task AddMissingMemberRoles(
{
int totalCount = missingMemberRoleMembers.Count;

_discordLogger.LogExtendedActivityMessage(
_discordLogger.LogOperationMessage(
$"Found {missingMemberRoleMembers.Count} users which are missing the Member role.");

int successCount = await ExecuteRoleChangeWithRetry(
missingMemberRoleMembers,
m => m.GrantRoleAsync(memberRole),
cancellationToken);

_discordLogger.LogExtendedActivityMessage(
$"Done adding Member role for {successCount}/{totalCount} users.");
_discordLogger.LogOperationMessage($"Done adding Member role for {successCount}/{totalCount} users.");
}

return;
Expand Down Expand Up @@ -137,16 +136,14 @@ private async Task RemoveUnneededMemberRoles(
{
int totalCount = memberRoleCandidates.Count;

_discordLogger.LogExtendedActivityMessage(
$"Found {memberRoleCandidates.Count} users with unneeded Member role.");
_discordLogger.LogOperationMessage($"Found {memberRoleCandidates.Count} users with unneeded Member role.");

int successCount = await ExecuteRoleChangeWithRetry(
memberRoleCandidates,
m => m.RevokeRoleAsync(memberRole),
cancellationToken);

_discordLogger.LogExtendedActivityMessage(
$"Done removing Member role for {successCount}/{totalCount} users.");
_discordLogger.LogOperationMessage($"Done removing Member role for {successCount}/{totalCount} users.");
}

return;
Expand Down Expand Up @@ -180,15 +177,15 @@ private async Task AddMissingGhostRoles(
{
int totalCount = ghostRoleCandidates.Count;

_discordLogger.LogExtendedActivityMessage(
_discordLogger.LogOperationMessage(
$"Found {ghostRoleCandidates.Count} users which are missing the Ghost role.");

int successCount = await ExecuteRoleChangeWithRetry(
ghostRoleCandidates,
m => m.GrantRoleAsync(ghostRole),
cancellationToken);

_discordLogger.LogExtendedActivityMessage($"Done adding Ghost role for {successCount}/{totalCount} users.");
_discordLogger.LogOperationMessage($"Done adding Ghost role for {successCount}/{totalCount} users.");
}
}

Expand All @@ -205,16 +202,14 @@ private async Task RemoveUnneededGhostRoles(
{
int totalCount = ghostRoleCandidates.Count;

_discordLogger.LogExtendedActivityMessage(
$"Found {ghostRoleCandidates.Count} users with unneeded Ghost role.");
_discordLogger.LogOperationMessage($"Found {ghostRoleCandidates.Count} users with unneeded Ghost role.");

int successCount = await ExecuteRoleChangeWithRetry(
ghostRoleCandidates,
m => m.RevokeRoleAsync(ghostRole),
cancellationToken);

_discordLogger.LogExtendedActivityMessage(
$"Done removing Ghost role for {successCount}/{totalCount} users.");
_discordLogger.LogOperationMessage($"Done removing Ghost role for {successCount}/{totalCount} users.");
}
}

Expand Down
Loading
Loading