mirror of https://github.com/velour/catbase.git
fuck: refactor by removal
This commit is contained in:
parent
4a0ba76b89
commit
1f954f301c
2
main.go
2
main.go
|
@ -39,7 +39,6 @@ import (
|
||||||
"github.com/velour/catbase/plugins/emojifyme"
|
"github.com/velour/catbase/plugins/emojifyme"
|
||||||
"github.com/velour/catbase/plugins/fact"
|
"github.com/velour/catbase/plugins/fact"
|
||||||
"github.com/velour/catbase/plugins/first"
|
"github.com/velour/catbase/plugins/first"
|
||||||
"github.com/velour/catbase/plugins/fuck"
|
|
||||||
"github.com/velour/catbase/plugins/git"
|
"github.com/velour/catbase/plugins/git"
|
||||||
"github.com/velour/catbase/plugins/impossible"
|
"github.com/velour/catbase/plugins/impossible"
|
||||||
"github.com/velour/catbase/plugins/inventory"
|
"github.com/velour/catbase/plugins/inventory"
|
||||||
|
@ -121,7 +120,6 @@ func main() {
|
||||||
b.AddPlugin(emojifyme.New(b))
|
b.AddPlugin(emojifyme.New(b))
|
||||||
b.AddPlugin(first.New(b))
|
b.AddPlugin(first.New(b))
|
||||||
b.AddPlugin(leftpad.New(b))
|
b.AddPlugin(leftpad.New(b))
|
||||||
b.AddPlugin(fuck.New(b))
|
|
||||||
b.AddPlugin(talker.New(b))
|
b.AddPlugin(talker.New(b))
|
||||||
b.AddPlugin(dice.New(b))
|
b.AddPlugin(dice.New(b))
|
||||||
b.AddPlugin(picker.New(b))
|
b.AddPlugin(picker.New(b))
|
||||||
|
|
|
@ -1,83 +0,0 @@
|
||||||
package fuck
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/chrissexton/gofuck"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
|
||||||
"github.com/velour/catbase/bot/msg"
|
|
||||||
"github.com/velour/catbase/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Fuck struct {
|
|
||||||
b bot.Bot
|
|
||||||
c *config.Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(b bot.Bot) *Fuck {
|
|
||||||
f := &Fuck{
|
|
||||||
b: b,
|
|
||||||
c: b.Config(),
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Register(f, bot.Help, f.help)
|
|
||||||
b.Register(f, bot.Message, f.message)
|
|
||||||
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Fuck) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
||||||
helpMsg := "Run brainfuck with: `!fuck ```<program>``` <stdin>`\n\nFor example:\n> !fuck ```,[.,]``` hello"
|
|
||||||
p.b.Send(c, bot.Message, message.Channel, helpMsg)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Fuck) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
||||||
lowerBody := strings.ToLower(message.Body)
|
|
||||||
if message.Command && strings.HasPrefix(lowerBody, "fuck") {
|
|
||||||
fields := strings.Split(message.Body, "```")
|
|
||||||
if len(fields) < 2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
pgm := fields[1]
|
|
||||||
in := strings.Join(fields[2:], "```")
|
|
||||||
|
|
||||||
log.Debug().
|
|
||||||
Str("pgm", pgm).
|
|
||||||
Str("in", in).
|
|
||||||
Msg("Asked to fuck hard")
|
|
||||||
|
|
||||||
stdin := bytes.NewBufferString(in)
|
|
||||||
stdout := &bytes.Buffer{}
|
|
||||||
|
|
||||||
m := gofuck.New(stdin, stdout)
|
|
||||||
|
|
||||||
m.InstructionLimit = p.c.GetInt("fuck.limit.instr", 100000)
|
|
||||||
if m.InstructionLimit < 1 {
|
|
||||||
m.InstructionLimit = 1
|
|
||||||
}
|
|
||||||
maxOut := p.c.GetInt("fuck.limit.output", 1000)
|
|
||||||
|
|
||||||
err := m.Run([]byte(pgm))
|
|
||||||
if stdout.Len() > maxOut {
|
|
||||||
stdout.Truncate(maxOut)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Error running program: %s", err))
|
|
||||||
if stdout.Len() > 0 {
|
|
||||||
p.b.Send(c, bot.Message, message.Channel,
|
|
||||||
fmt.Sprintf("Here's the output so far:\n%s", stdout.String()))
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
p.b.Send(c, bot.Message, message.Channel, stdout.String())
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
Loading…
Reference in New Issue