Skip to content

Commit ba43fd9

Browse files
committed
Adds admin option to set custom replacements
1 parent 01ee514 commit ba43fd9

File tree

6 files changed

+112
-11
lines changed

6 files changed

+112
-11
lines changed

Tools/BiblioTech/CommandHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ namespace BiblioTech
1010
public class CommandHandler
1111
{
1212
private readonly DiscordSocketClient client;
13+
private readonly CustomReplacement replacement;
1314
private readonly BaseCommand[] commands;
1415
private readonly ILog log;
1516

16-
public CommandHandler(ILog log, DiscordSocketClient client, params BaseCommand[] commands)
17+
public CommandHandler(ILog log, DiscordSocketClient client, CustomReplacement replacement, params BaseCommand[] commands)
1718
{
1819
this.client = client;
20+
this.replacement = replacement;
1921
this.commands = commands;
2022
this.log = log;
2123
client.Ready += Client_Ready;
@@ -31,7 +33,7 @@ private async Task Client_Ready()
3133
var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray();
3234
if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel");
3335
Program.AdminChecker.SetAdminChannel(adminChannels.First());
34-
Program.RoleDriver = new RoleDriver(client, log);
36+
Program.RoleDriver = new RoleDriver(client, log, replacement);
3537

3638
var builders = commands.Select(c =>
3739
{

Tools/BiblioTech/Commands/AdminCommand.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BiblioTech.Options;
2+
using BiblioTech.Rewards;
23

34
namespace BiblioTech.Commands
45
{
@@ -10,12 +11,14 @@ public class AdminCommand : BaseCommand
1011
private readonly AddSprCommand addSprCommand;
1112
private readonly ClearSprsCommand clearSprsCommand;
1213
private readonly GetSprCommand getSprCommand;
14+
private readonly LogReplaceCommand logReplaceCommand;
1315

14-
public AdminCommand(SprCommand sprCommand)
16+
public AdminCommand(SprCommand sprCommand, CustomReplacement replacement)
1517
{
1618
addSprCommand = new AddSprCommand(sprCommand);
1719
clearSprsCommand = new ClearSprsCommand(sprCommand);
1820
getSprCommand = new GetSprCommand(sprCommand);
21+
logReplaceCommand = new LogReplaceCommand(replacement);
1922
}
2023

2124
public override string Name => "admin";
@@ -29,7 +32,8 @@ public AdminCommand(SprCommand sprCommand)
2932
whoIsCommand,
3033
addSprCommand,
3134
clearSprsCommand,
32-
getSprCommand
35+
getSprCommand,
36+
logReplaceCommand
3337
};
3438

3539
protected override async Task Invoke(CommandContext context)
@@ -52,6 +56,7 @@ protected override async Task Invoke(CommandContext context)
5256
await addSprCommand.CommandHandler(context);
5357
await clearSprsCommand.CommandHandler(context);
5458
await getSprCommand.CommandHandler(context);
59+
await logReplaceCommand.CommandHandler(context);
5560
}
5661

5762
public class ClearUserAssociationCommand : SubCommandOption
@@ -194,7 +199,7 @@ protected override async Task onSubCommand(CommandContext context)
194199
}
195200
}
196201

197-
public class GetSprCommand: SubCommandOption
202+
public class GetSprCommand : SubCommandOption
198203
{
199204
private readonly SprCommand sprCommand;
200205

@@ -210,5 +215,56 @@ protected override async Task onSubCommand(CommandContext context)
210215
await context.Followup("SPRs: " + string.Join(", ", sprCommand.Get().Select(s => $"'{s}'")));
211216
}
212217
}
218+
219+
public class LogReplaceCommand : SubCommandOption
220+
{
221+
private readonly CustomReplacement replacement;
222+
private readonly StringOption fromOption = new StringOption("from", "string to replace", true);
223+
private readonly StringOption toOption = new StringOption("to", "string to replace with", false);
224+
225+
public LogReplaceCommand(CustomReplacement replacement)
226+
: base(name: "logreplace",
227+
description: "Replaces all occurances of 'from' with 'to' in ChainEvent messages. Leave 'to' empty to remove a replacement.")
228+
{
229+
this.replacement = replacement;
230+
}
231+
232+
public override CommandOption[] Options => new[] { fromOption, toOption };
233+
234+
protected override async Task onSubCommand(CommandContext context)
235+
{
236+
var from = await fromOption.Parse(context);
237+
var to = await toOption.Parse(context);
238+
239+
if (string.IsNullOrEmpty(from))
240+
{
241+
await context.Followup("'from' not received");
242+
return;
243+
}
244+
245+
if (from.Length < 5)
246+
{
247+
await context.Followup("'from' must be length 5 or greater.");
248+
return;
249+
}
250+
251+
if (string.IsNullOrEmpty(to))
252+
{
253+
replacement.Remove(from);
254+
await context.Followup($"Replace for '{from}' removed.");
255+
}
256+
else
257+
{
258+
if (to.Length < 5)
259+
{
260+
await context.Followup("'to' must be length 5 or greater.");
261+
return;
262+
}
263+
264+
replacement.Add(from, to);
265+
await context.Followup($"Replace added '{from}' -->> '{to}'.");
266+
}
267+
}
268+
}
213269
}
214270
}

Tools/BiblioTech/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace BiblioTech
1111
public class Program
1212
{
1313
private DiscordSocketClient client = null!;
14+
private readonly CustomReplacement replacement = new CustomReplacement();
1415

1516
public static Configuration Config { get; private set; } = null!;
1617
public static UserRepo UserRepo { get; } = new UserRepo();
@@ -73,13 +74,13 @@ private async Task StartDiscordBot()
7374
var notifyCommand = new NotifyCommand();
7475
var associateCommand = new UserAssociateCommand(notifyCommand);
7576
var sprCommand = new SprCommand();
76-
var handler = new CommandHandler(Log, client,
77+
var handler = new CommandHandler(Log, client, replacement,
7778
new GetBalanceCommand(associateCommand),
7879
new MintCommand(associateCommand),
7980
sprCommand,
8081
associateCommand,
8182
notifyCommand,
82-
new AdminCommand(sprCommand),
83+
new AdminCommand(sprCommand, replacement),
8384
new MarketCommand()
8485
);
8586

Tools/BiblioTech/Rewards/ChainEventsSender.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace BiblioTech.Rewards
66
public class ChainEventsSender
77
{
88
private readonly ILog log;
9-
private SocketTextChannel? eventsChannel;
9+
private readonly CustomReplacement replacement;
10+
private readonly SocketTextChannel? eventsChannel;
1011

11-
public ChainEventsSender(ILog log, SocketTextChannel? eventsChannel)
12+
public ChainEventsSender(ILog log, CustomReplacement replacement, SocketTextChannel? eventsChannel)
1213
{
1314
this.log = log;
15+
this.replacement = replacement;
1416
this.eventsChannel = eventsChannel;
1517
}
1618

@@ -43,6 +45,7 @@ await Task.Run(async () =>
4345
private string ApplyReplacements(UserData[] users, string msg)
4446
{
4547
var result = ApplyUserAddressReplacements(users, msg);
48+
result = ApplyCustomReplacements(result);
4649
return result;
4750
}
4851

@@ -60,5 +63,10 @@ private string ApplyUserAddressReplacements(UserData[] users, string msg)
6063

6164
return msg;
6265
}
66+
67+
private string ApplyCustomReplacements(string result)
68+
{
69+
return replacement.Apply(result);
70+
}
6371
}
6472
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace BiblioTech.Rewards
2+
{
3+
public class CustomReplacement
4+
{
5+
private readonly Dictionary<string, string> replacements = new Dictionary<string, string>();
6+
7+
public void Add(string from, string to)
8+
{
9+
if (replacements.ContainsKey(from))
10+
{
11+
replacements[from] = to;
12+
}
13+
else
14+
{
15+
replacements.Add(from, to);
16+
}
17+
}
18+
19+
public void Remove(string from)
20+
{
21+
replacements.Remove(from);
22+
}
23+
24+
public string Apply(string msg)
25+
{
26+
var result = msg;
27+
foreach (var pair in replacements)
28+
{
29+
result.Replace(pair.Key, pair.Value);
30+
}
31+
return result;
32+
}
33+
}
34+
}

Tools/BiblioTech/Rewards/RoleDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class RoleDriver : IDiscordRoleDriver
1414
private readonly ChainEventsSender eventsSender;
1515
private readonly RewardRepo repo = new RewardRepo();
1616

17-
public RoleDriver(DiscordSocketClient client, ILog log)
17+
public RoleDriver(DiscordSocketClient client, ILog log, CustomReplacement replacement)
1818
{
1919
this.client = client;
2020
this.log = log;
2121
rewardsChannel = GetChannel(Program.Config.RewardsChannelId);
22-
eventsSender = new ChainEventsSender(log, GetChannel(Program.Config.ChainEventsChannelId));
22+
eventsSender = new ChainEventsSender(log, replacement, GetChannel(Program.Config.ChainEventsChannelId));
2323
}
2424

2525
public async Task GiveRewards(GiveRewardsCommand rewards)

0 commit comments

Comments
 (0)