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.
This commit is contained in:
Jacob Jonsson 2026-03-11 01:35:37 +01:00
parent 8b15398196
commit 6796a62a5f
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E

View file

@ -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",
));
}