admin: leave and come back

This commit is contained in:
Chris Sexton 2020-04-29 17:45:53 -04:00 committed by Chris Sexton
parent 1166fc732e
commit cee267dbb8
5 changed files with 26 additions and 2 deletions

View File

@ -50,6 +50,8 @@ type bot struct {
password string
passwordCreated time.Time
quiet bool
}
type EndPoint struct {
@ -259,3 +261,7 @@ func (b *bot) GetPassword() string {
}
return b.password
}
func (b *bot) SetQuiet(status bool) {
b.quiet = status
}

View File

@ -54,6 +54,9 @@ func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.M
// Send a message to the connection
func (b *bot) Send(conn Connector, kind Kind, args ...interface{}) (string, error) {
if b.quiet {
return "", nil
}
return conn.Send(kind, args...)
}

View File

@ -68,6 +68,7 @@ type Bot interface {
DefaultConnector() Connector
GetWebNavigation() []EndPoint
GetPassword() string
SetQuiet(bool)
}
// Connector represents a server connection to a chat service

View File

@ -33,6 +33,7 @@ func (mb *MockBot) Who(string) []user.User { return []user.User{} }
func (mb *MockBot) WhoAmI() string { return "tester" }
func (mb *MockBot) DefaultConnector() Connector { return nil }
func (mb *MockBot) GetPassword() string { return "12345" }
func (mb *MockBot) SetQuiet(bool) {}
func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, error) {
switch kind {
case Message:

View File

@ -55,7 +55,7 @@ var forbiddenKeys = map[string]bool{
func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool {
body := message.Body
if p.quiet {
if p.quiet && message.Body != "come back" {
return true
}
@ -67,6 +67,14 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
return false
}
if p.quiet && message.Body == "come back" {
p.quiet = false
p.bot.SetQuiet(false)
backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.")
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
return true
}
if strings.ToLower(body) == "reboot" {
p.bot.Send(conn, bot.Message, message.Channel, "brb")
log.Info().Msgf("Got reboot command")
@ -76,13 +84,18 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
if strings.ToLower(body) == "shut up" {
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
p.bot.Send(conn, bot.Message, message.Channel, "Okay. I'll be back later.")
leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.")
p.bot.Send(conn, bot.Message, message.Channel, leaveMsg)
p.quiet = true
p.bot.SetQuiet(true)
go func() {
select {
case <-time.After(dur):
p.quiet = false
p.bot.SetQuiet(false)
log.Info().Msg("Waking up from nap.")
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
}
}()
return true