diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5fbc8af..a3fb9d9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ mono_crash.* [Dd]ebugPublic/ [Rr]elease/ [Rr]eleases/ +[Bb]uild/ x64/ x86/ [Ww][Ii][Nn]32/ diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module.csproj b/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module.csproj index ca03e87..750e813 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module.csproj +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module/CloudTheWolf.DSharpPlus.Scaffolding.Example.Module.csproj @@ -8,7 +8,7 @@ - C:\Users\MichaelHoward\Documents\GitHub\CloudTheWolf.DSharpPlus.Scaffolding\CloudTheWolf.DSharpPlus.Scaffolding.Worker\bin\Debug\Plugins + E:\Harmony\ AnyCPU diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/ClientErrors.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/ClientErrors.cs new file mode 100644 index 0000000..1abab49 --- /dev/null +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/ClientErrors.cs @@ -0,0 +1,28 @@ +using DSharpPlus.EventArgs; +using DSharpPlus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CloudTheWolf.DSharpPlus.Scaffolding.Worker.Actions +{ + internal class ClientErrors + { + + public static async Task Errored(DiscordClient sender, ClientErrorEventArgs e) + { + try + { + sender.ReconnectAsync(true); + + } + catch (Exception ex) + { + + Environment.Exit(500); + } + } + } +} diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/SocketErrors.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/SocketErrors.cs new file mode 100644 index 0000000..d900b15 --- /dev/null +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/SocketErrors.cs @@ -0,0 +1,43 @@ +using DSharpPlus.EventArgs; +using DSharpPlus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CloudTheWolf.DSharpPlus.Scaffolding.Worker.Actions +{ + using Microsoft.Extensions.Logging; + using Serilog; + + internal class SocketErrors + { + public static async Task Closed(DiscordClient sender, SocketCloseEventArgs e) + { + try + { + await sender.ReconnectAsync(true); + } + catch (Exception ex) + { + Log.Error(ex.Message); + Environment.Exit(500); + } + + } + + public static async Task Errored(DiscordClient sender, SocketErrorEventArgs e) + { + try + { + await sender.ReconnectAsync(true); + } + catch (Exception ex) + { + Log.Error(ex.Message); + Environment.Exit(500); + } + } + } +} diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs index 5578a7d..059a990 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs @@ -56,6 +56,7 @@ private void InitPlugins() foreach (var plugin in PluginLoader.Plugins) { + Console.WriteLine(plugin.Name); plugin.InitPlugin(this, Logger, _config, Program.configuration); } } diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj index e7b3b0d..70a7eda 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj @@ -6,6 +6,7 @@ true false True + Linux @@ -18,12 +19,18 @@ + + E:\Harmony + AnyCPU + + - + - + + diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile new file mode 100644 index 0000000..8d2f928 --- /dev/null +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile @@ -0,0 +1,20 @@ +#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj", "CloudTheWolf.DSharpPlus.Scaffolding.Worker/"] +RUN dotnet restore "CloudTheWolf.DSharpPlus.Scaffolding.Worker/CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj" +COPY . . +WORKDIR "/src/CloudTheWolf.DSharpPlus.Scaffolding.Worker" +RUN dotnet build "CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "CloudTheWolf.DSharpPlus.Scaffolding.Worker.csproj" -c Release -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "CloudTheWolf.DSharpPlus.Scaffolding.Worker.dll"] \ No newline at end of file diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Options.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Options.cs index ed925c7..4887f9f 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Options.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Options.cs @@ -38,5 +38,10 @@ public class Options /// Use DSharpPlus build in Help command /// public static bool DefaultHelp { get; set; } + + /// + /// Cure Zombie and Other abnormal status. (Set to false to handle this is a plugin) + /// + public static bool ZombieCure { get; set; } } } diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Program.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Program.cs index 764ae46..2fbb87a 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Program.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Program.cs @@ -29,10 +29,12 @@ public static void Main(string[] args) private static void ConfigureServices(IServiceCollection serviceCollection) { - - - var config = "appsettings.json"; - + var configPath = Environment.GetEnvironmentVariable("WORKER_CONFIG_DIR"); + if (!string.IsNullOrEmpty(configPath) && !configPath.EndsWith("/")) + { + configPath = $"{configPath}/"; + } + var config = $"{configPath}appsettings.json"; serviceCollection.AddSingleton(LoggerFactory.Create(builder => { diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs index 7513a15..520cb9c 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Services/PluginLoader.cs @@ -36,33 +36,17 @@ public void LoadPlugins() var interfaceType = typeof(IPlugin); //Fetch all types that implement the interface IPlugin and are a class var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - var ts = new List(); - var tsLikePlugin = new List(); - foreach (var assembly in assemblies) - { - var item = assembly.GetTypes(); - ts.Add(item.FirstOrDefault()); - } - - foreach (var type in ts) - { - if (interfaceType.IsAssignableFrom(type)) - { - if (type.IsClass) - { - tsLikePlugin.Add(type); - } - } - } + var ts = assemblies.Select(assembly => assembly.GetTypes()).Select(item => item.FirstOrDefault()).ToList(); - var types = tsLikePlugin.ToArray(); + var types = ts.Where(type => interfaceType.IsAssignableFrom(type)).Where(type => type.IsClass).ToArray(); //Create a new instance of all found types foreach (var type in types) { Plugins.Add((IPlugin)Activator.CreateInstance(type)); } + } catch (Exception e) { @@ -91,27 +75,10 @@ public void LoadShardPlugins() var interfaceType = typeof(IShardPlugin); //Fetch all types that implement the interface IPlugin and are a class var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - var ts = new List(); - var tsLikePlugin = new List(); - foreach (var assembly in assemblies) - { - var item = assembly.GetTypes(); - ts.Add(item.FirstOrDefault()); - } - - foreach (var type in ts) - { - if (interfaceType.IsAssignableFrom(type)) - { - if (type.IsClass) - { - tsLikePlugin.Add(type); - } - } - } + var ts = assemblies.Select(assembly => assembly.GetTypes()).Select(item => item.FirstOrDefault()).ToList(); - var types = tsLikePlugin.ToArray(); + var types = ts.Where(type => interfaceType.IsAssignableFrom(type)).Where(type => type.IsClass).ToArray(); //Create a new instance of all found types foreach (var type in types) diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs index 0b23ec5..1c6d932 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs @@ -65,11 +65,12 @@ private static void LoadConfig() Options.EnableMentionPrefix = Program.configuration.GetValue("Discord:enableMentionPrefix"); Options.DmHelp = Program.configuration.GetValue("Discord:dmHelp"); Options.DefaultHelp = Program.configuration.GetValue("Discord:enableDefaultHelp"); + Options.ZombieCure = Program.configuration.GetValue("UseZombieCure"); } - private void CreateClientCommandConfiguration() + private async Task CreateClientCommandConfiguration() { var commandsConfig = new CommandsNextConfiguration { @@ -81,16 +82,20 @@ private void CreateClientCommandConfiguration() }; - Commands = Client.UseCommandsNextAsync(commandsConfig).Result; + Commands = await Client.UseCommandsNextAsync(commandsConfig).ConfigureAwait(false); } - private void CreateDiscordClient() + private async Task CreateDiscordClient() { Client = new DiscordShardedClient(_config); - Interactivity = Client.GetInteractivityAsync().Result; + Interactivity = await Client.GetInteractivityAsync().ConfigureAwait(false); Client.Ready += OnClientReady; - SlashCommandsExt = Client.UseSlashCommandsAsync().Result; - + SlashCommandsExt = await Client.UseSlashCommandsAsync().ConfigureAwait(false); + if(!Options.ZombieCure) return; + Client.ClientErrored += Actions.ClientErrors.Errored; + Client.SocketErrored += Actions.SocketErrors.Errored; + Client.SocketClosed += Actions.SocketErrors.Closed; + } private static void SetDiscordConfig() diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Worker.cs b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Worker.cs index b762b88..6db3bf7 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Worker.cs +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/Worker.cs @@ -19,7 +19,7 @@ public Worker(ILogger logger) protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - while (!stoppingToken.IsCancellationRequested) + while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); if(Program.configuration.GetValue("ShardMode")) diff --git a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/appsettings.json.example b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/appsettings.json.example index 269e00b..f67f83e 100644 --- a/CloudTheWolf.DSharpPlus.Scaffolding.Worker/appsettings.json.example +++ b/CloudTheWolf.DSharpPlus.Scaffolding.Worker/appsettings.json.example @@ -7,6 +7,7 @@ } }, "ShardMode": false, + "UseZombieCure": true, "Discord": { "token": "{{DISCORD_TOKEN}}", "prefix": "a!", diff --git a/docker-compose.dcproj b/docker-compose.dcproj new file mode 100644 index 0000000..6f4f9f5 --- /dev/null +++ b/docker-compose.dcproj @@ -0,0 +1,15 @@ + + + + 2.1 + Linux + c326da53-b1d0-4751-9881-82b4140c6a0c + + + + docker-compose.yml + + + + + \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..4b618be --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,8 @@ +version: '3.4' + +services: + cloudthewolf.dsharpplus.scaffolding.worker: + environment: + - DOTNET_ENVIRONMENT=Development + volumes: + - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1867bba --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: '3.4' + +services: + cloudthewolf.dsharpplus.scaffolding.worker: + image: ${DOCKER_REGISTRY-}cloudthewolfdsharpplusscaffoldingworker + build: + context: . + dockerfile: CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile diff --git a/launchSettings.json b/launchSettings.json new file mode 100644 index 0000000..20bb02e --- /dev/null +++ b/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "Docker Compose": { + "commandName": "DockerCompose", + "commandVersion": "1.0", + "serviceActions": { + "cloudthewolf.dsharpplus.scaffolding.worker": "StartDebugging" + } + } + } +} \ No newline at end of file