diff --git a/backend/Bot/Abstractions/Command.cs b/backend/Bot/Abstractions/Command.cs index dee268238..e25760a65 100644 --- a/backend/Bot/Abstractions/Command.cs +++ b/backend/Bot/Abstractions/Command.cs @@ -23,7 +23,9 @@ public abstract class Command : InteractionModuleBase - + - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -23,11 +23,11 @@ - + - + - + \ No newline at end of file diff --git a/backend/Bot/Services/DiscordBot.cs b/backend/Bot/Services/DiscordBot.cs index 85bb5551a..7f2f6f10f 100644 --- a/backend/Bot/Services/DiscordBot.cs +++ b/backend/Bot/Services/DiscordBot.cs @@ -93,7 +93,7 @@ private async Task HandleInteraction(SocketInteraction arg) } catch (Exception) { - _logger.LogInformation($"Unable to execute {arg.Type} in channel {arg.Channel}"); + _logger.LogInformation("Unable to execute {ArgType} in channel {ArgChannel}", arg.Type, arg.Channel); if (arg.Type is InteractionType.ApplicationCommand) await arg.GetOriginalResponseAsync().ContinueWith(async msg => await msg.Result.DeleteAsync()); @@ -145,7 +145,7 @@ private async Task ReadyHandler() } catch (Exception ex) { - _logger.LogError(ex, $"Something went wrong while handling guild join for {guild.Id}."); + _logger.LogError(ex, "Something went wrong while handling guild join for {GuildId}.", guild.Id); } } @@ -171,9 +171,9 @@ private static Task Log(LogMessage logMessage, ILogger logger) }; if (logMessage.Exception is null) - logger.Log(level, logMessage.Message); + logger.Log(level, "{Message}", logMessage.Message); else - logger.LogError(logMessage.Exception, logMessage.Message); + logger.LogError(logMessage.Exception, "{Message}", logMessage.Message); return Task.CompletedTask; } @@ -184,7 +184,7 @@ await _interactions.RegisterCommandsToGuildAsync( guild.Id ); - _logger.LogInformation($"Initialized guild commands for guild {guild.Name}."); + _logger.LogInformation("Initialized guild commands for guild {GuildName}.", guild.Name); } private Task GuildBanRemoved(SocketUser user, SocketGuild guild) @@ -248,7 +248,7 @@ private Task GuildUserUpdatedHandler(Cacheable oldUsrCac private Task GuildCreatedHandler(SocketGuild guild) { - _logger.LogInformation($"Joined guild '{guild.Name}' with ID: '{guild.Id}'"); + _logger.LogInformation("Joined guild {GuildName} ({GuildId})", guild.Name, guild.Id); return Task.CompletedTask; } @@ -288,7 +288,7 @@ private async Task CmdErrorHandler(SlashCommandInfo info, IInteractionContext co { await SendError(info, translation, context, $"{result.ErrorReason}\n{exception.Message}", result.Error.Value.ToString()); - _logger.LogError($"Command '{info.Name}' invoked by '{context.User.Username}' failed: " + + _logger.LogError("Command '{Name}' invoked by '{Username}' failed: {Message}", info.Name, context.User.Username, exception.Message + "\n" + exception.StackTrace); } @@ -301,7 +301,8 @@ private async Task CmdErrorHandler(SlashCommandInfo info, IInteractionContext co await SendError(info, translation, context, result.ErrorReason, result.Error.Value.ToString()); _logger.LogError( - $"Command '{info.Name}' ({result.GetType()}) invoked by '{context.User.Username}' failed due to {result.Error}: {result.ErrorReason}."); + "Command '{Name}' ({Result}) invoked by '{Username}' failed due to {Error}: {ErrorReason}.", + info.Name, result.GetType(), context.User.Username, result.Error, result.ErrorReason); _eventHandler.CommandErroredEvent.Invoke( new Exception($"{result.ErrorReason}\nResult Type: {result.GetType()}")); @@ -323,7 +324,9 @@ private async Task SendError(ICommandInfo info, Translation translation, IIntera string errorReason, string code) { _logger.LogError( - $"Command '{info.Name}' invoked by '{context.User.Username}' failed: {errorReason}"); + "Command '{Name}' invoked by '{Username}' failed: {ErrorReason}", + info.Name, context.User.Username, errorReason + ); var builder = new EmbedBuilder() .WithTitle(translation.Get().SomethingWentWrong()) diff --git a/backend/Bot/Services/DiscordRest.cs b/backend/Bot/Services/DiscordRest.cs index 2ae4f639d..3c5891ff6 100644 --- a/backend/Bot/Services/DiscordRest.cs +++ b/backend/Bot/Services/DiscordRest.cs @@ -192,11 +192,11 @@ public async Task> GetGuildBans(ulong guildId, CacheBehavior cacheBeh .Select(x => x as IBan) .Where(x => x is not null) .ToListAsync(); - _logger.LogInformation(string.Join(", ", bans)); + _logger.LogInformation("{Bans}", string.Join(", ", bans)); } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guild bans for guild '{guildId}' from API."); + _logger.LogError(e, "Failed to fetch guild bans for guild '{GuildId}' from API.", guildId); return FallBackToCache>(cacheKey, cacheBehavior); } @@ -233,7 +233,7 @@ public async Task GetGuildUserBan(ulong guildId, ulong userId, CacheBehavi } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guild ban for guild '{guildId}' and user '{userId}' from API."); + _logger.LogError(e, "Failed to fetch guild ban for guild '{GuildId}' and user '{UserId}' from API.", guildId, userId); return FallBackToCache(cacheKey, cacheBehavior); } @@ -316,7 +316,7 @@ public async Task FetchUserInfo(ulong userId, bool onlyUseCachedUsers) } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch user '{userId}' from API."); + _logger.LogError(e, "Failed to fetch user '{UserId}' from API.", userId); user = FallBackToCache(cacheKey, cachingType); @@ -341,7 +341,7 @@ public IUser FetchMemCachedUserInfo(ulong userId) private async Task IsImageAvailable(string imageUrl) { - _logger.LogInformation($"Fetching {imageUrl} from API for user download."); + _logger.LogInformation("Fetching {ImageUrl} from API for user download.", imageUrl); using var client = new HttpClient(); @@ -375,7 +375,7 @@ public async Task> FetchGuildUsers(ulong guildId, CacheBehavior } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch users for guild '{guildId}' from API."); + _logger.LogError(e, "Failed to fetch users for guild '{GuildId}' from API.", guildId); return FallBackToCache>(cacheKey, cacheBehavior); } @@ -413,7 +413,7 @@ public async Task FetchCurrentUserInfo(string token, CacheBehavior ca } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch current user for token '{token}' from API."); + _logger.LogError(e, "Failed to fetch current user for token '{Token}' from API.", token); return FallBackToCache(cacheKey, cacheBehavior); } @@ -448,7 +448,7 @@ public List FetchGuildChannels(ulong guildId, CacheBehavior cache } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guild channels for guild '{guildId}' from API."); + _logger.LogError(e, "Failed to fetch guild channels for guild '{GuildId}' from API.", guildId); return FallBackToCache>(cacheKey, cacheBehavior); } @@ -480,7 +480,7 @@ public IGuild FetchGuildInfo(ulong guildId, CacheBehavior cacheBehavior) } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guild '{guildId}' from API."); + _logger.LogError(e, "Failed to fetch guild '{GuildId}' from API.", guildId); return FallBackToCache(cacheKey, cacheBehavior); } @@ -515,7 +515,7 @@ public async Task> FetchGuildsOfCurrentUser(string token, CacheB } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guilds of current user for token '{token}' from API."); + _logger.LogError(e, "Failed to fetch guilds of current user for token '{Token}' from API.", token); return FallBackToCache>(cacheKey, cacheBehavior); } @@ -547,7 +547,7 @@ public IGuildUser FetchGuildUserInfo(ulong guildId, ulong userId, CacheBehavior } catch (Exception e) { - _logger.LogError(e, $"Failed to fetch guild '{guildId}' user '{userId}' from API."); + _logger.LogError(e, "Failed to fetch guild '{GuildId}' user '{UserId}' from API.", guildId, userId); return FallBackToCache(cacheKey, cacheBehavior); } @@ -572,7 +572,7 @@ public async Task BanUser(ulong guildId, ulong userId, string reason = nul } catch (Exception e) { - _logger.LogError(e, $"Failed to ban user '{userId}' from guild '{guildId}'."); + _logger.LogError(e, "Failed to ban user '{UserId}' from guild '{GuildId}'.", userId, guildId); return false; } @@ -594,7 +594,7 @@ public async Task UnBanUser(ulong guildId, ulong userId, string reason = n } catch (Exception e) { - _logger.LogError(e, $"Failed to unban user '{userId}' from guild '{guildId}'."); + _logger.LogError(e, "Failed to unban user '{UserId}' from guild '{GuildId}'.", userId, guildId); return false; } @@ -621,7 +621,7 @@ public async Task TimeoutGuildUser(ulong guildId, ulong userId, TimeSpan t } catch (Exception e) { - _logger.LogError(e, $"Failed to grant user '{userId}' from guild '{guildId}' timeout '{timeOutDuration}'."); + _logger.LogError(e, "Failed to grant user '{UserId}' from guild '{GuildId}' timeout '{TimeOutDuration}'.", userId, guildId, timeOutDuration); return false; } @@ -647,7 +647,7 @@ public async Task RemoveTimeoutFromGuildUser(ulong guildId, ulong userId, } catch (Exception e) { - _logger.LogError(e, $"Failed to revoke user '{userId}' from guild '{guildId}' timeout."); + _logger.LogError(e, "Failed to revoke user '{UserId}' from guild '{GuildId}' timeout.", userId, guildId); return false; } @@ -672,7 +672,7 @@ public async Task KickGuildUser(ulong guildId, ulong userId, string reason } catch (Exception e) { - _logger.LogError(e, $"Failed to kick user '{userId}' from guild '{guildId}'."); + _logger.LogError(e, "Failed to kick user '{UserId}' from guild '{GuildId}'.", userId, guildId); return false; } @@ -702,7 +702,7 @@ public async Task CreateDmChannel(ulong userId) } catch (Exception e) { - _logger.LogError(e, $"Failed to create dm with user '{userId}'."); + _logger.LogError(e, "Failed to create dm with user '{UserId}'.", userId); return FallBackToCache(cacheKey, CacheBehavior.Default); } diff --git a/backend/Levels/Commands/UpdateRoles.cs b/backend/Levels/Commands/UpdateRoles.cs index 01d55559c..4e090881e 100644 --- a/backend/Levels/Commands/UpdateRoles.cs +++ b/backend/Levels/Commands/UpdateRoles.cs @@ -6,6 +6,7 @@ using Levels.Data; using Levels.Models; using Levels.Services; +using Microsoft.Extensions.Logging; using Color = Discord.Color; namespace Levels.Commands; @@ -25,7 +26,7 @@ public override async Task BeforeCommandExecute() => [SlashCommand("updateroles", "Update a user's roles to match their level.", runMode: RunMode.Async)] public async Task RankCommand( [Summary("user", "User to update roles for. Defaults to oneself.")] - IGuildUser user = null + IGuildUser user = null ) { user ??= Context.Guild.GetUser(Context.User.Id); @@ -44,6 +45,9 @@ public async Task RankCommand( var result = await LevelingService.HandleLevelRoles(level, totalLevel, user, GuildLevelConfigRepository); + Logger.LogInformation("{RunAgent} used the update roles command on {Affected} in {GuildName} ({GuildId})", + Context.User.Username, user.Username, Context.Guild.Name, Context.Guild.Id); + var embed = new EmbedBuilder() .WithTitle("Role update") .WithCurrentTimestamp(); diff --git a/backend/Music/Commands/Play.cs b/backend/Music/Commands/Play.cs index f3b619859..ea958468a 100644 --- a/backend/Music/Commands/Play.cs +++ b/backend/Music/Commands/Play.cs @@ -4,7 +4,6 @@ using Discord.Interactions; using Discord.WebSocket; using Lavalink4NET.Players; -using Lavalink4NET.Rest.Entities.Tracks; using Lavalink4NET.Tracks; using Music.Abstractions; using Music.Enums; diff --git a/backend/Music/Music.csproj b/backend/Music/Music.csproj index 42f10e4d3..aa01d4b11 100644 --- a/backend/Music/Music.csproj +++ b/backend/Music/Music.csproj @@ -8,7 +8,7 @@ - +