Merge pull request #138 from velour/quiet

admin: be quiet
This commit is contained in:
Chris Sexton 2019-01-25 10:09:44 -05:00 committed by GitHub
commit e161e9fece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/velour/catbase/bot"
@ -19,6 +20,8 @@ type AdminPlugin struct {
Bot bot.Bot
db *sqlx.DB
cfg *config.Config
quiet bool
}
// NewAdminPlugin creates a new AdminPlugin with the Plugin interface
@ -44,6 +47,10 @@ var forbiddenKeys = map[string]bool{
func (p *AdminPlugin) Message(message msg.Message) bool {
body := message.Body
if p.quiet {
return true
}
if len(body) > 0 && body[0] == '$' {
return p.handleVariables(message)
}
@ -52,6 +59,21 @@ func (p *AdminPlugin) Message(message msg.Message) bool {
return false
}
if strings.ToLower(body) == "shut up" {
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
log.Printf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
p.Bot.SendMessage(message.Channel, "Okay. I'll be back later.")
p.quiet = true
go func() {
select {
case <-time.After(dur):
p.quiet = false
log.Println("Waking up from nap.")
}
}()
return true
}
parts := strings.Split(body, " ")
if parts[0] == "set" && len(parts) > 2 && forbiddenKeys[parts[1]] {
p.Bot.SendMessage(message.Channel, "You cannot access that key")