217 lines
7.5 KiB
C#
Raw Normal View History

2023-10-24 15:25:45 +02:00
using BiblioTech.Options;
using BiblioTech.Rewards;
2023-10-24 14:07:15 +02:00
namespace BiblioTech.Commands
{
2023-10-24 15:25:45 +02:00
public class AdminCommand : BaseCommand
2023-10-24 14:07:15 +02:00
{
2023-10-25 11:25:27 +02:00
private readonly ClearUserAssociationCommand clearCommand = new ClearUserAssociationCommand();
private readonly ReportCommand reportCommand = new ReportCommand();
private readonly WhoIsCommand whoIsCommand = new WhoIsCommand();
private readonly LogReplaceCommand logReplaceCommand;
2025-07-31 11:05:11 +02:00
private readonly BalanceCommand balanceCommand = new BalanceCommand();
2023-10-29 09:25:50 +01:00
2025-04-16 15:39:30 +02:00
public AdminCommand(CustomReplacement replacement)
2023-10-29 09:25:50 +01:00
{
logReplaceCommand = new LogReplaceCommand(replacement);
2023-10-29 09:25:50 +01:00
}
2023-10-24 15:25:45 +02:00
public override string Name => "admin";
public override string StartingMessage => "...";
public override string Description => "Admins only.";
2023-10-24 14:07:15 +02:00
2025-07-31 11:05:11 +02:00
public override CommandOption[] Options =>
[
2023-10-24 15:25:45 +02:00
clearCommand,
reportCommand,
2023-10-25 11:25:27 +02:00
whoIsCommand,
2025-07-31 11:05:11 +02:00
logReplaceCommand,
balanceCommand
];
2023-10-24 15:25:45 +02:00
protected override async Task Invoke(CommandContext context)
{
if (!IsSenderAdmin(context.Command))
2023-10-24 14:07:15 +02:00
{
2023-10-25 10:54:26 +02:00
await context.Followup("You're not an admin.");
2023-10-24 15:25:45 +02:00
return;
2023-10-24 14:07:15 +02:00
}
2023-10-24 15:25:45 +02:00
if (!IsInAdminChannel(context.Command))
{
2023-10-25 10:54:26 +02:00
await context.Followup("Please use admin commands only in the admin channel.");
return;
}
2023-10-24 15:25:45 +02:00
await clearCommand.CommandHandler(context);
await reportCommand.CommandHandler(context);
2023-10-25 11:25:27 +02:00
await whoIsCommand.CommandHandler(context);
await logReplaceCommand.CommandHandler(context);
2025-07-31 11:05:11 +02:00
await balanceCommand.CommandHandler(context);
2023-10-24 14:07:15 +02:00
}
2023-10-24 15:25:45 +02:00
public class ClearUserAssociationCommand : SubCommandOption
{
2023-10-25 11:25:27 +02:00
private readonly UserOption userOption = new UserOption("User to clear Eth address for.", true);
2023-10-24 15:25:45 +02:00
public ClearUserAssociationCommand()
: base("clear", "Admin only. Clears current Eth address for a user, allowing them to set a new one.")
{
}
2023-10-24 14:07:15 +02:00
2023-10-25 11:25:27 +02:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 14:07:15 +02:00
2023-10-24 15:25:45 +02:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 11:25:27 +02:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 15:25:45 +02:00
{
await context.Followup("Failed to get user ID");
2023-10-24 15:25:45 +02:00
return;
}
2023-10-25 11:25:27 +02:00
Program.UserRepo.ClearUserAssociatedAddress(user);
await context.Followup("Done.");
2023-10-24 15:25:45 +02:00
}
}
public class ReportCommand : SubCommandOption
2023-10-24 14:07:15 +02:00
{
2023-10-25 11:25:27 +02:00
private readonly UserOption userOption = new UserOption(
2023-10-24 15:25:45 +02:00
description: "User to report history for.",
isRequired: true);
public ReportCommand()
: base("report", "Admin only. Reports bot-interaction history for a user.")
{
}
2023-10-24 14:07:15 +02:00
2023-10-25 11:25:27 +02:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 15:25:45 +02:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 11:25:27 +02:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 15:25:45 +02:00
{
await context.Followup("Failed to get user ID");
2023-10-24 15:25:45 +02:00
return;
}
2023-11-09 10:13:34 +01:00
var report = Program.UserRepo.GetInteractionReport(user);
2023-11-08 13:33:48 +01:00
await context.Followup(report);
2023-10-24 15:25:45 +02:00
}
}
2023-10-25 11:25:27 +02:00
public class WhoIsCommand : SubCommandOption
{
private readonly UserOption userOption = new UserOption("User", isRequired: false);
private readonly EthAddressOption ethAddressOption = new EthAddressOption(isRequired: false);
public WhoIsCommand()
: base(name: "whois",
description: "Fetches info about a user or ethAddress in the testnet.")
{
}
public override CommandOption[] Options => new CommandOption[]
{
userOption,
ethAddressOption
};
protected override async Task onSubCommand(CommandContext context)
{
var user = userOption.GetUser(context);
var ethAddr = await ethAddressOption.Parse(context);
if (user != null)
{
await context.Followup(Program.UserRepo.GetUserReport(user));
2023-10-25 11:25:27 +02:00
}
if (ethAddr != null)
{
await context.Followup(Program.UserRepo.GetUserReport(ethAddr));
2023-10-25 11:25:27 +02:00
}
}
}
2023-10-29 09:25:50 +01:00
public class LogReplaceCommand : SubCommandOption
{
private readonly CustomReplacement replacement;
private readonly StringOption fromOption = new StringOption("from", "string to replace", true);
private readonly StringOption toOption = new StringOption("to", "string to replace with", false);
public LogReplaceCommand(CustomReplacement replacement)
: base(name: "logreplace",
description: "Replaces all 'from' with 'to' in ChainEvents.")
{
this.replacement = replacement;
}
public override CommandOption[] Options => new[] { fromOption, toOption };
protected override async Task onSubCommand(CommandContext context)
{
var from = await fromOption.Parse(context);
var to = await toOption.Parse(context);
if (string.IsNullOrEmpty(from))
{
await context.Followup("'from' not received");
return;
}
if (from.Length < 5)
{
await context.Followup("'from' must be length 5 or greater.");
return;
}
if (string.IsNullOrEmpty(to))
{
replacement.Remove(from);
await context.Followup($"Replace for '{from}' removed.");
}
else
{
if (to.Length < 5)
{
await context.Followup("'to' must be length 5 or greater.");
return;
}
replacement.Add(from, to);
await context.Followup($"Replace added '{from}' -->> '{to}'.");
}
}
}
2025-07-31 11:05:11 +02:00
public class BalanceCommand : SubCommandOption
{
public BalanceCommand()
: base(name: "balance",
description: "Shows ETH and TST balance of the bot.")
{
}
protected override async Task onSubCommand(CommandContext context)
{
if (Program.GethLink == null)
{
await context.Followup("Geth connection not available.");
return;
}
var node = Program.GethLink.Node;
var contracts = Program.GethLink.Contracts;
var address = node.CurrentAddress;
var eth = node.GetEthBalance();
var tst = contracts.GetTestTokenBalance(address);
await context.Followup($"Bot account ({address}) has {eth} and {tst}");
}
}
2023-10-24 14:07:15 +02:00
}
}