Skip to content

Commit af40766

Browse files
committed
fix !blist
1 parent c85f01b commit af40766

4 files changed

Lines changed: 90 additions & 9 deletions

File tree

MedalBOT/BotContext.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public class BotContext
5353
public Dictionary<string, DateTime> TimedMutes { get; set; }
5454
= new(StringComparer.OrdinalIgnoreCase);
5555

56+
// Track pending service requests (command ID → requester info)
57+
public Dictionary<string, (string RequesterNick, bool IsDiscord)> PendingServiceRequests { get; set; }
58+
= new();
59+
5660
private readonly object _voicedLock = new();
5761
private readonly object _muteLock = new();
5862
private const string MutedIdsFile = "muted_ids.txt";
@@ -251,5 +255,25 @@ private string ExtractIdent(string hostmask)
251255
if (at <= 0) return null;
252256
return hostmask.Substring(0, at);
253257
}
258+
259+
public void TrackServiceRequest(string commandId, string requesterNick, bool isDiscord)
260+
{
261+
PendingServiceRequests[commandId] = (requesterNick, isDiscord);
262+
}
263+
264+
public bool GetServiceRequest(string commandId, out string requesterNick, out bool isDiscord)
265+
{
266+
requesterNick = null;
267+
isDiscord = false;
268+
269+
if (PendingServiceRequests.TryGetValue(commandId, out var request))
270+
{
271+
requesterNick = request.RequesterNick;
272+
isDiscord = request.IsDiscord;
273+
PendingServiceRequests.Remove(commandId);
274+
return true;
275+
}
276+
return false;
277+
}
254278
}
255279
}

MedalBOT/Commands/ChanServCommand.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ChanServCommand : ICommand
1717
return (true, "You must be an admin to use ChanServ commands.");
1818

1919
if (message.StartsWith("!blist"))
20-
return HandleBanList(ctx, senderNick);
20+
return HandleBanList(ctx, senderNick, false);
2121

2222
if (message.StartsWith("!addban"))
2323
return HandleAddBan(ctx, senderNick, message);
@@ -31,10 +31,36 @@ public class ChanServCommand : ICommand
3131
return (false, null);
3232
}
3333

34-
private (bool, string) HandleBanList(BotContext ctx, string senderNick)
34+
public (bool handled, string response) ProcessDiscord(BotContext ctx, string senderNick, string message)
3535
{
36+
if (!message.StartsWith("!blist") && !message.StartsWith("!addban") &&
37+
!message.StartsWith("!delban") && !message.StartsWith("!tb"))
38+
return (false, null);
39+
40+
if (!ctx.Admins.Contains(senderNick))
41+
return (true, "You must be an admin to use ChanServ commands.");
42+
43+
if (message.StartsWith("!blist"))
44+
return HandleBanList(ctx, senderNick, true);
45+
46+
if (message.StartsWith("!addban"))
47+
return HandleAddBan(ctx, senderNick, message);
48+
49+
if (message.StartsWith("!delban"))
50+
return HandleDelBan(ctx, senderNick, message);
51+
52+
if (message.StartsWith("!tb"))
53+
return HandleTimedBan(ctx, senderNick, message);
54+
55+
return (false, null);
56+
}
57+
58+
private (bool, string) HandleBanList(BotContext ctx, string senderNick, bool isDiscord)
59+
{
60+
string commandId = $"blist_{DateTime.UtcNow.Ticks}";
61+
ctx.TrackServiceRequest(commandId, senderNick, isDiscord);
3662
ctx.Writer?.WriteLine($"PRIVMSG ChanServ :bans {ctx.Channel}");
37-
ctx.Logger?.Log($"[CHANSERV] {senderNick} requested banlist for {ctx.Channel}");
63+
ctx.Logger?.Log($"[CHANSERV] {senderNick} requested banlist for {ctx.Channel} (ID: {commandId})");
3864
return (false, null);
3965
}
4066

@@ -82,3 +108,5 @@ public class ChanServCommand : ICommand
82108
}
83109
}
84110

111+
112+

MedalBOT/Program.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,32 @@ static async Task Main()
140140
ParseWhoResponse(ctx, line);
141141
}
142142

143-
// Relay ChanServ/SpamServ NOTICE replies to channel
143+
// Relay ChanServ/SpamServ NOTICE replies to channel or Discord
144144
if (line.Contains("NOTICE") && line.Contains("ChanServ"))
145145
{
146146
string message = MessageParser.GetMessage(line);
147147
if (!string.IsNullOrWhiteSpace(message))
148148
{
149-
writer.WriteLine($"PRIVMSG {ctx.Channel} :{message}");
150-
ctx.Logger?.Log($"[CHANSERV RELAY] {message}");
149+
// Extract last command ID - simple FIFO approach for recent requests
150+
var lastRequest = ctx.PendingServiceRequests.LastOrDefault();
151+
if (lastRequest.Key != null && ctx.GetServiceRequest(lastRequest.Key, out string requesterNick, out bool isDiscord))
152+
{
153+
ctx.Logger?.Log($"[CHANSERV RELAY] Sending to {(isDiscord ? "Discord" : "IRC")}: {message}");
154+
if (isDiscord)
155+
{
156+
await ctx.Discord?.SendMessage($"**{requesterNick}** - {message}");
157+
}
158+
else
159+
{
160+
writer.WriteLine($"PRIVMSG {ctx.Channel} :{message}");
161+
}
162+
}
163+
else
164+
{
165+
// No pending request, relay to IRC channel
166+
writer.WriteLine($"PRIVMSG {ctx.Channel} :{message}");
167+
ctx.Logger?.Log($"[CHANSERV RELAY] {message}");
168+
}
151169
}
152170
}
153171

MedalBOT/Services/DiscordBotService.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public class DiscordBotService
1313
private ISocketMessageChannel _channel;
1414
private readonly ulong _channelId;
1515
private SayCommand _sayCommand;
16+
private ChanServCommand _chanServCommand;
1617

1718
public DiscordBotService(BotContext ctx, ulong channelId)
1819
{
1920
_ctx = ctx;
2021
_channelId = channelId;
2122
_sayCommand = new SayCommand();
23+
_chanServCommand = new ChanServCommand();
2224

2325
_client = new DiscordSocketClient(new DiscordSocketConfig
2426
{
@@ -58,11 +60,20 @@ private async Task OnMessageReceived(SocketMessage msg)
5860

5961
if (content.StartsWith("!"))
6062
{
63+
// Check for ChanServ commands from Discord
64+
var (handled, response) = _chanServCommand.ProcessDiscord(_ctx, msg.Author.Username, content);
65+
if (handled && !string.IsNullOrEmpty(response))
66+
{
67+
await msg.Channel.SendMessageAsync(response);
68+
return;
69+
}
70+
71+
// Regular command processing
6172
var commandManager = new CommandManager();
62-
string response = commandManager.TryProcess(_ctx, msg.Author.Username, content, content);
73+
string cmdResponse = commandManager.TryProcess(_ctx, msg.Author.Username, content, content);
6374

64-
if (!string.IsNullOrEmpty(response))
65-
await msg.Channel.SendMessageAsync(response);
75+
if (!string.IsNullOrEmpty(cmdResponse))
76+
await msg.Channel.SendMessageAsync(cmdResponse);
6677
}
6778

6879
if (!_ctx.RelayDiscordToIrc) return;

0 commit comments

Comments
 (0)