2023-10-20 07:49:23 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Discord;
|
2023-10-22 08:38:46 +00:00
|
|
|
|
using BiblioTech.Commands;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
|
|
|
|
|
namespace BiblioTech
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseCommand
|
|
|
|
|
{
|
|
|
|
|
public abstract string Name { get; }
|
2023-10-20 09:20:38 +00:00
|
|
|
|
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;
|
2023-10-20 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await command.RespondAsync(StartingMessage);
|
|
|
|
|
await Invoke(command);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await command.FollowupAsync("Something failed while trying to do that...");
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
}
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract Task Invoke(SocketSlashCommand command);
|
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 ulong GetUserId(UserOption userOption, SocketSlashCommand command)
|
|
|
|
|
{
|
|
|
|
|
var targetUser = userOption.GetOptionUserId(command);
|
|
|
|
|
if (IsSenderAdmin(command) && targetUser != null) return targetUser.Value;
|
|
|
|
|
return command.User.Id;
|
|
|
|
|
}
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CommandOption
|
|
|
|
|
{
|
2023-10-22 08:10:52 +00:00
|
|
|
|
public CommandOption(string name, string description, ApplicationCommandOptionType type, bool isRequired)
|
2023-10-20 07:49:23 +00:00
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Description = description;
|
|
|
|
|
Type = type;
|
2023-10-22 08:10:52 +00:00
|
|
|
|
IsRequired = isRequired;
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string Description { get; }
|
|
|
|
|
public ApplicationCommandOptionType Type { get; }
|
2023-10-22 08:10:52 +00:00
|
|
|
|
public bool IsRequired { get; }
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|