Added command line arguments: --config, --transcript, --leave, --help, --version

This commit is contained in:
Toastie (DCS Team) 2024-10-29 23:05:16 +13:00
parent 9f15236878
commit e3137eaa1f
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
5 changed files with 126 additions and 32 deletions

View file

@ -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();

View file

@ -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<ulong, DiscordRole> 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)

View file

@ -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<ulong> 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<CommandLineArguments>(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

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>3.1.0.0</Version>
<Version>3.1.0</Version>
<ApplicationIcon>ellie_icon.ico</ApplicationIcon>
<TargetFramework>net8.0</TargetFramework>
<StartupObject>SupportChild.SupportChild</StartupObject>
@ -21,6 +21,8 @@
<PackageProjectUrl>https://toastielab.dev/Emotions-stuff/SupportChild</PackageProjectUrl>
<PackageVersion>3.1.0</PackageVersion>
<LangVersion>default</LangVersion>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<GitVersion>false</GitVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@ -28,9 +30,15 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="DSharpPlus" Version="4.5.0" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.5.0" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.5.0" />
<PackageReference Include="EmbeddedBuildTime" Version="1.0.3" />
<PackageReference Include="GitInfo" Version="3.3.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View file

@ -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)