refactor(bot): introduce response type
The response type holds represents the way a bot can respond to a message it executes and allows us to move the IRC dependency out of bot and into only the main module.
This commit is contained in:
parent
4f2b9cbce2
commit
4d1f22194c
3 changed files with 50 additions and 54 deletions
17
src/main.zig
17
src/main.zig
|
|
@ -6,6 +6,7 @@ const Error = zigeru.bot.Error;
|
|||
const UserCommand = zigeru.commands.UserCommand;
|
||||
const AdminCommand = zigeru.commands.AdminCommand;
|
||||
const BotMessage = zigeru.bot.Message;
|
||||
const BotResponse = zigeru.bot.Response;
|
||||
const zircon = @import("zircon");
|
||||
|
||||
var debug_allocator = std.heap.DebugAllocator(.{}).init;
|
||||
|
|
@ -72,10 +73,10 @@ pub const BotAdapter = struct {
|
|||
);
|
||||
const nick = if (msg.prefix) |prefix| if (prefix.nick) |nick| nick else "unknown" else "unknown";
|
||||
if (UserCommand.parse(nick, msg.text)) |cmd| {
|
||||
return self.bot.execute(&cmd, msg.prefix, msg.targets) catch |err| return report_error(err);
|
||||
return toIRC(self.bot.execute(&cmd, msg.targets) catch |err| return report_error(err));
|
||||
}
|
||||
if (AdminCommand.parse(msg.text)) |cmd| {
|
||||
return self.bot.execute_admin(&cmd, msg.prefix, "#eru-admin") catch |err| return report_error(err);
|
||||
return toIRC(self.bot.execute_admin(&cmd, "#eru-admin") catch |err| return report_error(err));
|
||||
}
|
||||
const bot_msg = BotMessage.init_owned(
|
||||
self.allocator,
|
||||
|
|
@ -120,6 +121,18 @@ pub const BotAdapter = struct {
|
|||
}
|
||||
};
|
||||
|
||||
/// toIRC converts a bot response and converts it to a IRC message.
|
||||
fn toIRC(response: BotResponse) zircon.Message {
|
||||
switch (response) {
|
||||
.join => |join| return .{
|
||||
.JOIN = .{ .prefix = null, .channels = join.channels },
|
||||
},
|
||||
.privmsg => |msg| return .{
|
||||
.PRIVMSG = .{ .prefix = null, .targets = msg.targets, .text = msg.text },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test "substitute" {
|
||||
var bot_adapter = try BotAdapter.init(std.testing.allocator);
|
||||
defer bot_adapter.deinit();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue