dice: clean up

This commit is contained in:
Chris Sexton 2017-12-19 13:38:02 -05:00
parent 132fdd29be
commit 6bc72b93d1
1 changed files with 24 additions and 35 deletions

View File

@ -12,8 +12,6 @@ import (
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"strconv"
"strings"
) )
// This is a dice plugin to serve as an example and quick copy/paste for new plugins. // This is a dice plugin to serve as an example and quick copy/paste for new plugins.
@ -39,46 +37,37 @@ func rollDie(sides int) int {
// This function returns true if the plugin responds in a meaningful way to the users message. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *DicePlugin) Message(message msg.Message) bool { func (p *DicePlugin) Message(message msg.Message) bool {
if !message.Command {
return false
}
channel := message.Channel channel := message.Channel
parts := strings.Fields(message.Body) nDice := 0
sides := 0
if len(parts) == 1 && message.Command { if n, err := fmt.Sscanf(message.Body, "%dd%d", &nDice, &sides); n != 2 || err != nil {
var dice []string return false
dice = strings.Split(parts[0], "d") }
if len(dice) == 2 { if sides < 2 || nDice < 1 || nDice > 20 {
// We actually have a die roll. p.Bot.SendMessage(channel, "You're a dick.")
nDice, err := strconv.Atoi(dice[0]) return true
if err != nil { }
return false
}
sides, err := strconv.Atoi(dice[1]) rolls := fmt.Sprintf("%s, you rolled: ", message.User.Name)
if err != nil {
return false
}
if sides < 2 || nDice < 1 || nDice > 20 { for i := 0; i < nDice; i++ {
p.Bot.SendMessage(channel, "You're a dick.") rolls = fmt.Sprintf("%s %d", rolls, rollDie(sides))
return true if i != nDice-1 {
} rolls = fmt.Sprintf("%s,", rolls)
} else {
rolls := fmt.Sprintf("%s, you rolled: ", message.User.Name) rolls = fmt.Sprintf("%s.", rolls)
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
p.Bot.SendMessage(channel, rolls)
return true
} }
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.