mirror of https://github.com/velour/catbase.git
bot: add crash detector
This commit is contained in:
parent
a602917008
commit
e27288b3aa
|
@ -30,11 +30,23 @@ func (b *bot) MsgReceived(msg msg.Message) {
|
|||
}
|
||||
|
||||
go (func() {
|
||||
for _, name := range b.pluginOrdering {
|
||||
p := b.plugins[name]
|
||||
if p.Message(msg) {
|
||||
break
|
||||
done := make(chan bool, 1)
|
||||
go (func() {
|
||||
for _, name := range b.pluginOrdering {
|
||||
p := b.plugins[name]
|
||||
if p.Message(msg) {
|
||||
break
|
||||
}
|
||||
}
|
||||
done <- true
|
||||
})()
|
||||
select {
|
||||
case <-done:
|
||||
break
|
||||
case <-time.After(5 * time.Second):
|
||||
failMsg := fmt.Sprintf("I thought about it for a really long time, but ultimately I couldn't really comprehend the magnitude of this message...\n```%+v```\nI'm going to take a forever nap now. I'm so sorry that you won't know what great things could have come from that message. It was a really good message..", msg)
|
||||
b.SendMessage(msg.Channel, failMsg)
|
||||
log.Fatal(failMsg)
|
||||
}
|
||||
})()
|
||||
|
||||
|
|
2
main.go
2
main.go
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/velour/catbase/plugins/babbler"
|
||||
"github.com/velour/catbase/plugins/beers"
|
||||
"github.com/velour/catbase/plugins/counter"
|
||||
"github.com/velour/catbase/plugins/crash"
|
||||
"github.com/velour/catbase/plugins/dice"
|
||||
"github.com/velour/catbase/plugins/fact"
|
||||
"github.com/velour/catbase/plugins/leftpad"
|
||||
|
@ -62,6 +63,7 @@ func main() {
|
|||
b.AddHandler("zork", zork.New(b))
|
||||
b.AddHandler("rss", rss.New(b))
|
||||
b.AddHandler("reaction", reaction.New(b))
|
||||
b.AddHandler("crash", crash.New(b))
|
||||
// catches anything left, will always return true
|
||||
b.AddHandler("factoid", fact.New(b))
|
||||
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
|
||||
|
||||
// Crash contains the plugin that allows the bot to pad messages
|
||||
package crash
|
||||
|
||||
import (
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
"github.com/velour/catbase/config"
|
||||
)
|
||||
|
||||
type CrashPlugin struct {
|
||||
bot bot.Bot
|
||||
config *config.Config
|
||||
}
|
||||
|
||||
// New creates a new CrashPlugin with the Plugin interface
|
||||
func New(bot bot.Bot) *CrashPlugin {
|
||||
p := CrashPlugin{
|
||||
bot: bot,
|
||||
config: bot.Config(),
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p *CrashPlugin) Message(message msg.Message) bool {
|
||||
if !message.Command {
|
||||
return false
|
||||
}
|
||||
|
||||
if message.Body == "CRASH!" {
|
||||
p.bot.SendMessage(message.Channel, "Crashing...")
|
||||
for {
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *CrashPlugin) Event(e string, message msg.Message) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *CrashPlugin) BotMessage(message msg.Message) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *CrashPlugin) Help(e string, m []string) {
|
||||
}
|
||||
|
||||
func (p *CrashPlugin) RegisterWeb() *string {
|
||||
// nothing to register
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue