diff --git a/cscsysbot.go b/cscsysbot.go index 0ccf25e..e970c31 100644 --- a/cscsysbot.go +++ b/cscsysbot.go @@ -15,6 +15,7 @@ import ( _ "git.uwaterloo.ca/csc/cscsysbot/plugins/club" _ "git.uwaterloo.ca/csc/cscsysbot/plugins/ping" _ "git.uwaterloo.ca/csc/cscsysbot/plugins/greetings" + _ "git.uwaterloo.ca/csc/cscsysbot/plugins/personality" // Backgrounds tasks _ "git.uwaterloo.ca/csc/cscsysbot/plugins/background" diff --git a/plugins/personality/personality.go b/plugins/personality/personality.go new file mode 100644 index 0000000..f980848 --- /dev/null +++ b/plugins/personality/personality.go @@ -0,0 +1,66 @@ +package personality + +import ( + "fmt" + "math/rand" + "regexp" + + "github.com/go-chat-bot/bot" +) + +const ( + botPattern = "(?i)cscsysbot" + thanksPattern = "(?i)(thanks?|thank you)" + negativePattern = "(?i)(you suck|u suck|you're (wrong|incorrect)|ur (wrong|incorrect)|that'?s (wrong|incorrect))" + whyPattern = "(?i)why" +) + +var ( + botRe = regexp.MustCompile(botPattern) + + thanksRe = regexp.MustCompile(thanksPattern) + thanksResponses = []string{ + "You're welcome %s", + "It's my pleasure %s", + "I'm here to serve you %s", + "No problem %s", + "Anytime %s", + } + + negativeRe = regexp.MustCompile(negativePattern) + negativeResponses = []string{ + "I'm sorry to dissapoint you %s", + "Sorry %s", + "I promise I'll do better next time %s", + } + + whyRe = regexp.MustCompile(whyPattern) + whyResponses = []string{ + "That is a good question %s", + "I don't know why %s", + "I'm unsure as well %s", + } +) + +func responseTemplate(re *regexp.Regexp, responses []string) (func(*bot.PassiveCmd) (string, error)) { + return func(command *bot.PassiveCmd) (string, error) { + if botRe.MatchString(command.Raw) && re.MatchString(command.Raw) { + return fmt.Sprintf(responses[rand.Intn(len(responses))], command.User.Nick), nil + } + return "", nil + } +} + +func init() { + bot.RegisterPassiveCommand( + "personality_thanks", + responseTemplate(thanksRe, thanksResponses)) + + bot.RegisterPassiveCommand( + "personality_negative", + responseTemplate(negativeRe, negativeResponses)) + + bot.RegisterPassiveCommand( + "personality_why", + responseTemplate(whyRe, whyResponses)) +}