cs-codex-dist-tests/Tools/BiblioTech/BaseCommand.cs

57 lines
1.7 KiB
C#
Raw Normal View History

2023-10-20 07:49:23 +00:00
using Discord.WebSocket;
2023-10-24 13:25:45 +00:00
using BiblioTech.Options;
2023-10-25 09:25:27 +00:00
using Discord;
2023-10-20 07:49:23 +00:00
namespace BiblioTech
{
public abstract class BaseCommand
{
public abstract string Name { get; }
public abstract string StartingMessage { get; }
2023-10-20 07:49:23 +00:00
public abstract string Description { get; }
public virtual CommandOption[] Options
{
get
{
return Array.Empty<CommandOption>();
}
}
public async Task SlashCommandHandler(SocketSlashCommand command)
{
if (command.CommandName != Name) return;
try
{
2023-10-25 08:54:26 +00:00
await command.RespondAsync(StartingMessage, ephemeral: true);
2023-10-24 13:25:45 +00:00
await Invoke(new CommandContext(command, command.Data.Options));
2023-10-25 08:54:26 +00:00
await command.DeleteOriginalResponseAsync();
}
catch (Exception ex)
{
2023-10-25 08:54:26 +00:00
await command.FollowupAsync("Something failed while trying to do that...", ephemeral: true);
Console.WriteLine(ex);
}
2023-10-20 07:49:23 +00:00
}
2023-10-24 13:25:45 +00:00
protected abstract Task Invoke(CommandContext context);
2023-10-22 08:10:52 +00:00
protected bool IsSenderAdmin(SocketSlashCommand command)
{
2023-10-22 08:38:46 +00:00
return Program.AdminChecker.IsUserAdmin(command.User.Id);
2023-10-22 08:10:52 +00:00
}
protected bool IsInAdminChannel(SocketSlashCommand command)
{
return Program.AdminChecker.IsAdminChannel(command.Channel);
}
2023-10-25 09:25:27 +00:00
protected IUser GetUserFromCommand(UserOption userOption, CommandContext context)
2023-10-22 08:10:52 +00:00
{
2023-10-25 09:25:27 +00:00
var targetUser = userOption.GetUser(context);
if (IsSenderAdmin(context.Command) && targetUser != null) return targetUser;
return context.Command.User;
2023-10-24 12:07:15 +00:00
}
2023-10-20 07:49:23 +00:00
}
}