From acdb707a3175cfe9cd9ca01c5587afd3f483c432 Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Tue, 11 Feb 2025 12:20:21 +1300 Subject: [PATCH] Move creds.yml to data/creds.yml, update versions from v5 to v6 everywhere --- .gitignore | 8 +-- src/EllieBot/Bot.cs | 8 +-- .../Self/CheckForUpdatesService.cs | 54 +++++++------------ .../Providers/TwitchHelixProvider.cs | 2 +- src/EllieBot/Program.cs | 2 +- src/EllieBot/_common/Impl/BotCredsProvider.cs | 22 +++----- 6 files changed, 34 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index eda3552..05687b8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,18 +7,12 @@ src/EllieBot/data/marmalades/ # other -command_errors*.txt output/ src/EllieBot/output src/EllieBot/creds.yml - +src/EllieBot/data/creds.yml src/EllieBot/Command Errors*.txt -src/EllieBot/creds.yml -# credentials file before and after v3 -src/EllieBot/credentials.json -src/EllieBot/old_credentials.json -src/EllieBot/credentials.json.bak src/EllieBot/data/EllieBot.db # scripts ellie-menu.ps1 diff --git a/src/EllieBot/Bot.cs b/src/EllieBot/Bot.cs index c9b7f25..38b08ac 100644 --- a/src/EllieBot/Bot.cs +++ b/src/EllieBot/Bot.cs @@ -17,7 +17,7 @@ public sealed class Bot : IBot private IContainer Services { get; set; } public bool IsReady { get; private set; } - public int ShardId { get; set; } + public int ShardId { get; } private readonly IBotCreds _creds; private readonly CommandService _commandService; @@ -28,12 +28,12 @@ public sealed class Bot : IBot private readonly Assembly[] _loadedAssemblies; // private readonly InteractionService _interactionService; - public Bot(int shardId, int? totalShards, string credPath = null) + public Bot(int shardId, int? totalShards) { ArgumentOutOfRangeException.ThrowIfLessThan(shardId, 0); ShardId = shardId; - _credsProvider = new BotCredsProvider(totalShards, credPath); + _credsProvider = new BotCredsProvider(totalShards); _creds = _credsProvider.GetCreds(); LogSetup.SetupLogger(shardId, _creds); @@ -286,7 +286,7 @@ public sealed class Bot : IBot Log.Information("Initializing Owner Id..."); var info = await Client.GetApplicationInfoAsync(); - _credsProvider.ModifyCredsFile(x => x.OwnerIds = new[] { info.Owner.Id }); + _credsProvider.ModifyCredsFile(x => x.OwnerIds = [info.Owner.Id]); } catch (Exception ex) { diff --git a/src/EllieBot/Modules/Administration/Self/CheckForUpdatesService.cs b/src/EllieBot/Modules/Administration/Self/CheckForUpdatesService.cs index 699140d..211d332 100644 --- a/src/EllieBot/Modules/Administration/Self/CheckForUpdatesService.cs +++ b/src/EllieBot/Modules/Administration/Self/CheckForUpdatesService.cs @@ -10,47 +10,33 @@ public sealed class ToastielabReleaseModel [JsonPropertyName("tag_name")] public required string TagName { get; init; } } -public sealed class CheckForUpdatesService : IEService, IReadyExecutor + +public sealed class CheckForUpdatesService( + BotConfigService bcs, + IBotCredsProvider bcp, + IHttpClientFactory httpFactory, + DiscordSocketClient client, + IMessageSenderService sender) + : IEService, IReadyExecutor { - private readonly BotConfigService _bcs; - private readonly IBotCredsProvider _bcp; - private readonly IHttpClientFactory _httpFactory; - private readonly DiscordSocketClient _client; - private readonly IMessageSenderService _sender; - - private const string RELEASES_URL = "https://toastielab.dev/api/v1/repos/EllieBotDevs/elliebot/releases"; - public CheckForUpdatesService( - BotConfigService bcs, - IBotCredsProvider bcp, - IHttpClientFactory httpFactory, - DiscordSocketClient client, - IMessageSenderService sender) - { - _bcs = bcs; - _bcp = bcp; - _httpFactory = httpFactory; - _client = client; - _sender = sender; - } - public async Task OnReadyAsync() { - if (_client.ShardId != 0) + if (client.ShardId != 0) return; using var timer = new PeriodicTimer(TimeSpan.FromHours(1)); while (await timer.WaitForNextTickAsync()) { - var conf = _bcs.Data; + var conf = bcs.Data; if (!conf.CheckForUpdates) continue; try { - using var http = _httpFactory.CreateClient(); + using var http = httpFactory.CreateClient(); var toastielabRelease = (await http.GetFromJsonAsync<ToastielabReleaseModel[]>(RELEASES_URL)) ?.FirstOrDefault(); @@ -72,7 +58,7 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor UpdateLastKnownVersion(latestVersion); // pull changelog - var changelog = await http.GetStringAsync("https://toastielab.dev/EllieBotDevs/elliebot/raw/branch/v5/CHANGELOG.md"); + var changelog = await http.GetStringAsync("https://toastielab.dev/EllieBotDevs/elliebot/raw/branch/v6/CHANGELOG.md"); var thisVersionChangelog = GetVersionChangelog(latestVersion, changelog); @@ -83,24 +69,24 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor continue; } - var creds = _bcp.GetCreds(); + var creds = bcp.GetCreds(); await creds.OwnerIds .Select(async x => { - var user = await _client.GetUserAsync(x); + var user = await client.GetUserAsync(x); if (user is null) return; - var eb = _sender.CreateEmbed() + var eb = sender.CreateEmbed() .WithOkColor() .WithAuthor($"EllieBot v{latest} Released!") .WithTitle("Changelog") - .WithUrl("https://toastielab.dev/EllieBotDevs/elliebot/src/branch/v5/CHANGELOG.md") + .WithUrl("https://toastielab.dev/EllieBotDevs/elliebot/src/branch/v6/CHANGELOG.md") .WithDescription(thisVersionChangelog.TrimTo(4096)) .WithFooter( "You may disable these messages by typing '.conf bot checkforupdates false'"); - await _sender.Response(user).Embed(eb).SendAsync(); + await sender.Response(user).Embed(eb).SendAsync(); }) .WhenAll(); } @@ -152,7 +138,7 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor private const string LAST_KNOWN_VERSION_PATH = "data/last_known_version.txt"; - private Version? GetLastKnownVersion() + private static Version? GetLastKnownVersion() { if (!File.Exists(LAST_KNOWN_VERSION_PATH)) return null; @@ -162,8 +148,8 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor : null; } - private void UpdateLastKnownVersion(Version version) + private static void UpdateLastKnownVersion(Version version) { - File.WriteAllText("data/last_known_version.txt", version.ToString()); + File.WriteAllText(LAST_KNOWN_VERSION_PATH, version.ToString()); } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Searches/_common/StreamNotifications/Providers/TwitchHelixProvider.cs b/src/EllieBot/Modules/Searches/_common/StreamNotifications/Providers/TwitchHelixProvider.cs index f369110..d63abea 100644 --- a/src/EllieBot/Modules/Searches/_common/StreamNotifications/Providers/TwitchHelixProvider.cs +++ b/src/EllieBot/Modules/Searches/_common/StreamNotifications/Providers/TwitchHelixProvider.cs @@ -124,7 +124,7 @@ public sealed class TwitchHelixProvider : Provider if (token is null) { - Log.Warning("Twitch client id and client secret key are not added to creds.yml or incorrect"); + Log.Warning("Twitch client id and client secret key are not added to data/creds.yml or incorrect"); return Array.Empty<StreamData>(); } diff --git a/src/EllieBot/Program.cs b/src/EllieBot/Program.cs index 5a74a96..f0e553b 100644 --- a/src/EllieBot/Program.cs +++ b/src/EllieBot/Program.cs @@ -21,4 +21,4 @@ if (args.Length > 0 && args[0] != "run") } -await new Bot(shardId, totalShards, Environment.GetEnvironmentVariable("EllieBot__creds")).RunAndBlockAsync(); \ No newline at end of file +await new Bot(shardId, totalShards).RunAndBlockAsync(); \ No newline at end of file diff --git a/src/EllieBot/_common/Impl/BotCredsProvider.cs b/src/EllieBot/_common/Impl/BotCredsProvider.cs index d883522..ad94a38 100644 --- a/src/EllieBot/_common/Impl/BotCredsProvider.cs +++ b/src/EllieBot/_common/Impl/BotCredsProvider.cs @@ -6,8 +6,8 @@ namespace EllieBot.Services; public sealed class BotCredsProvider : IBotCredsProvider { - private const string CREDS_FILE_NAME = "creds.yml"; - private const string CREDS_EXAMPLE_FILE_NAME = "creds_example.yml"; + private const string CREDS_FILE_NAME = "data/creds.yml"; + private const string CREDS_EXAMPLE_FILE_NAME = "data/creds_example.yml"; private string CredsPath { get; } @@ -22,20 +22,12 @@ public sealed class BotCredsProvider : IBotCredsProvider private readonly object _reloadLock = new(); - public BotCredsProvider(int? totalShards = null, string credPath = null) + public BotCredsProvider(int? totalShards = null) { _totalShards = totalShards; - if (!string.IsNullOrWhiteSpace(credPath)) - { - CredsPath = credPath; - CredsExamplePath = Path.Combine(Path.GetDirectoryName(credPath), CREDS_EXAMPLE_FILE_NAME); - } - else - { - CredsPath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_FILE_NAME); - CredsExamplePath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_EXAMPLE_FILE_NAME); - } + CredsPath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_FILE_NAME); + CredsExamplePath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_EXAMPLE_FILE_NAME); try { @@ -61,8 +53,8 @@ public sealed class BotCredsProvider : IBotCredsProvider } _config = new ConfigurationBuilder().AddYamlFile(CredsPath, false, true) - .AddEnvironmentVariables("EllieBot_") - .Build(); + .AddEnvironmentVariables("EllieBot_") + .Build(); } catch (Exception ex) {