From 6796a62a5ff56ed5af148f81d9b2008dc2541d8f Mon Sep 17 00:00:00 2001 From: Jacob Jonsson Date: Wed, 11 Mar 2026 01:35:37 +0100 Subject: [PATCH] fix(bot): don't substitute if there is no typo This commit fixes an issue where a substitution command replays a message because there was no needle to replace. --- src/bot.zig | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/bot.zig b/src/bot.zig index a6da684..aae3b94 100644 --- a/src/bot.zig +++ b/src/bot.zig @@ -90,6 +90,9 @@ pub const Bot = struct { command.author, targets, ) orelse return Error.NoMessage; + if (std.mem.count(u8, prev_msg.content, command.needle) == 0) { + return Error.NoMessage; + } const output = try std.mem.replaceOwned( u8, self.allocator, @@ -255,7 +258,7 @@ test "hear wraps" { try std.testing.expectEqual(1024, bot.backlog.len()); } -test "execute_substitution_no_previous_message" { +test "execute substitution no previous message" { var bot = try Bot.init(std.testing.allocator); defer bot.deinit(); const cmd = UserCommand{ .substitute = .{ @@ -270,7 +273,7 @@ test "execute_substitution_no_previous_message" { )); } -test "execute_substitution" { +test "execute substitution" { var bot = try Bot.init(std.testing.allocator); defer bot.deinit(); @@ -298,3 +301,28 @@ test "execute_substitution" { else => unreachable, } } + +test "execute substitution with no matching needle" { + var bot = try Bot.init(std.testing.allocator); + defer bot.deinit(); + + // hear original message + const msg = try Message.init_owned( + std.testing.allocator, + 1234, + "jassob", + "#test", + "original", + ); + bot.hear(msg); + + // execute substitution + const cmd = UserCommand{ + .substitute = .{ .author = "jassob", .needle = "something else", .replacement = "weird" }, + }; + try std.testing.expectError(Error.NoMessage, bot.execute( + &cmd, + null, + "#test", + )); +}