mirror of https://github.com/velour/catbase.git
inventory: refactor and fix regexes
This commit is contained in:
parent
001a04d186
commit
c802b52a3b
|
@ -21,43 +21,27 @@ type InventoryPlugin struct {
|
||||||
*sqlx.DB
|
*sqlx.DB
|
||||||
bot bot.Bot
|
bot bot.Bot
|
||||||
config *config.Config
|
config *config.Config
|
||||||
r1, r2, r3, r4, r5 *regexp.Regexp
|
handlers bot.HandlerTable
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new InventoryPlugin with the Plugin interface
|
// New creates a new InventoryPlugin with the Plugin interface
|
||||||
func New(b bot.Bot) *InventoryPlugin {
|
func New(b bot.Bot) *InventoryPlugin {
|
||||||
config := b.Config()
|
config := b.Config()
|
||||||
nick := config.Get("nick", "bot")
|
|
||||||
r1, err := regexp.Compile("take this (.+)")
|
|
||||||
checkerr(err)
|
|
||||||
r2, err := regexp.Compile("have a (.+)")
|
|
||||||
checkerr(err)
|
|
||||||
r3, err := regexp.Compile(fmt.Sprintf("puts (.+) in %s([^a-zA-Z].*)?", nick))
|
|
||||||
checkerr(err)
|
|
||||||
r4, err := regexp.Compile(fmt.Sprintf("gives %s (.+)", nick))
|
|
||||||
checkerr(err)
|
|
||||||
r5, err := regexp.Compile(fmt.Sprintf("gives (.+) to %s([^a-zA-Z].*)?", nick))
|
|
||||||
checkerr(err)
|
|
||||||
|
|
||||||
p := &InventoryPlugin{
|
p := &InventoryPlugin{
|
||||||
DB: b.DB(),
|
DB: b.DB(),
|
||||||
bot: b,
|
bot: b,
|
||||||
config: config,
|
config: config,
|
||||||
r1: r1, r2: r2, r3: r3, r4: r4, r5: r5,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b.RegisterFilter("$item", p.itemFilter)
|
b.RegisterFilter("$item", p.itemFilter)
|
||||||
b.RegisterFilter("$giveitem", p.giveItemFilter)
|
b.RegisterFilter("$giveitem", p.giveItemFilter)
|
||||||
|
|
||||||
_, err = p.DB.Exec(`create table if not exists inventory (
|
p.DB.MustExec(`create table if not exists inventory (
|
||||||
item string primary key
|
item string primary key
|
||||||
);`)
|
);`)
|
||||||
|
|
||||||
if err != nil {
|
p.register()
|
||||||
log.Fatal().Err(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Register(p, bot.Message, p.message)
|
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
@ -78,52 +62,48 @@ func (p *InventoryPlugin) itemFilter(input string) string {
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *InventoryPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (p *InventoryPlugin) register() {
|
||||||
m := message.Body
|
nick := p.config.Get("nick", "bot")
|
||||||
log.Debug().Msgf("inventory trying to read %+v", message)
|
|
||||||
if message.Command {
|
p.handlers = append(p.handlers, bot.HandlerSpec{Kind: bot.Message, IsCmd: true,
|
||||||
if strings.ToLower(m) == "inventory" {
|
Regex: regexp.MustCompile(`inventory`),
|
||||||
|
Handler: func(r bot.Request) bool {
|
||||||
items := p.getAll()
|
items := p.getAll()
|
||||||
say := "I'm not holding anything"
|
say := "I'm not holding anything"
|
||||||
if len(items) > 0 {
|
if len(items) > 0 {
|
||||||
log.Debug().Msgf("I think I have more than 0 items: %+v, len(items)=%d", items, len(items))
|
log.Debug().Msgf("I think I have more than 0 items: %+v, len(items)=%d", items, len(items))
|
||||||
say = fmt.Sprintf("I'm currently holding %s", strings.Join(items, ", "))
|
say = fmt.Sprintf("I'm currently holding %s", strings.Join(items, ", "))
|
||||||
}
|
}
|
||||||
p.bot.Send(c, bot.Message, message.Channel, say)
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, say)
|
||||||
return true
|
return true
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
h := func(r bot.Request) bool {
|
||||||
|
log.Debug().Msgf("Adding item: %s", r.Values["item"])
|
||||||
|
return p.addItem(r.Conn, r.Msg, r.Values["item"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// <Randall> Bucket[:,] take this (.+)
|
for _, r := range []*regexp.Regexp{
|
||||||
// <Randall> Bucket[:,] have a (.+)
|
regexp.MustCompile(`(?i)^take this (?P<item>.+)$`),
|
||||||
if matches := p.r1.FindStringSubmatch(m); len(matches) > 0 {
|
regexp.MustCompile(`(?i)^have a (?P<item>.+)$`),
|
||||||
log.Debug().Msgf("Found item to add: %s", matches[1])
|
} {
|
||||||
return p.addItem(c, message, matches[1])
|
p.handlers = append(p.handlers, bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
||||||
|
Regex: r,
|
||||||
|
Handler: h})
|
||||||
}
|
}
|
||||||
if matches := p.r2.FindStringSubmatch(m); len(matches) > 0 {
|
|
||||||
log.Debug().Msgf("Found item to add: %s", matches[1])
|
|
||||||
return p.addItem(c, message, matches[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if message.Action {
|
|
||||||
log.Debug().Msg("Inventory found an action")
|
|
||||||
// * Randall puts (.+) in Bucket([^a-zA-Z].*)?
|
|
||||||
// * Randall gives Bucket (.+)
|
|
||||||
// * Randall gives (.+) to Bucket([^a-zA-Z].*)?
|
|
||||||
|
|
||||||
if matches := p.r3.FindStringSubmatch(m); len(matches) > 0 {
|
for _, r := range []*regexp.Regexp{
|
||||||
log.Debug().Msgf("Found item to add: %s", matches[1])
|
regexp.MustCompile(fmt.Sprintf(`(?i)^puts (?P<item>\S+) in %s$`, nick)),
|
||||||
return p.addItem(c, message, matches[1])
|
regexp.MustCompile(fmt.Sprintf("(?i)^gives %s (?P<item>.+)$", nick)),
|
||||||
|
regexp.MustCompile(fmt.Sprintf(`(?i)^gives (?P<item>\S+) to %s$`, nick)),
|
||||||
|
} {
|
||||||
|
p.handlers = append(p.handlers, bot.HandlerSpec{Kind: bot.Action, IsCmd: false,
|
||||||
|
Regex: r,
|
||||||
|
Handler: h})
|
||||||
}
|
}
|
||||||
if matches := p.r4.FindStringSubmatch(m); len(matches) > 0 {
|
|
||||||
log.Debug().Msgf("Found item to add: %s", matches[1])
|
p.bot.RegisterTable(p, p.handlers)
|
||||||
return p.addItem(c, message, matches[1])
|
|
||||||
}
|
|
||||||
if matches := p.r5.FindStringSubmatch(m); len(matches) > 0 {
|
|
||||||
log.Debug().Msgf("Found item to add: %s", matches[1])
|
|
||||||
return p.addItem(c, message, matches[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *InventoryPlugin) removeRandom() string {
|
func (p *InventoryPlugin) removeRandom() string {
|
||||||
|
@ -219,9 +199,3 @@ func (p *InventoryPlugin) addItem(c bot.Connector, m msg.Message, i string) bool
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkerr(e error) {
|
|
||||||
if e != nil {
|
|
||||||
log.Error().Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue