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:
parent
8b15398196
commit
6796a62a5f
1 changed files with 30 additions and 2 deletions
32
src/bot.zig
32
src/bot.zig
|
|
@ -90,6 +90,9 @@ pub const Bot = struct {
|
||||||
command.author,
|
command.author,
|
||||||
targets,
|
targets,
|
||||||
) orelse return Error.NoMessage;
|
) orelse return Error.NoMessage;
|
||||||
|
if (std.mem.count(u8, prev_msg.content, command.needle) == 0) {
|
||||||
|
return Error.NoMessage;
|
||||||
|
}
|
||||||
const output = try std.mem.replaceOwned(
|
const output = try std.mem.replaceOwned(
|
||||||
u8,
|
u8,
|
||||||
self.allocator,
|
self.allocator,
|
||||||
|
|
@ -255,7 +258,7 @@ test "hear wraps" {
|
||||||
try std.testing.expectEqual(1024, bot.backlog.len());
|
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);
|
var bot = try Bot.init(std.testing.allocator);
|
||||||
defer bot.deinit();
|
defer bot.deinit();
|
||||||
const cmd = UserCommand{ .substitute = .{
|
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);
|
var bot = try Bot.init(std.testing.allocator);
|
||||||
defer bot.deinit();
|
defer bot.deinit();
|
||||||
|
|
||||||
|
|
@ -298,3 +301,28 @@ test "execute_substitution" {
|
||||||
else => unreachable,
|
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",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue