2023-10-20 07:49:23 +00:00
|
|
|
|
using Discord.Net;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Newtonsoft.Json;
|
2023-12-20 14:56:03 +00:00
|
|
|
|
using BiblioTech.Rewards;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
using Logging;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
|
|
|
|
|
namespace BiblioTech
|
|
|
|
|
{
|
|
|
|
|
public class CommandHandler
|
|
|
|
|
{
|
|
|
|
|
private readonly DiscordSocketClient client;
|
2024-06-27 09:16:17 +00:00
|
|
|
|
private readonly CustomReplacement replacement;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
private readonly BaseCommand[] commands;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
private readonly ILog log;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
|
2024-06-27 09:16:17 +00:00
|
|
|
|
public CommandHandler(ILog log, DiscordSocketClient client, CustomReplacement replacement, params BaseCommand[] commands)
|
2023-10-20 07:49:23 +00:00
|
|
|
|
{
|
|
|
|
|
this.client = client;
|
2024-06-27 09:16:17 +00:00
|
|
|
|
this.replacement = replacement;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
this.commands = commands;
|
2024-06-26 09:27:19 +00:00
|
|
|
|
this.log = log;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
client.Ready += Client_Ready;
|
|
|
|
|
client.SlashCommandExecuted += SlashCommandHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task Client_Ready()
|
|
|
|
|
{
|
2024-05-16 12:05:58 +00:00
|
|
|
|
var guild = client.Guilds.Single(g => g.Id == Program.Config.ServerId);
|
2023-10-22 08:38:46 +00:00
|
|
|
|
Program.AdminChecker.SetGuild(guild);
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Log($"Initializing for guild: '{guild.Name}'");
|
2023-12-18 10:27:28 +00:00
|
|
|
|
|
|
|
|
|
var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray();
|
|
|
|
|
if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel");
|
|
|
|
|
Program.AdminChecker.SetAdminChannel(adminChannels.First());
|
2024-06-27 09:16:17 +00:00
|
|
|
|
Program.RoleDriver = new RoleDriver(client, log, replacement);
|
2023-10-20 07:49:23 +00:00
|
|
|
|
|
|
|
|
|
var builders = commands.Select(c =>
|
|
|
|
|
{
|
2023-12-18 10:27:28 +00:00
|
|
|
|
var msg = $"Building command '{c.Name}' with options: ";
|
2023-10-20 07:49:23 +00:00
|
|
|
|
var builder = new SlashCommandBuilder()
|
|
|
|
|
.WithName(c.Name)
|
|
|
|
|
.WithDescription(c.Description);
|
|
|
|
|
|
|
|
|
|
foreach (var option in c.Options)
|
|
|
|
|
{
|
2023-12-18 10:27:28 +00:00
|
|
|
|
msg += option.Name + " ";
|
2023-10-24 12:07:15 +00:00
|
|
|
|
builder.AddOption(option.Build());
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Log(msg);
|
2023-10-20 07:49:23 +00:00
|
|
|
|
return builder;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-02 08:08:13 +00:00
|
|
|
|
log.Log("Building application commands...");
|
|
|
|
|
var commands = builders.Select(b => b.Build()).ToArray();
|
|
|
|
|
|
|
|
|
|
log.Log("Submitting application commands...");
|
|
|
|
|
var response = await guild.BulkOverwriteApplicationCommandAsync(commands);
|
|
|
|
|
|
|
|
|
|
log.Log("Commands in response:");
|
|
|
|
|
foreach (var cmd in response)
|
2023-10-20 07:49:23 +00:00
|
|
|
|
{
|
2024-07-02 08:08:13 +00:00
|
|
|
|
log.Log($"{cmd.Name} ({cmd.Description}) [{DescribOptions(cmd.Options)}]");
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException exception)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
2024-06-26 09:27:19 +00:00
|
|
|
|
log.Error(json);
|
2024-07-02 08:37:32 +00:00
|
|
|
|
throw;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
2024-07-02 08:08:13 +00:00
|
|
|
|
log.Log("Initialized.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string DescribOptions(IReadOnlyCollection<SocketApplicationCommandOption> options)
|
|
|
|
|
{
|
|
|
|
|
return string.Join(",", options.Select(DescribeOption).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string DescribeOption(SocketApplicationCommandOption option)
|
|
|
|
|
{
|
|
|
|
|
return $"({option.Name}[{DescribOptions(option.Options)}])";
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SlashCommandHandler(SocketSlashCommand command)
|
|
|
|
|
{
|
|
|
|
|
foreach (var cmd in commands)
|
|
|
|
|
{
|
|
|
|
|
await cmd.SlashCommandHandler(command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|