From 0e5e0bf4962054730542c6ce3ff1194efa439027 Mon Sep 17 00:00:00 2001 From: Lunar Starstrum Date: Tue, 17 May 2022 15:53:57 -0500 Subject: [PATCH] Docker --- .dockerignore | 6 +++++ .github/workflows/docker_image.yml | 39 ++++++++++++++++++++++++++++ .vscode/launch.json | 24 +++++++++++++++++ .vscode/tasks.json | 41 ++++++++++++++++++++++++++++++ Dockerfile | 14 ++++++++++ Modules/Commands.cs | 16 +++++++----- Program.cs | 5 +--- Query/Extensions/BaseDisplay.cs | 3 ++- README.md | 5 ++++ docker-compose.yml | 8 ++++++ 10 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker_image.yml create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..dffd3d0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.github +.vscode +.gitignore +.editorconfig +bin +obj diff --git a/.github/workflows/docker_image.yml b/.github/workflows/docker_image.yml new file mode 100644 index 0000000..723c342 --- /dev/null +++ b/.github/workflows/docker_image.yml @@ -0,0 +1,39 @@ +name: Publish Docker image +on: + release: + types: [published] + +jobs: + push_to_registry: + name: Publish Docker image + runs-on: self-hosted + steps: + - name: Check out the repo + uses: actions/checkout@v2 + + - name: Get Previous tag + id: previoustag + uses: WyriHaximus/github-action-get-previous-tag@master + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and Push + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + push: true + tags: | + ghcr.io/dsharpplus/dsharpplusdocs:latest + ghcr.io/dsharpplus/dsharpplusdocs:${{ steps.previoustag.outputs.tag }} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0154e1a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/bin/Debug/net6.0/DSharpPlusDocs.dll", + "args": [], + "cwd": "${workspaceFolder}", + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..9a018fa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/DSharpPlusDocs.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/DSharpPlusDocs.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/DSharpPlusDocs.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4937cdb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build +WORKDIR /src + +COPY ./ /src +RUN dotnet restore -r linux-musl-x64 && dotnet publish -c Release -r linux-musl-x64 --no-restore --self-contained -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true -p:DebugType=embedded + +FROM alpine:latest +WORKDIR /src + +COPY --from=build /src/bin/Release/net6.0/linux-musl-x64/publish /src +COPY ./res /src/res +RUN apk upgrade --update-cache --available && apk add openssl libstdc++ icu-libs && rm -rf /var/cache/apk/* + +ENTRYPOINT /src/DSharpPlusDocs \ No newline at end of file diff --git a/Modules/Commands.cs b/Modules/Commands.cs index 76f790e..ecf324b 100644 --- a/Modules/Commands.cs +++ b/Modules/Commands.cs @@ -41,13 +41,15 @@ using Microsoft.CodeAnalysis.Scripting; using Microsoft.Extensions.DependencyInjection; +#pragma warning disable CA1822 // Mark members as static + namespace DSharpPlusDocs.Modules { public class GeneralCommands : BaseCommandModule { [Command("clean")] [Description("Delete all the messages from this bot within the last X messages")] - public static async Task CleanAsync(CommandContext ctx, int messages = 30) + public async Task CleanAsync(CommandContext ctx, int messages = 30) { if (messages > 50) { @@ -68,16 +70,16 @@ public static async Task CleanAsync(CommandContext ctx, int messages = 30) [Command("docs")] [Description("Show the docs url")] - public static async Task DocsAsync(CommandContext ctx) => await ctx.RespondAsync($"Docs: {QueryHandler.DocsBaseUrl}"); + public async Task DocsAsync(CommandContext ctx) => await ctx.RespondAsync($"Docs: {QueryHandler.DocsBaseUrl}"); [Command("invite")] [Description("Show the invite url")] - public static async Task InviteAsync(CommandContext ctx) => await ctx.RespondAsync("Invite: https://discordapp.com/oauth2/authorize?client_id=341606460720939008&scope=bot"); + public async Task InviteAsync(CommandContext ctx) => await ctx.RespondAsync("Invite: https://discordapp.com/oauth2/authorize?client_id=341606460720939008&scope=bot"); [Command("guides")] [Aliases("guide")] [Description("Show the url of a guide")] - public static async Task GuidesAsync(CommandContext ctx, [RemainingText] string guide = null) + public async Task GuidesAsync(CommandContext ctx, [RemainingText] string guide = null) { try { @@ -202,7 +204,7 @@ public static async Task GuidesAsync(CommandContext ctx, [RemainingText] string [Command("info")] [Description("Show some information about the application")] - public static async Task InfoAsync(CommandContext ctx) + public async Task InfoAsync(CommandContext ctx) { _ = ctx.Client.CurrentApplication; MainHandler mainHandler = ctx.Services.GetService(); @@ -244,7 +246,7 @@ public static async Task InfoAsync(CommandContext ctx) [Command("eval")] //TODO: Safe eval ? 👀 [RequireOwner] - public static async Task EvalAsync(CommandContext ctx, [RemainingText] string code) + public async Task EvalAsync(CommandContext ctx, [RemainingText] string code) { /*using (Context.Channel.EnterTypingState()) {*/ @@ -278,7 +280,7 @@ public static async Task EvalAsync(CommandContext ctx, [RemainingText] string co [Command("setdocsurl")] [RequireOwner] - public static async Task SetDocsUrlAsync(CommandContext ctx, [RemainingText] string url) + public async Task SetDocsUrlAsync(CommandContext ctx, [RemainingText] string url) { if (!url.EndsWith("/")) { diff --git a/Program.cs b/Program.cs index 1c65ff7..6d36e30 100644 --- a/Program.cs +++ b/Program.cs @@ -25,9 +25,6 @@ namespace DSharpPlusDocs { public class Program { - public static void Main() - { - new DSharpPlusDocs().RunAsync().GetAwaiter().GetResult(); - } + public static void Main() => new DSharpPlusDocs().RunAsync().GetAwaiter().GetResult(); } } diff --git a/Query/Extensions/BaseDisplay.cs b/Query/Extensions/BaseDisplay.cs index aff4adc..72cc879 100644 --- a/Query/Extensions/BaseDisplay.cs +++ b/Query/Extensions/BaseDisplay.cs @@ -130,7 +130,8 @@ private static string GetDocsUrlPath(object o) public static bool IsInherited(object o) => o is PropertyInfoWrapper property ? $"{property.Parent.TypeInfo.Namespace}.{property.Parent.TypeInfo.Name}" != $"{property.Property.DeclaringType.Namespace}.{property.Property.DeclaringType.Name}" - : o is MethodInfoWrapper method && $"{method.Parent.TypeInfo.Namespace}.{method.Parent.TypeInfo.Name}" != $"{method.Method.DeclaringType.Namespace}.{method.Method.DeclaringType.Name}"; + : o is MethodInfoWrapper method + && $"{method.Parent.TypeInfo.Namespace}.{method.Parent.TypeInfo.Name}" != $"{method.Method.DeclaringType.Namespace}.{method.Method.DeclaringType.Name}"; private static List GetPaths(IEnumerable list) => list.Select(x => GetPath(x)).ToList(); diff --git a/README.md b/README.md new file mode 100644 index 0000000..86db058 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# DSharpPlus Documentation Bot +Don't host this until the rewrite. PR's to make the code more optimized are welcome. + +# Getting Started +Set the `DISCORD_TOKEN` environment variable to your bot's token, then `dotnet run`. Alternatively you can use Docker. \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cb2866e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: "3.9" + +services: + tomoe: + build: . + environment: + - "DISCORD_TOKEN": "" + restart: unless-stopped \ No newline at end of file