From 371ce2de16902718af12adaa3f2e4381d12ce77e Mon Sep 17 00:00:00 2001 From: Liam Stanley Date: Wed, 7 Jun 2017 05:03:36 -0400 Subject: [PATCH] implement Reply, Replyf, ReplyTo and ReplyTof --- commands.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/commands.go b/commands.go index 8ecf340..6d02df2 100644 --- a/commands.go +++ b/commands.go @@ -140,6 +140,51 @@ func (cmd *Commands) Messagef(target, format string, a ...interface{}) error { return cmd.Message(target, fmt.Sprintf(format, a...)) } +var ErrInvalidSource = errors.New("event has nil or invalid source address") + +// Reply sends a reply to channel or user, based on where the supplied event +// originated from. See also ReplyTo(). +func (cmd *Commands) Reply(event Event, message string) error { + if event.Source == nil { + return ErrInvalidSource + } + + if len(event.Params) > 0 && IsValidChannel(event.Params[0]) { + return cmd.Message(event.Params[0], message) + } + + return cmd.Message(event.Source.Name, message) +} + +// Replyf sends a reply to channel or user with a format string, based on +// where the supplied event originated from. See also ReplyTof(). +func (cmd *Commands) Replyf(event Event, format string, a ...interface{}) error { + return cmd.Reply(event, fmt.Sprintf(format, a...)) +} + +// ReplyTo sends a reply to a channel or user, based on where the supplied +// event originated from. ReplyTo(), when originating from a channel will +// default to replying with ", ". See also Reply(). +func (cmd *Commands) ReplyTo(event Event, message string) error { + if event.Source == nil { + return ErrInvalidSource + } + + if len(event.Params) > 0 && IsValidChannel(event.Params[0]) { + return cmd.Message(event.Params[0], event.Source.Name+", "+message) + } + + return cmd.Message(event.Source.Name, message) +} + +// ReplyTof sends a reply to a channel or user with a format string, based +// on where the supplied event originated from. ReplyTo(), when originating +// from a channel will default to replying with ", ". See +// also Replyf(). +func (cmd *Commands) ReplyTof(event Event, format string, a ...interface{}) error { + return cmd.ReplyTo(event, fmt.Sprintf(format, a...)) +} + // Action sends a PRIVMSG ACTION (/me) to target (either channel, service, // or user). func (cmd *Commands) Action(target, message string) error {