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,22 +37,15 @@ 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 {
channel := message.Channel if !message.Command {
parts := strings.Fields(message.Body)
if len(parts) == 1 && message.Command {
var dice []string
dice = strings.Split(parts[0], "d")
if len(dice) == 2 {
// We actually have a die roll.
nDice, err := strconv.Atoi(dice[0])
if err != nil {
return false return false
} }
sides, err := strconv.Atoi(dice[1]) channel := message.Channel
if err != nil { nDice := 0
sides := 0
if n, err := fmt.Sscanf(message.Body, "%dd%d", &nDice, &sides); n != 2 || err != nil {
return false return false
} }
@ -76,9 +67,7 @@ func (p *DicePlugin) Message(message msg.Message) bool {
p.Bot.SendMessage(channel, rolls) p.Bot.SendMessage(channel, rolls)
return true return true
}
}
return false
} }
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.