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 (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
)
|
2013-01-22 14:45:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2016-03-30 14:00:20 +00:00
|
|
|
func New(bot bot.Bot) *DicePlugin {
|
2016-03-30 23:04:49 +00:00
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
|
2013-01-22 14:45:31 +00:00
|
|
|
return &DicePlugin{
|
|
|
|
Bot: bot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (p *DicePlugin) Message(message bot.Message) bool {
|
|
|
|
channel := message.Channel
|
|
|
|
parts := strings.Fields(message.Body)
|
|
|
|
|
2016-03-30 23:04:49 +00:00
|
|
|
if len(parts) == 1 && message.Command {
|
2013-01-22 14:45:31 +00:00
|
|
|
var dice []string
|
2016-03-30 23:04:49 +00:00
|
|
|
dice = strings.Split(parts[0], "d")
|
2013-01-22 14:45:31 +00:00
|
|
|
|
|
|
|
if len(dice) == 2 {
|
|
|
|
// We actually have a die roll.
|
2013-01-22 19:54:27 +00:00
|
|
|
nDice, err := strconv.Atoi(dice[0])
|
2013-01-22 21:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
2013-01-22 19:54:27 +00:00
|
|
|
}
|
2013-01-22 21:16:27 +00:00
|
|
|
|
2013-01-22 19:54:27 +00:00
|
|
|
sides, err := strconv.Atoi(dice[1])
|
2013-01-22 21:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sides < 2 || nDice < 1 || nDice > 20 {
|
2013-01-22 20:06:14 +00:00
|
|
|
p.Bot.SendMessage(channel, "You're a dick.")
|
|
|
|
return true
|
2013-01-22 19:54:27 +00:00
|
|
|
}
|
2013-01-22 20:06:14 +00:00
|
|
|
|
2013-01-22 14:49:32 +00:00
|
|
|
rolls := fmt.Sprintf("%s, you rolled: ", message.User.Name)
|
2013-01-22 14:45:31 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Bot.SendMessage(channel, rolls)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *DicePlugin) Help(channel string, parts []string) {
|
|
|
|
p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
|
|
|
func (p *DicePlugin) Event(kind string, message bot.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Handler for bot's own messages
|
|
|
|
func (p *DicePlugin) BotMessage(message bot.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *DicePlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|