Adds admin option to set custom replacements

This commit is contained in:
Ben 2024-06-27 11:16:17 +02:00
parent 01ee514c73
commit ba43fd90c6
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
6 changed files with 112 additions and 11 deletions

View File

@ -10,12 +10,14 @@ namespace BiblioTech
public class CommandHandler public class CommandHandler
{ {
private readonly DiscordSocketClient client; private readonly DiscordSocketClient client;
private readonly CustomReplacement replacement;
private readonly BaseCommand[] commands; private readonly BaseCommand[] commands;
private readonly ILog log; private readonly ILog log;
public CommandHandler(ILog log, DiscordSocketClient client, params BaseCommand[] commands) public CommandHandler(ILog log, DiscordSocketClient client, CustomReplacement replacement, params BaseCommand[] commands)
{ {
this.client = client; this.client = client;
this.replacement = replacement;
this.commands = commands; this.commands = commands;
this.log = log; this.log = log;
client.Ready += Client_Ready; client.Ready += Client_Ready;
@ -31,7 +33,7 @@ namespace BiblioTech
var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray(); var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray();
if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel"); if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel");
Program.AdminChecker.SetAdminChannel(adminChannels.First()); Program.AdminChecker.SetAdminChannel(adminChannels.First());
Program.RoleDriver = new RoleDriver(client, log); Program.RoleDriver = new RoleDriver(client, log, replacement);
var builders = commands.Select(c => var builders = commands.Select(c =>
{ {

View File

@ -1,4 +1,5 @@
using BiblioTech.Options; using BiblioTech.Options;
using BiblioTech.Rewards;
namespace BiblioTech.Commands namespace BiblioTech.Commands
{ {
@ -10,12 +11,14 @@ namespace BiblioTech.Commands
private readonly AddSprCommand addSprCommand; private readonly AddSprCommand addSprCommand;
private readonly ClearSprsCommand clearSprsCommand; private readonly ClearSprsCommand clearSprsCommand;
private readonly GetSprCommand getSprCommand; private readonly GetSprCommand getSprCommand;
private readonly LogReplaceCommand logReplaceCommand;
public AdminCommand(SprCommand sprCommand) public AdminCommand(SprCommand sprCommand, CustomReplacement replacement)
{ {
addSprCommand = new AddSprCommand(sprCommand); addSprCommand = new AddSprCommand(sprCommand);
clearSprsCommand = new ClearSprsCommand(sprCommand); clearSprsCommand = new ClearSprsCommand(sprCommand);
getSprCommand = new GetSprCommand(sprCommand); getSprCommand = new GetSprCommand(sprCommand);
logReplaceCommand = new LogReplaceCommand(replacement);
} }
public override string Name => "admin"; public override string Name => "admin";
@ -29,7 +32,8 @@ namespace BiblioTech.Commands
whoIsCommand, whoIsCommand,
addSprCommand, addSprCommand,
clearSprsCommand, clearSprsCommand,
getSprCommand getSprCommand,
logReplaceCommand
}; };
protected override async Task Invoke(CommandContext context) protected override async Task Invoke(CommandContext context)
@ -52,6 +56,7 @@ namespace BiblioTech.Commands
await addSprCommand.CommandHandler(context); await addSprCommand.CommandHandler(context);
await clearSprsCommand.CommandHandler(context); await clearSprsCommand.CommandHandler(context);
await getSprCommand.CommandHandler(context); await getSprCommand.CommandHandler(context);
await logReplaceCommand.CommandHandler(context);
} }
public class ClearUserAssociationCommand : SubCommandOption public class ClearUserAssociationCommand : SubCommandOption
@ -210,5 +215,56 @@ namespace BiblioTech.Commands
await context.Followup("SPRs: " + string.Join(", ", sprCommand.Get().Select(s => $"'{s}'"))); await context.Followup("SPRs: " + string.Join(", ", sprCommand.Get().Select(s => $"'{s}'")));
} }
} }
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 occurances of 'from' with 'to' in ChainEvent messages. Leave 'to' empty to remove a replacement.")
{
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}'.");
}
}
}
} }
} }

View File

@ -11,6 +11,7 @@ namespace BiblioTech
public class Program public class Program
{ {
private DiscordSocketClient client = null!; private DiscordSocketClient client = null!;
private readonly CustomReplacement replacement = new CustomReplacement();
public static Configuration Config { get; private set; } = null!; public static Configuration Config { get; private set; } = null!;
public static UserRepo UserRepo { get; } = new UserRepo(); public static UserRepo UserRepo { get; } = new UserRepo();
@ -73,13 +74,13 @@ namespace BiblioTech
var notifyCommand = new NotifyCommand(); var notifyCommand = new NotifyCommand();
var associateCommand = new UserAssociateCommand(notifyCommand); var associateCommand = new UserAssociateCommand(notifyCommand);
var sprCommand = new SprCommand(); var sprCommand = new SprCommand();
var handler = new CommandHandler(Log, client, var handler = new CommandHandler(Log, client, replacement,
new GetBalanceCommand(associateCommand), new GetBalanceCommand(associateCommand),
new MintCommand(associateCommand), new MintCommand(associateCommand),
sprCommand, sprCommand,
associateCommand, associateCommand,
notifyCommand, notifyCommand,
new AdminCommand(sprCommand), new AdminCommand(sprCommand, replacement),
new MarketCommand() new MarketCommand()
); );

View File

@ -6,11 +6,13 @@ namespace BiblioTech.Rewards
public class ChainEventsSender public class ChainEventsSender
{ {
private readonly ILog log; private readonly ILog log;
private SocketTextChannel? eventsChannel; private readonly CustomReplacement replacement;
private readonly SocketTextChannel? eventsChannel;
public ChainEventsSender(ILog log, SocketTextChannel? eventsChannel) public ChainEventsSender(ILog log, CustomReplacement replacement, SocketTextChannel? eventsChannel)
{ {
this.log = log; this.log = log;
this.replacement = replacement;
this.eventsChannel = eventsChannel; this.eventsChannel = eventsChannel;
} }
@ -43,6 +45,7 @@ namespace BiblioTech.Rewards
private string ApplyReplacements(UserData[] users, string msg) private string ApplyReplacements(UserData[] users, string msg)
{ {
var result = ApplyUserAddressReplacements(users, msg); var result = ApplyUserAddressReplacements(users, msg);
result = ApplyCustomReplacements(result);
return result; return result;
} }
@ -60,5 +63,10 @@ namespace BiblioTech.Rewards
return msg; return msg;
} }
private string ApplyCustomReplacements(string result)
{
return replacement.Apply(result);
}
} }
} }

View File

@ -0,0 +1,34 @@
namespace BiblioTech.Rewards
{
public class CustomReplacement
{
private readonly Dictionary<string, string> replacements = new Dictionary<string, string>();
public void Add(string from, string to)
{
if (replacements.ContainsKey(from))
{
replacements[from] = to;
}
else
{
replacements.Add(from, to);
}
}
public void Remove(string from)
{
replacements.Remove(from);
}
public string Apply(string msg)
{
var result = msg;
foreach (var pair in replacements)
{
result.Replace(pair.Key, pair.Value);
}
return result;
}
}
}

View File

@ -14,12 +14,12 @@ namespace BiblioTech.Rewards
private readonly ChainEventsSender eventsSender; private readonly ChainEventsSender eventsSender;
private readonly RewardRepo repo = new RewardRepo(); private readonly RewardRepo repo = new RewardRepo();
public RoleDriver(DiscordSocketClient client, ILog log) public RoleDriver(DiscordSocketClient client, ILog log, CustomReplacement replacement)
{ {
this.client = client; this.client = client;
this.log = log; this.log = log;
rewardsChannel = GetChannel(Program.Config.RewardsChannelId); rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
eventsSender = new ChainEventsSender(log, GetChannel(Program.Config.ChainEventsChannelId)); eventsSender = new ChainEventsSender(log, replacement, GetChannel(Program.Config.ChainEventsChannelId));
} }
public async Task GiveRewards(GiveRewardsCommand rewards) public async Task GiveRewards(GiveRewardsCommand rewards)