1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-19 16:33:36 +03:00

style: move static fields from Program to Utility

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-05-16 18:35:51 +05:00
parent 27431c72cf
commit 9c86859cdf
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
7 changed files with 22 additions and 17 deletions

View file

@ -23,18 +23,23 @@ public static class ResultExtensions
private static void LogResultStackTrace(Result result) private static void LogResultStackTrace(Result result)
{ {
if (Program.StaticLogger is null || result.IsSuccess) if (result.IsSuccess)
{ {
return; return;
} }
Program.StaticLogger.LogError("{ErrorType}: {ErrorMessage}{NewLine}{StackTrace}", if (Utility.StaticLogger is null)
{
throw new InvalidOperationException();
}
Utility.StaticLogger.LogError("{ErrorType}: {ErrorMessage}{NewLine}{StackTrace}",
result.Error.GetType().FullName, result.Error.Message, Environment.NewLine, ConstructStackTrace()); result.Error.GetType().FullName, result.Error.Message, Environment.NewLine, ConstructStackTrace());
var inner = result.Inner; var inner = result.Inner;
while (inner is { IsSuccess: false }) while (inner is { IsSuccess: false })
{ {
Program.StaticLogger.LogError("Caused by: {ResultType}: {ResultMessage}", Utility.StaticLogger.LogError("Caused by: {ResultType}: {ResultMessage}",
inner.Error.GetType().FullName, inner.Error.Message); inner.Error.GetType().FullName, inner.Error.Message);
inner = inner.Inner; inner = inner.Inner;

View file

@ -4,7 +4,6 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Remora.Discord.API.Abstractions.Gateway.Commands; using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Objects;
using Remora.Discord.Caching.Extensions; using Remora.Discord.Caching.Extensions;
using Remora.Discord.Caching.Services; using Remora.Discord.Caching.Services;
using Remora.Discord.Commands.Extensions; using Remora.Discord.Commands.Extensions;
@ -12,9 +11,7 @@ using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Extensions; using Remora.Discord.Extensions.Extensions;
using Remora.Discord.Gateway; using Remora.Discord.Gateway;
using Remora.Discord.Hosting.Extensions; using Remora.Discord.Hosting.Extensions;
using Remora.Rest.Core;
using Serilog.Extensions.Logging; using Serilog.Extensions.Logging;
using TeamOctolings.Octobot.Attributes;
using TeamOctolings.Octobot.Commands.Events; using TeamOctolings.Octobot.Commands.Events;
using TeamOctolings.Octobot.Services; using TeamOctolings.Octobot.Services;
using TeamOctolings.Octobot.Services.Update; using TeamOctolings.Octobot.Services.Update;
@ -23,17 +20,11 @@ namespace TeamOctolings.Octobot;
public sealed class Program public sealed class Program
{ {
public static readonly AllowedMentions NoMentions = new(
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
[StaticCallersOnly]
public static ILogger<Program>? StaticLogger { get; private set; }
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
var host = CreateHostBuilder(args).UseConsoleLifetime().Build(); var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
var services = host.Services; var services = host.Services;
StaticLogger = services.GetRequiredService<ILogger<Program>>(); Utility.StaticLogger = services.GetRequiredService<ILogger<Program>>();
var slashService = services.GetRequiredService<SlashService>(); var slashService = services.GetRequiredService<SlashService>();
// Providing a guild ID to this call will result in command duplicates! // Providing a guild ID to this call will result in command duplicates!

View file

@ -77,7 +77,7 @@ public class GuildMemberJoinedResponder : IResponder<IGuildMemberAdd>
return await _channelApi.CreateMessageWithEmbedResultAsync( return await _channelApi.CreateMessageWithEmbedResultAsync(
GuildSettings.WelcomeMessagesChannel.Get(cfg), embedResult: embed, GuildSettings.WelcomeMessagesChannel.Get(cfg), embedResult: embed,
allowedMentions: Program.NoMentions, ct: ct); allowedMentions: Utility.NoMentions, ct: ct);
} }
private async Task<Result> TryReturnRolesAsync( private async Task<Result> TryReturnRolesAsync(

View file

@ -67,6 +67,6 @@ public class GuildMemberLeftResponder : IResponder<IGuildMemberRemove>
return await _channelApi.CreateMessageWithEmbedResultAsync( return await _channelApi.CreateMessageWithEmbedResultAsync(
GuildSettings.WelcomeMessagesChannel.Get(cfg), embedResult: embed, GuildSettings.WelcomeMessagesChannel.Get(cfg), embedResult: embed,
allowedMentions: Program.NoMentions, ct: ct); allowedMentions: Utility.NoMentions, ct: ct);
} }
} }

View file

@ -102,6 +102,6 @@ public class MessageDeletedResponder : IResponder<IMessageDelete>
return await _channelApi.CreateMessageWithEmbedResultAsync( return await _channelApi.CreateMessageWithEmbedResultAsync(
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed,
allowedMentions: Program.NoMentions, ct: ct); allowedMentions: Utility.NoMentions, ct: ct);
} }
} }

View file

@ -104,6 +104,6 @@ public class MessageEditedResponder : IResponder<IMessageUpdate>
return await _channelApi.CreateMessageWithEmbedResultAsync( return await _channelApi.CreateMessageWithEmbedResultAsync(
GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed, GuildSettings.PrivateFeedbackChannel.Get(cfg), embedResult: embed,
allowedMentions: Program.NoMentions, ct: ct); allowedMentions: Utility.NoMentions, ct: ct);
} }
} }

View file

@ -1,12 +1,15 @@
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Embeds;
using Remora.Discord.Extensions.Formatting; using Remora.Discord.Extensions.Formatting;
using Remora.Rest.Core; using Remora.Rest.Core;
using Remora.Results; using Remora.Results;
using TeamOctolings.Octobot.Attributes;
using TeamOctolings.Octobot.Data; using TeamOctolings.Octobot.Data;
using TeamOctolings.Octobot.Extensions; using TeamOctolings.Octobot.Extensions;
@ -18,6 +21,9 @@ namespace TeamOctolings.Octobot;
/// </summary> /// </summary>
public sealed class Utility public sealed class Utility
{ {
public static readonly AllowedMentions NoMentions = new(
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
private readonly IDiscordRestChannelAPI _channelApi; private readonly IDiscordRestChannelAPI _channelApi;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi; private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly IDiscordRestGuildAPI _guildApi; private readonly IDiscordRestGuildAPI _guildApi;
@ -30,6 +36,9 @@ public sealed class Utility
_guildApi = guildApi; _guildApi = guildApi;
} }
[StaticCallersOnly]
public static ILogger<Program>? StaticLogger { get; set; }
/// <summary> /// <summary>
/// Gets the string mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event subscribers related to /// Gets the string mentioning the <see cref="GuildSettings.EventNotificationRole" /> and event subscribers related to
/// a scheduled /// a scheduled