From e3137eaa1f51ff33ae4786017e3f68481455b247 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 29 Oct 2024 23:05:16 +1300 Subject: [PATCH] Added command line arguments: --config, --transcript, --leave, --help, --version --- Config.cs | 17 ++++++++--- EventHandler.cs | 15 +++++++--- SupportChild.cs | 69 +++++++++++++++++++++++++++++++++++++++++++-- SupportChild.csproj | 44 +++++++++++++++++------------ Transcriber.cs | 13 +++++++-- 5 files changed, 126 insertions(+), 32 deletions(-) diff --git a/Config.cs b/Config.cs index 010fa3e..fd58e92 100644 --- a/Config.cs +++ b/Config.cs @@ -32,16 +32,25 @@ internal static class Config internal static string username = "supportchild"; internal static string password = ""; + private static string configPath = "./config.yml"; + public static void LoadConfig() { - // Writes default config to file if it does not already exist - if (!File.Exists("./config.yml")) + if (!string.IsNullOrEmpty(SupportChild.commandLineArgs.configPath)) { - File.WriteAllText("./config.yml", Utilities.ReadManifestData("default_config.yml")); + configPath = SupportChild.commandLineArgs.configPath; + } + + Logger.Log("Loading config \"" + Path.GetFullPath(configPath) + "\""); + + // Writes default config to file if it does not already exist + if (!File.Exists(configPath)) + { + File.WriteAllText(configPath, Utilities.ReadManifestData("default_config.yml")); } // Reads config contents into FileStream - FileStream stream = File.OpenRead("./config.yml"); + FileStream stream = File.OpenRead(configPath); // Converts the FileStream into a YAML object IDeserializer deserializer = new DeserializerBuilder().Build(); diff --git a/EventHandler.cs b/EventHandler.cs index ea1d59f..583787f 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.Entities; @@ -29,17 +30,23 @@ internal static class EventHandler return Task.CompletedTask; } - internal static Task OnGuildAvailable(DiscordClient _, GuildCreateEventArgs e) + internal static async Task OnGuildAvailable(DiscordClient discordClient, GuildCreateEventArgs e) { - Logger.Log("Guild available: " + e.Guild.Name); + Logger.Log("Found Discord server: " + e.Guild.Name + " (" + e.Guild.Id + ")"); + + if (SupportChild.commandLineArgs.serversToLeave.Contains(e.Guild.Id)) + { + Logger.Warn("LEAVING DISCORD SERVER AS REQUESTED: " + e.Guild.Name + " (" + e.Guild.Id + ")"); + await e.Guild.LeaveAsync(); + return; + } IReadOnlyDictionary roles = e.Guild.Roles; foreach ((ulong roleID, DiscordRole role) in roles) { - Logger.Log(role.Name.PadRight(40, '.') + roleID); + Logger.Debug(role.Name.PadRight(40, '.') + roleID); } - return Task.CompletedTask; } internal static Task OnClientError(DiscordClient _, ClientErrorEventArgs e) diff --git a/SupportChild.cs b/SupportChild.cs index cc9bc48..0f8147f 100644 --- a/SupportChild.cs +++ b/SupportChild.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; +using System.Linq; using DSharpPlus; using DSharpPlus.Interactivity; using DSharpPlus.Interactivity.Enums; @@ -9,6 +11,7 @@ using DSharpPlus.Interactivity.Extensions; using DSharpPlus.SlashCommands; using Microsoft.Extensions.Logging; using SupportChild.Commands; +using CommandLine; namespace SupportChild; @@ -30,8 +33,65 @@ internal static class SupportChild private static SlashCommandsExtension commands = null; - private static void Main() + public class CommandLineArguments { + [CommandLine.Option('c', + "config", + Required = false, + HelpText = "Select a config file to use.", + Default = "config.yml", + MetaValue = "PATH")] + public string configPath { get; set; } + + [CommandLine.Option('t', + "transcripts", + Required = false, + HelpText = "Select directory to store transcripts in.", + Default = "./transcripts", + MetaValue = "PATH")] + public string transcriptDir { get; set; } + + [CommandLine.Option( + "leave", + Required = false, + HelpText = "Leaves one or more Discord servers. " + + "You can check which servers your bot is in when it starts up.", + MetaValue = "ID,ID,ID...", + Separator = ',' + )] + public IEnumerable serversToLeave { get; set; } + } + + internal static CommandLineArguments commandLineArgs; + + private static void Main(string[] args) + { + StringWriter sw = new StringWriter(); + commandLineArgs = new Parser(settings => + { + settings.AutoHelp = true; + settings.HelpWriter = sw; + settings.AutoVersion = false; + }).ParseArguments(args).Value; + + // CommandLineParser has some bugs related to the built-in version option, ignore the output if it isn't found. + if (!sw.ToString().Contains("Option 'version' is unknown.")) + { + Console.Write(sw); + } + + if (args.Contains("--help")) + { + return; + } + + if (args.Contains("--version")) + { + Console.WriteLine(Assembly.GetEntryAssembly()?.GetName().Name + ' ' + GetVersion()); + Console.WriteLine("Build time: " + BuildInfo.BuildTimeUTC.ToString("yyyy-MM-dd HH:mm:ss") + " UTC"); + return; + } + MainAsync().GetAwaiter().GetResult(); } @@ -55,7 +115,11 @@ internal static class SupportChild public static string GetVersion() { Version version = Assembly.GetEntryAssembly()?.GetName().Version; - return version?.Major + "." + version?.Minor + "." + version?.Build + (version?.Revision == 0 ? "" : "-" + (char)(64 + version?.Revision ?? 0)); + return version?.Major + "." + + version?.Minor + "." + + version?.Build + + (version?.Revision == 0 ? "" : "-" + (char)(64 + version?.Revision ?? 0)) + + " (" + ThisAssembly.Git.Commit + ")"; } public static async void Reload() @@ -66,7 +130,6 @@ internal static class SupportChild discordClient.Dispose(); } - Logger.Log("Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\""); Config.LoadConfig(); // Check if token is unset diff --git a/SupportChild.csproj b/SupportChild.csproj index 77d9f3f..edbd738 100644 --- a/SupportChild.csproj +++ b/SupportChild.csproj @@ -1,26 +1,28 @@  - Exe - 3.1.0.0 - ellie_icon.ico - net8.0 - SupportChild.SupportChild - win-x64;linux-x64 - true - partial + Exe + 3.1.0 + ellie_icon.ico + net8.0 + SupportChild.SupportChild + win-x64;linux-x64 + true + partial - EmotionChild - A Discord support ticket bot built for the Ellie's home server - en - Git - https://toastielab.dev/Emotions-stuff/SupportChild + EmotionChild + A Discord support ticket bot built for the Ellie's home server + en + Git + https://toastielab.dev/Emotions-stuff/SupportChild - https://cdn.discordapp.com/attachments/765441543100170271/914327948667011132/Ellie_Concept_2_transparent_ver.png - LICENSE - https://toastielab.dev/Emotions-stuff/SupportChild - 3.1.0 - default + https://cdn.discordapp.com/attachments/765441543100170271/914327948667011132/Ellie_Concept_2_transparent_ver.png + LICENSE + https://toastielab.dev/Emotions-stuff/SupportChild + 3.1.0 + default + false + false @@ -28,9 +30,15 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Transcriber.cs b/Transcriber.cs index ee2500b..6967545 100644 --- a/Transcriber.cs +++ b/Transcriber.cs @@ -11,14 +11,21 @@ namespace SupportChild; internal static class Transcriber { + private static string transcriptDir = "./transcripts"; + internal static async Task ExecuteAsync(ulong channelID, uint ticketID) { DiscordClient discordClient = new DiscordClient(Config.token); ChannelExporter exporter = new ChannelExporter(discordClient); - if (!Directory.Exists("./transcripts")) + if (!string.IsNullOrEmpty(SupportChild.commandLineArgs.transcriptDir)) { - Directory.CreateDirectory("./transcripts"); + transcriptDir = SupportChild.commandLineArgs.transcriptDir; + } + + if (!Directory.Exists(transcriptDir)) + { + Directory.CreateDirectory(transcriptDir); } Channel channel = await discordClient.GetChannelAsync(new Snowflake(channelID)); @@ -46,7 +53,7 @@ internal static class Transcriber internal static string GetPath(uint ticketNumber) { - return "./transcripts/" + GetFilename(ticketNumber); + return transcriptDir + "/" + GetFilename(ticketNumber); } internal static string GetFilename(uint ticketNumber)