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
|
|
|
|
2016-03-30 23:04:49 +00:00
|
|
|
import (
|
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2016-03-30 23:04:49 +00:00
|
|
|
)
|
2013-01-22 14:45:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// NewDicePlugin 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
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(dp, bot.Message, dp.message)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message responds to the bot hook on recieving messages.
|
|
|
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
|
|
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *DicePlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2017-12-19 18:38:02 +00:00
|
|
|
if !message.Command {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:45:31 +00:00
|
|
|
channel := message.Channel
|
2017-12-19 18:38:02 +00:00
|
|
|
nDice := 0
|
|
|
|
sides := 0
|
|
|
|
|
|
|
|
if n, err := fmt.Sscanf(message.Body, "%dd%d", &nDice, &sides); n != 2 || err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sides < 2 || nDice < 1 || nDice > 20 {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, "You're a dick.")
|
2017-12-19 18:38:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
rolls := fmt.Sprintf("%s, you rolled: ", message.User.Name)
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, 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-02-05 19:41:38 +00:00
|
|
|
func (p *DicePlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.Bot.Send(bot.Message, message.Channel, "Roll dice using notation XdY. Try \"3d20\".")
|
|
|
|
return true
|
2013-05-08 00:08:18 +00:00
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *DicePlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|