diff --git a/LukeDictionary.Bot/.dockerignore b/LukeDictionary.Bot/.dockerignore index 3729ff0..614e383 100644 --- a/LukeDictionary.Bot/.dockerignore +++ b/LukeDictionary.Bot/.dockerignore @@ -22,4 +22,6 @@ **/secrets.dev.yaml **/values.dev.yaml LICENSE -README.md \ No newline at end of file +README.md +appsecrets.Example.json +dockerscript.sh \ No newline at end of file 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/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 diff --git a/LukeDictionary.Bot/LukeDictionary.Bot.csproj b/LukeDictionary.Bot/LukeDictionary.Bot.csproj index d8706d3..78d02c3 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 @@ -21,11 +21,17 @@ + + + - + + Always + + Always @@ -34,6 +40,12 @@ Always + + Always + + + Always + 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": { 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