diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e7ee68e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +build/ +**/bin/ +**/obj/ diff --git a/src/NosCoreBot/NosCoreBot.csproj b/src/NosCoreBot/NosCoreBot.csproj index d9fae26..81ac321 100644 --- a/src/NosCoreBot/NosCoreBot.csproj +++ b/src/NosCoreBot/NosCoreBot.csproj @@ -10,17 +10,17 @@ - + - + - - - - - - - + + + + + + + - \ No newline at end of file + diff --git a/src/NosCoreBot/Program.cs b/src/NosCoreBot/Program.cs index 0dbe2f4..9e89960 100644 --- a/src/NosCoreBot/Program.cs +++ b/src/NosCoreBot/Program.cs @@ -26,10 +26,11 @@ public static IHostBuilder CreateHostBuilder(string[] args) .UseWindowsService() .ConfigureServices((hostContext, services) => { + var runOnce = Environment.GetEnvironmentVariable("RUN_ONCE") == "true"; services.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig { - AlwaysDownloadUsers = true, - GatewayIntents = GatewayIntents.All + AlwaysDownloadUsers = !runOnce, + GatewayIntents = runOnce ? GatewayIntents.Guilds : GatewayIntents.All })) .AddHttpClient() .AddTransient() diff --git a/src/NosCoreBot/Services/TimeHandlingService.cs b/src/NosCoreBot/Services/TimeHandlingService.cs index ea61032..29c4c07 100644 --- a/src/NosCoreBot/Services/TimeHandlingService.cs +++ b/src/NosCoreBot/Services/TimeHandlingService.cs @@ -159,7 +159,7 @@ await Task.WhenAll(fileslist.Select(file => if (_discord.GetChannel(719772084968095775) is SocketTextChannel channel) { var file = new FileInfo(archiveName); - if (file.Length > 8388119) + if ((ulong)file.Length > channel.Guild.MaxUploadLimit) { var send = await channel.SendMessageAsync($"<:altz:699420721088168036><:altz:699420721088168036><:altz:699420721088168036>Parser Too Heavy<:altz:699420721088168036><:altz:699420721088168036><:altz:699420721088168036>\n - Size : {file.Length}"); } diff --git a/src/NosCoreBot/Worker.cs b/src/NosCoreBot/Worker.cs index 52b0aaa..8fb9c41 100644 --- a/src/NosCoreBot/Worker.cs +++ b/src/NosCoreBot/Worker.cs @@ -21,14 +21,16 @@ public class Worker : BackgroundService private readonly CommandService _cmservice; private readonly CommandHandlingService _chservice; private readonly TimeHandlingService _thservice; + private readonly IHostApplicationLifetime _lifetime; public Worker(DiscordSocketClient client, CommandService cmservice, CommandHandlingService chservice, - TimeHandlingService thservice) + TimeHandlingService thservice, IHostApplicationLifetime lifetime) { _client = client; _cmservice = cmservice; _chservice = chservice; _thservice = thservice; + _lifetime = lifetime; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -36,14 +38,36 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) _client.Log += LogAsync; _cmservice.Log += LogAsync; + var ready = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _client.Ready += () => + { + ready.TrySetResult(); + return Task.CompletedTask; + }; + // Tokens should be considered secret data, and never hard-coded. await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); await _client.StartAsync(); + await ready.Task.WaitAsync(TimeSpan.FromMinutes(2), stoppingToken); + + if (Environment.GetEnvironmentVariable("RUN_ONCE") == "true") + { + try + { + await _thservice.UploadInputFilesAsync(); + } + finally + { + await _client.StopAsync(); + _lifetime.StopApplication(); + } + return; + } await _chservice.InitializeAsync(); await _thservice.UploadInputFilesAsync(); - await Task.Delay(-1); + await Task.Delay(-1, stoppingToken); } private Task LogAsync(LogMessage log) diff --git a/src/NosCoreBot/dockerfile b/src/NosCoreBot/dockerfile index b95fefd..218f3b0 100644 --- a/src/NosCoreBot/dockerfile +++ b/src/NosCoreBot/dockerfile @@ -1,7 +1,9 @@ -FROM mcr.microsoft.com/dotnet/aspnet:7.0 -WORKDIR /dotnetapp - -COPY . /app -WORKDIR /app/build/net7.0 +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build +WORKDIR /src +COPY . . +RUN dotnet publish src/NosCoreBot/NosCoreBot.csproj -c Release -o /app/publish +FROM mcr.microsoft.com/dotnet/aspnet:10.0 +WORKDIR /app +COPY --from=build /app/publish . ENTRYPOINT ["dotnet", "NosCoreBot.dll"] diff --git a/tests/NosCoreBot.Tests/NosCoreBot.Tests.csproj b/tests/NosCoreBot.Tests/NosCoreBot.Tests.csproj index b2e57e1..502eab1 100644 --- a/tests/NosCoreBot.Tests/NosCoreBot.Tests.csproj +++ b/tests/NosCoreBot.Tests/NosCoreBot.Tests.csproj @@ -7,9 +7,9 @@ - - - + + +