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; }
|
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; }
|
2023-11-09 09:37:57 +00:00
|
|
|
|
public virtual CommandOption[] Options => Array.Empty<CommandOption>();
|
2023-10-20 07:49:23 +00:00
|
|
|
|
|
|
|
|
|
public async Task SlashCommandHandler(SocketSlashCommand command)
|
|
|
|
|
{
|
|
|
|
|
if (command.CommandName != Name) return;
|
2023-10-20 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-12-18 10:27:28 +00:00
|
|
|
|
Program.Log.Log($"Responding to '{Name}'");
|
2023-11-02 14:04:53 +00:00
|
|
|
|
var context = new CommandContext(command, command.Data.Options);
|
|
|
|
|
await command.RespondAsync(StartingMessage, ephemeral: IsEphemeral(context));
|
|
|
|
|
await Invoke(context);
|
2023-10-20 09:20:38 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-12-18 10:27:28 +00:00
|
|
|
|
var msg = "Failed with exception: " + ex;
|
2024-09-30 08:59:22 +00:00
|
|
|
|
Program.Log.Error(msg);
|
|
|
|
|
|
|
|
|
|
if (!IsInAdminChannel(command))
|
2023-11-09 09:13:34 +00:00
|
|
|
|
{
|
2024-09-30 08:59:22 +00:00
|
|
|
|
await command.FollowupAsync("Something failed while trying to do that... (error details posted in admin channel)", ephemeral: true);
|
2023-11-09 09:13:34 +00:00
|
|
|
|
}
|
2024-09-30 08:59:22 +00:00
|
|
|
|
await Program.AdminChecker.SendInAdminChannel(msg);
|
2023-10-20 09:20:38 +00:00
|
|
|
|
}
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 14:04:53 +00:00
|
|
|
|
private bool IsEphemeral(CommandContext context)
|
|
|
|
|
{
|
|
|
|
|
if (IsSenderAdmin(context.Command) && IsInAdminChannel(context.Command)) return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 08:38:21 +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
|
|
|
|
}
|
2024-09-30 08:59:22 +00:00
|
|
|
|
|
|
|
|
|
protected string Mention(SocketUser user)
|
|
|
|
|
{
|
|
|
|
|
return Mention(user.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string Mention(IUser user)
|
|
|
|
|
{
|
|
|
|
|
return Mention(user.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string Mention(ulong userId)
|
|
|
|
|
{
|
|
|
|
|
return $"<@{userId}>";
|
|
|
|
|
}
|
2023-10-20 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|