Move creds.yml to data/creds.yml, update versions from v5 to v6 everywhere

This commit is contained in:
Toastie 2025-02-11 12:20:21 +13:00
parent 0ea185f769
commit acdb707a31
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
6 changed files with 34 additions and 62 deletions
.gitignore
src/EllieBot
Bot.cs
Modules
Administration/Self
Searches/_common/StreamNotifications/Providers
Program.cs
_common/Impl

8
.gitignore vendored
View file

@ -7,18 +7,12 @@ src/EllieBot/data/marmalades/
# other # other
command_errors*.txt
output/ output/
src/EllieBot/output src/EllieBot/output
src/EllieBot/creds.yml src/EllieBot/creds.yml
src/EllieBot/data/creds.yml
src/EllieBot/Command Errors*.txt 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 src/EllieBot/data/EllieBot.db
# scripts # scripts
ellie-menu.ps1 ellie-menu.ps1

View file

@ -17,7 +17,7 @@ public sealed class Bot : IBot
private IContainer Services { get; set; } private IContainer Services { get; set; }
public bool IsReady { get; private set; } public bool IsReady { get; private set; }
public int ShardId { get; set; } public int ShardId { get; }
private readonly IBotCreds _creds; private readonly IBotCreds _creds;
private readonly CommandService _commandService; private readonly CommandService _commandService;
@ -28,12 +28,12 @@ public sealed class Bot : IBot
private readonly Assembly[] _loadedAssemblies; private readonly Assembly[] _loadedAssemblies;
// private readonly InteractionService _interactionService; // private readonly InteractionService _interactionService;
public Bot(int shardId, int? totalShards, string credPath = null) public Bot(int shardId, int? totalShards)
{ {
ArgumentOutOfRangeException.ThrowIfLessThan(shardId, 0); ArgumentOutOfRangeException.ThrowIfLessThan(shardId, 0);
ShardId = shardId; ShardId = shardId;
_credsProvider = new BotCredsProvider(totalShards, credPath); _credsProvider = new BotCredsProvider(totalShards);
_creds = _credsProvider.GetCreds(); _creds = _credsProvider.GetCreds();
LogSetup.SetupLogger(shardId, _creds); LogSetup.SetupLogger(shardId, _creds);
@ -286,7 +286,7 @@ public sealed class Bot : IBot
Log.Information("Initializing Owner Id..."); Log.Information("Initializing Owner Id...");
var info = await Client.GetApplicationInfoAsync(); 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) catch (Exception ex)
{ {

View file

@ -10,47 +10,33 @@ public sealed class ToastielabReleaseModel
[JsonPropertyName("tag_name")] [JsonPropertyName("tag_name")]
public required string TagName { get; init; } 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"; 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() public async Task OnReadyAsync()
{ {
if (_client.ShardId != 0) if (client.ShardId != 0)
return; return;
using var timer = new PeriodicTimer(TimeSpan.FromHours(1)); using var timer = new PeriodicTimer(TimeSpan.FromHours(1));
while (await timer.WaitForNextTickAsync()) while (await timer.WaitForNextTickAsync())
{ {
var conf = _bcs.Data; var conf = bcs.Data;
if (!conf.CheckForUpdates) if (!conf.CheckForUpdates)
continue; continue;
try try
{ {
using var http = _httpFactory.CreateClient(); using var http = httpFactory.CreateClient();
var toastielabRelease = (await http.GetFromJsonAsync<ToastielabReleaseModel[]>(RELEASES_URL)) var toastielabRelease = (await http.GetFromJsonAsync<ToastielabReleaseModel[]>(RELEASES_URL))
?.FirstOrDefault(); ?.FirstOrDefault();
@ -72,7 +58,7 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor
UpdateLastKnownVersion(latestVersion); UpdateLastKnownVersion(latestVersion);
// pull changelog // 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); var thisVersionChangelog = GetVersionChangelog(latestVersion, changelog);
@ -83,24 +69,24 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor
continue; continue;
} }
var creds = _bcp.GetCreds(); var creds = bcp.GetCreds();
await creds.OwnerIds await creds.OwnerIds
.Select(async x => .Select(async x =>
{ {
var user = await _client.GetUserAsync(x); var user = await client.GetUserAsync(x);
if (user is null) if (user is null)
return; return;
var eb = _sender.CreateEmbed() var eb = sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.WithAuthor($"EllieBot v{latest} Released!") .WithAuthor($"EllieBot v{latest} Released!")
.WithTitle("Changelog") .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)) .WithDescription(thisVersionChangelog.TrimTo(4096))
.WithFooter( .WithFooter(
"You may disable these messages by typing '.conf bot checkforupdates false'"); "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(); .WhenAll();
} }
@ -152,7 +138,7 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor
private const string LAST_KNOWN_VERSION_PATH = "data/last_known_version.txt"; 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)) if (!File.Exists(LAST_KNOWN_VERSION_PATH))
return null; return null;
@ -162,8 +148,8 @@ public sealed class CheckForUpdatesService : IEService, IReadyExecutor
: null; : 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());
} }
} }

View file

@ -124,7 +124,7 @@ public sealed class TwitchHelixProvider : Provider
if (token is null) 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>(); return Array.Empty<StreamData>();
} }

View file

@ -21,4 +21,4 @@ if (args.Length > 0 && args[0] != "run")
} }
await new Bot(shardId, totalShards, Environment.GetEnvironmentVariable("EllieBot__creds")).RunAndBlockAsync(); await new Bot(shardId, totalShards).RunAndBlockAsync();

View file

@ -6,8 +6,8 @@ namespace EllieBot.Services;
public sealed class BotCredsProvider : IBotCredsProvider public sealed class BotCredsProvider : IBotCredsProvider
{ {
private const string CREDS_FILE_NAME = "creds.yml"; private const string CREDS_FILE_NAME = "data/creds.yml";
private const string CREDS_EXAMPLE_FILE_NAME = "creds_example.yml"; private const string CREDS_EXAMPLE_FILE_NAME = "data/creds_example.yml";
private string CredsPath { get; } private string CredsPath { get; }
@ -22,20 +22,12 @@ public sealed class BotCredsProvider : IBotCredsProvider
private readonly object _reloadLock = new(); private readonly object _reloadLock = new();
public BotCredsProvider(int? totalShards = null, string credPath = null) public BotCredsProvider(int? totalShards = null)
{ {
_totalShards = totalShards; _totalShards = totalShards;
if (!string.IsNullOrWhiteSpace(credPath)) CredsPath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_FILE_NAME);
{ CredsExamplePath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_EXAMPLE_FILE_NAME);
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);
}
try try
{ {
@ -61,8 +53,8 @@ public sealed class BotCredsProvider : IBotCredsProvider
} }
_config = new ConfigurationBuilder().AddYamlFile(CredsPath, false, true) _config = new ConfigurationBuilder().AddYamlFile(CredsPath, false, true)
.AddEnvironmentVariables("EllieBot_") .AddEnvironmentVariables("EllieBot_")
.Build(); .Build();
} }
catch (Exception ex) catch (Exception ex)
{ {