Skip to content

Commit

Permalink
Update To DSahrpPlus 4.4.2
Browse files Browse the repository at this point in the history
Create Docker Image
  • Loading branch information
CloudTheWolf committed Aug 5, 2023
1 parent 05bb9cb commit c4d83e3
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 52 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mono_crash.*
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
[Bb]uild/
x64/
x86/
[Ww][Ii][Nn]32/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>C:\Users\MichaelHoward\Documents\GitHub\CloudTheWolf.DSharpPlus.Scaffolding\CloudTheWolf.DSharpPlus.Scaffolding.Worker\bin\Debug\Plugins</OutputPath>
<OutputPath>E:\Harmony\</OutputPath>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

Expand Down
28 changes: 28 additions & 0 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/ClientErrors.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
43 changes: 43 additions & 0 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Actions/SocketErrors.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private void InitPlugins()

foreach (var plugin in PluginLoader.Plugins)
{
Console.WriteLine(plugin.Name);
plugin.InitPlugin(this, Logger, _config, Program.configuration);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,12 +19,18 @@
</Content>
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>E:\Harmony</OutputPath>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Data" Version="2.0.0.1" />
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Shared" Version="2.1.1.3" />
<PackageReference Include="CloudTheWolf.DSharpPlus.Scaffolding.Shared" Version="2.1.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
</ItemGroup>

Expand Down
20 changes: 20 additions & 0 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
5 changes: 5 additions & 0 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public class Options
/// Use DSharpPlus build in Help command
/// </summary>
public static bool DefaultHelp { get; set; }

/// <summary>
/// Cure Zombie and Other abnormal status. (Set to false to handle this is a plugin)
/// </summary>
public static bool ZombieCure { get; set; }
}
}
10 changes: 6 additions & 4 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>();
var tsLikePlugin = new List<Type>();

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)
{
Expand Down Expand Up @@ -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<Type>();
var tsLikePlugin = new List<Type>();

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)
Expand Down
17 changes: 11 additions & 6 deletions CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ private static void LoadConfig()
Options.EnableMentionPrefix = Program.configuration.GetValue<bool>("Discord:enableMentionPrefix");
Options.DmHelp = Program.configuration.GetValue<bool>("Discord:dmHelp");
Options.DefaultHelp = Program.configuration.GetValue<bool>("Discord:enableDefaultHelp");
Options.ZombieCure = Program.configuration.GetValue<bool>("UseZombieCure");

}


private void CreateClientCommandConfiguration()
private async Task CreateClientCommandConfiguration()
{
var commandsConfig = new CommandsNextConfiguration
{
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion CloudTheWolf.DSharpPlus.Scaffolding.Worker/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Worker(ILogger<Logger> 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<bool>("ShardMode"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}
},
"ShardMode": false,
"UseZombieCure": true,
"Discord": {
"token": "{{DISCORD_TOKEN}}",
"prefix": "a!",
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>c326da53-b1d0-4751-9881-82b4140c6a0c</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.4'

services:
cloudthewolf.dsharpplus.scaffolding.worker:
environment:
- DOTNET_ENVIRONMENT=Development
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.4'

services:
cloudthewolf.dsharpplus.scaffolding.worker:
image: ${DOCKER_REGISTRY-}cloudthewolfdsharpplusscaffoldingworker
build:
context: .
dockerfile: CloudTheWolf.DSharpPlus.Scaffolding.Worker/Dockerfile
11 changes: 11 additions & 0 deletions launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Docker Compose": {
"commandName": "DockerCompose",
"commandVersion": "1.0",
"serviceActions": {
"cloudthewolf.dsharpplus.scaffolding.worker": "StartDebugging"
}
}
}
}

0 comments on commit c4d83e3

Please sign in to comment.