Re-add discord code

This commit is contained in:
Timothy Baldridge 2022-01-18 15:58:54 -07:00
parent b7119c8bf8
commit 035b8e7aa7
6 changed files with 131 additions and 12 deletions

View File

@ -22,15 +22,7 @@ public class CommandLineBuilder
var root = new RootCommand();
foreach (var verb in _verbs)
root.Add(verb.MakeCommand());
/*
foreach (var verb in _verbs)
root.AddCommand(verb.MakeCommand());
var builder = new System.CommandLine.Builder.CommandLineBuilder(root);
var built = builder.Build();
var parsed = built.Parse(args);
return await parsed.InvokeAsync(_console);*/
return await root.InvokeAsync(args);
}
}

View File

@ -17,6 +17,8 @@ public class AppSettings
public AbsolutePath TempPath => (AbsolutePath) TempFolder;
public string SpamWebHook { get; set; } = null;
public string HamWebHook { get; set; } = null;
public string DiscordKey { get; set; }
public string AuthoredFilesFolder { get; set; }

View File

@ -0,0 +1,104 @@
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Logging;
using Wabbajack.BuildServer;
namespace Wabbajack.Server.Services;
public class DiscordBackend
{
private readonly AppSettings _settings;
private readonly ILogger<DiscordBackend> _logger;
private readonly DiscordSocketClient _client;
private readonly NexusCacheManager _nexusCacheManager;
public DiscordBackend(ILogger<DiscordBackend> logger, AppSettings settings, NexusCacheManager nexusCacheManager)
{
_settings = settings;
_logger = logger;
_nexusCacheManager = nexusCacheManager;
_client = new DiscordSocketClient(new DiscordSocketConfig()
{
});
_client.Log += LogAsync;
_client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync;
Task.Run(async () =>
{
await _client.LoginAsync(TokenType.Bot, settings.DiscordKey);
await _client.StartAsync();
});
}
private async Task MessageReceivedAsync(SocketMessage arg)
{
_logger.LogInformation(arg.Content);
if (arg.Content.StartsWith("!dervenin"))
{
var parts = arg.Content.Split(" ", StringSplitOptions.RemoveEmptyEntries);
if (parts[0] != "!dervenin")
return;
if (parts.Length == 1)
{
await ReplyTo(arg, "Wat?");
}
if (parts[1] == "purge-nexus-cache")
{
if (parts.Length != 3)
{
await ReplyTo(arg, "Welp you did that wrong, gotta give me a mod-id or url");
return;
}
var rows = await _nexusCacheManager.Purge(parts[2]);
await ReplyTo(arg, $"Purged {rows} rows");
}
if (parts[1] == "nft")
{
await ReplyTo(arg, "No Fucking Thanks.");
}
}
}
private async Task ReplyTo(SocketMessage socketMessage, string message)
{
await socketMessage.Channel.SendMessageAsync(message);
}
private async Task ReadyAsync()
{
}
private async Task LogAsync(LogMessage arg)
{
switch (arg.Severity)
{
case LogSeverity.Info:
_logger.LogInformation(arg.Message);
break;
case LogSeverity.Warning:
_logger.LogWarning(arg.Message);
break;
case LogSeverity.Critical:
_logger.LogCritical(arg.Message);
break;
case LogSeverity.Error:
_logger.LogError(arg.Exception, arg.Message);
break;
case LogSeverity.Verbose:
_logger.LogTrace(arg.Message);
break;
case LogSeverity.Debug:
_logger.LogDebug(arg.Message);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}

View File

@ -145,4 +145,25 @@ public class NexusCacheManager
_lockObject.Release();
}
}
public async Task<int> Purge(string mod)
{
if (Uri.TryCreate(mod, UriKind.Absolute, out var url))
{
mod = Enumerable.Last(url.AbsolutePath.Split("/", StringSplitOptions.RemoveEmptyEntries));
}
var count = 0;
if (!int.TryParse(mod, out var mod_id)) return count;
foreach (var file in _cacheFolder.EnumerateFiles())
{
if (!file.FileName.ToString().Contains($"_{mod_id}")) continue;
await PurgeCacheEntry(file);
count++;
}
return count;
}
}

View File

@ -75,6 +75,7 @@ public class Startup
services.AddSingleton<Client>();
services.AddSingleton<NexusCacheManager>();
services.AddSingleton<NexusApi>();
services.AddSingleton<DiscordBackend>();
services.AddAllSingleton<ITokenProvider<NexusApiState>, NexusApiTokenProvider>();
services.AddAllSingleton<IResource, IResource<HttpClient>>(s => new Resource<HttpClient>("Web Requests", 12));
// Application Info
@ -176,6 +177,7 @@ public class Startup
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
// Trigger the internal update code
var _ = app.ApplicationServices.GetRequiredService<NexusCacheManager>();
app.ApplicationServices.GetRequiredService<NexusCacheManager>();
app.ApplicationServices.GetRequiredService<DiscordBackend>();
}
}

View File

@ -14,8 +14,6 @@
"PatchesFilesFolder": "c:\\tmp\\patches",
"MirrorFilesFolder": "c:\\tmp\\mirrors",
"NexusCacheFolder": "c:\\tmp\\nexus-cache",
"HamWebHook": "https://discordapp.com/api/webhooks/710264989646323772/2VI5Y0rYLOtq1o1KeSR6S2xTRNIOdxONiCju81rnoEUKtcdbVZDdC_PnLdEpYOdiMHCt",
"SpamWebHook": "https://discordapp.com/api/webhooks/710264989646323772/2VI5Y0rYLOtq1o1KeSR6S2xTRNIOdxONiCju81rnoEUKtcdbVZDdC_PnLdEpYOdiMHCt",
"GitHubKey": ""
},
"AllowedHosts": "*"