From fe8e383f3dcbf4770a0058e031e109891de340c9 Mon Sep 17 00:00:00 2001 From: TehGM Date: Tue, 2 Nov 2021 22:43:30 +0000 Subject: [PATCH 1/6] Configurable words list expiration --- LukeDictionary.Bot/Commands/LukeWordCommands.cs | 7 +++++-- LukeDictionary.Bot/Program.cs | 2 ++ LukeDictionary.Bot/Words/LukeWordsOptions.cs | 10 ++++++++++ LukeDictionary.Bot/appsettings.Development.json | 3 +++ LukeDictionary.Bot/appsettings.json | 3 +++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 LukeDictionary.Bot/Words/LukeWordsOptions.cs diff --git a/LukeDictionary.Bot/Commands/LukeWordCommands.cs b/LukeDictionary.Bot/Commands/LukeWordCommands.cs index f8f87c7..55856f9 100644 --- a/LukeDictionary.Bot/Commands/LukeWordCommands.cs +++ b/LukeDictionary.Bot/Commands/LukeWordCommands.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using DevSubmarine.LukeDictionary.Discord; using DevSubmarine.LukeDictionary.PasteMyst; +using DevSubmarine.LukeDictionary.Words; using DSharpPlus; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; @@ -25,16 +26,18 @@ public class LukeWordCommands private readonly ILukeWordsStore _store; private readonly ILogger _log; private readonly IOptionsMonitor _devsubOptions; + private readonly IOptionsMonitor _wordsOptions; private readonly IPasteMystClient _pasteMyst; private readonly Regex _inputValidationRegex; public LukeWordCommands(DiscordClient client, ILukeWordsStore store, ILogger log, - IOptionsMonitor devsubOptions, IPasteMystClient pasteMyst) + IOptionsMonitor devsubOptions, IOptionsMonitor wordsOptions, IPasteMystClient pasteMyst) { this._client = client; this._store = store; this._log = log; this._devsubOptions = devsubOptions; + this._wordsOptions = wordsOptions; this._pasteMyst = pasteMyst; this._inputValidationRegex = new Regex(@"^[A-Za-z0-9]{1,25}$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled, @@ -107,7 +110,7 @@ public async Task GetWordsListAsync(CancellationToken cancellationToken { new Pasty(contentPlain, $"{title} (Plain Text)", PastyLanguages.PlainText), new Pasty(contentJson, $"{title} (JSON)", PastyLanguages.JSON) - }); + }, this._wordsOptions.CurrentValue.ListExpiration); paste = await this._pasteMyst.CreatePasteAsync(paste, cancellationToken).ConfigureAwait(false); return $"https://paste.myst.rs/{paste.ID}"; } diff --git a/LukeDictionary.Bot/Program.cs b/LukeDictionary.Bot/Program.cs index 89ed15f..2ac600c 100644 --- a/LukeDictionary.Bot/Program.cs +++ b/LukeDictionary.Bot/Program.cs @@ -3,6 +3,7 @@ using DevSubmarine.LukeDictionary.Database; using DevSubmarine.LukeDictionary.Discord; using DevSubmarine.LukeDictionary.PasteMyst; +using DevSubmarine.LukeDictionary.Words; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -44,6 +45,7 @@ static Task Main(string[] args) services.Configure(context.Configuration.GetSection("Database")); services.Configure(context.Configuration); services.Configure(context.Configuration.GetSection("PasteMyst")); + services.Configure(context.Configuration.GetSection("Words")); // add services services.AddDiscord(); diff --git a/LukeDictionary.Bot/Words/LukeWordsOptions.cs b/LukeDictionary.Bot/Words/LukeWordsOptions.cs new file mode 100644 index 0000000..d203c54 --- /dev/null +++ b/LukeDictionary.Bot/Words/LukeWordsOptions.cs @@ -0,0 +1,10 @@ +using DevSubmarine.LukeDictionary.PasteMyst; + +namespace DevSubmarine.LukeDictionary.Words +{ + public class LukeWordsOptions + { + /// How long the words list paste will be alive. + public PasteExpiration ListExpiration { get; set; } = PasteExpiration.OneMonth; + } +} diff --git a/LukeDictionary.Bot/appsettings.Development.json b/LukeDictionary.Bot/appsettings.Development.json index f61179f..c19908e 100644 --- a/LukeDictionary.Bot/appsettings.Development.json +++ b/LukeDictionary.Bot/appsettings.Development.json @@ -1,6 +1,9 @@ { "GuildID": 757959067812495480, + "Words": { + "ListExpiration": "OneWeek" + }, "Logging": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { diff --git a/LukeDictionary.Bot/appsettings.json b/LukeDictionary.Bot/appsettings.json index 3c97264..0d1d797 100644 --- a/LukeDictionary.Bot/appsettings.json +++ b/LukeDictionary.Bot/appsettings.json @@ -1,4 +1,7 @@ { + "Words": { + "ListExpiration": "OneMonth" + }, "Logging": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { From e7d45a09b16a9ef622bed2ab828ebbd41837e231 Mon Sep 17 00:00:00 2001 From: TehGM Date: Tue, 2 Nov 2021 22:43:44 +0000 Subject: [PATCH 2/6] Version bump --- LukeDictionary.Bot/LukeDictionary.Bot.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LukeDictionary.Bot/LukeDictionary.Bot.csproj b/LukeDictionary.Bot/LukeDictionary.Bot.csproj index d8706d3..7e76c9d 100644 --- a/LukeDictionary.Bot/LukeDictionary.Bot.csproj +++ b/LukeDictionary.Bot/LukeDictionary.Bot.csproj @@ -8,7 +8,7 @@ DevSubmarine,TehGM DevSubmarine LukeDictionary Discord Bot - 1.0.1 + 1.0.2 Apache-2.0 https://github.com/DevSubmarine/LukeDictionary https://github.com/DevSubmarine/LukeDictionary From 9510d337965f38703d608edb13a3a997d19a78a2 Mon Sep 17 00:00:00 2001 From: TehGM Date: Fri, 24 Dec 2021 23:03:12 +0100 Subject: [PATCH 3/6] Dockerfile fix --- LukeDictionary.Bot/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LukeDictionary.Bot/Dockerfile b/LukeDictionary.Bot/Dockerfile index f168a1e..3ba31e2 100644 --- a/LukeDictionary.Bot/Dockerfile +++ b/LukeDictionary.Bot/Dockerfile @@ -1,3 +1,4 @@ -FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base +FROM mcr.microsoft.com/dotnet/runtime:5.0 +WORKDIR /app COPY . . ENTRYPOINT ["dotnet", "LukeDictionary.Bot.dll"] \ No newline at end of file From 6df3072d9fddc4cdcc7f0d4ad3a2316cdc552a93 Mon Sep 17 00:00:00 2001 From: TehGM Date: Fri, 24 Dec 2021 23:04:48 +0100 Subject: [PATCH 4/6] dockerignore appsecrets example file --- LukeDictionary.Bot/.dockerignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LukeDictionary.Bot/.dockerignore b/LukeDictionary.Bot/.dockerignore index 3729ff0..1a61050 100644 --- a/LukeDictionary.Bot/.dockerignore +++ b/LukeDictionary.Bot/.dockerignore @@ -22,4 +22,5 @@ **/secrets.dev.yaml **/values.dev.yaml LICENSE -README.md \ No newline at end of file +README.md +appsecrets.Example.json \ No newline at end of file From d6089e129429c39098d0ac5cca55c44204f8a199 Mon Sep 17 00:00:00 2001 From: TehGM Date: Fri, 24 Dec 2021 23:07:20 +0100 Subject: [PATCH 5/6] Added dockerscript.sh --- LukeDictionary.Bot/.dockerignore | 3 ++- LukeDictionary.Bot/LukeDictionary.Bot.csproj | 4 ++++ LukeDictionary.Bot/dockerscript.sh | 21 ++++++++++++++++++++ README.md | 2 ++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 LukeDictionary.Bot/dockerscript.sh diff --git a/LukeDictionary.Bot/.dockerignore b/LukeDictionary.Bot/.dockerignore index 1a61050..614e383 100644 --- a/LukeDictionary.Bot/.dockerignore +++ b/LukeDictionary.Bot/.dockerignore @@ -23,4 +23,5 @@ **/values.dev.yaml LICENSE README.md -appsecrets.Example.json \ No newline at end of file +appsecrets.Example.json +dockerscript.sh \ No newline at end of file diff --git a/LukeDictionary.Bot/LukeDictionary.Bot.csproj b/LukeDictionary.Bot/LukeDictionary.Bot.csproj index 7e76c9d..c2eddd7 100644 --- a/LukeDictionary.Bot/LukeDictionary.Bot.csproj +++ b/LukeDictionary.Bot/LukeDictionary.Bot.csproj @@ -22,6 +22,7 @@ + @@ -34,6 +35,9 @@ Always + + Always + diff --git a/LukeDictionary.Bot/dockerscript.sh b/LukeDictionary.Bot/dockerscript.sh new file mode 100644 index 0000000..1638cc0 --- /dev/null +++ b/LukeDictionary.Bot/dockerscript.sh @@ -0,0 +1,21 @@ +#!/bin/bash +IMAGE="devsub/lukedictionary:latest" +CONTAINER="LukeDictionary" +ENVIRONMENT="Production" + +echo "Building Docker Image" +docker build -t $IMAGE . + +echo "Stopping old Docker Containers (error might pop, ignore it)" +docker stop $CONTAINER || true && docker rm $CONTAINER || true + +echo "Starting new Docker Container" +docker run -d --restart=always \ +--name=$CONTAINER \ +-e ASPNETCORE_ENVIRONMENT=$ENVIRONMENT \ +-e DOTNET_ENVIRONMENT=$ENVIRONMENT \ +-v "/var/log/LukeDictionary/$ENVIRONMENT":"/logs" \ +$IMAGE + +echo "Pruning old Docker Images" +docker image prune -f -a \ No newline at end of file diff --git a/README.md b/README.md index f17fd60..461772d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Run on host: 3. Move published files to host machine. 4. Run `dotnet LukeDictionary.Bot.dll`, create a systemd service, or build and run Docker image using [Dockerfile](LukeDictionary.Bot/Dockerfile). +There also is a [dockerscript.sh](LukeDictionary.Bot/dockerscript.sh) script to simplify running docker image with auto-restart. Of course it requires Docker installed on the host. + > Note: Do ***NOT*** push this docker image to remote repo (like dockerhub) if you created `appsecrets.json` file. Just don't, unless you want yor secrets compromised. ## License From fc6469607394ec12c5938d4516567663714926e0 Mon Sep 17 00:00:00 2001 From: TehGM Date: Fri, 24 Dec 2021 23:09:03 +0100 Subject: [PATCH 6/6] Update files copied on build --- LukeDictionary.Bot/LukeDictionary.Bot.csproj | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/LukeDictionary.Bot/LukeDictionary.Bot.csproj b/LukeDictionary.Bot/LukeDictionary.Bot.csproj index c2eddd7..78d02c3 100644 --- a/LukeDictionary.Bot/LukeDictionary.Bot.csproj +++ b/LukeDictionary.Bot/LukeDictionary.Bot.csproj @@ -21,12 +21,17 @@ + + - + + Always + + Always @@ -35,7 +40,10 @@ Always - + + Always + + Always