2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-24 17:32:40 +00:00
|
|
|
package dice
|
2013-01-22 14:45:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2021-02-01 20:57:34 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
2013-01-22 14:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a dice plugin to serve as an example and quick copy/paste for new plugins.
|
|
|
|
|
|
|
|
type DicePlugin struct {
|
2016-03-30 14:00:20 +00:00
|
|
|
Bot bot.Bot
|
2013-01-22 14:45:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:57:34 +00:00
|
|
|
// New creates a new DicePlugin with the Plugin interface
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *DicePlugin {
|
|
|
|
dp := &DicePlugin{
|
|
|
|
Bot: b,
|
2013-01-22 14:45:31 +00:00
|
|
|
}
|
2021-02-01 20:57:34 +00:00
|
|
|
b.RegisterRegexCmd(dp, bot.Message, rollRegex, dp.rollCmd)
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(dp, bot.Help, dp.help)
|
|
|
|
return dp
|
2013-01-22 14:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rollDie(sides int) int {
|
|
|
|
return rand.Intn(sides) + 1
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:57:34 +00:00
|
|
|
var rollRegex = regexp.MustCompile(`^(?P<number>\d+)d(?P<sides>\d+)$`)
|
2017-12-19 18:38:02 +00:00
|
|
|
|
2021-02-01 20:57:34 +00:00
|
|
|
func (p *DicePlugin) rollCmd(r bot.Request) bool {
|
|
|
|
nDice, _ := strconv.Atoi(r.Values["number"])
|
|
|
|
sides, _ := strconv.Atoi(r.Values["sides"])
|
2017-12-19 18:38:02 +00:00
|
|
|
|
|
|
|
if sides < 2 || nDice < 1 || nDice > 20 {
|
2021-02-01 20:57:34 +00:00
|
|
|
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You're a dick.")
|
2017-12-19 18:38:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:57:34 +00:00
|
|
|
rolls := fmt.Sprintf("%s, you rolled: ", r.Msg.User.Name)
|
2017-12-19 18:38:02 +00:00
|
|
|
|
|
|
|
for i := 0; i < nDice; i++ {
|
|
|
|
rolls = fmt.Sprintf("%s %d", rolls, rollDie(sides))
|
|
|
|
if i != nDice-1 {
|
|
|
|
rolls = fmt.Sprintf("%s,", rolls)
|
|
|
|
} else {
|
|
|
|
rolls = fmt.Sprintf("%s.", rolls)
|
2013-01-22 14:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 18:38:02 +00:00
|
|
|
|
2021-02-01 20:57:34 +00:00
|
|
|
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, rolls)
|
2017-12-19 18:38:02 +00:00
|
|
|
return true
|
2013-01-22 14:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *DicePlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.Bot.Send(c, bot.Message, message.Channel, "Roll dice using notation XdY. Try \"3d20\".")
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2013-05-08 00:08:18 +00:00
|
|
|
}
|