IRC bot for interacting with mazerats.jerryaldrichiii.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.3 KiB

package roll
import (
"fmt"
"github.com/go-chat-bot/bot"
"github.com/spf13/viper"
"ratbot/plugins/web"
"strconv"
"strings"
)
func roll(command *bot.Cmd) (string, error) {
var diceRequest string
if len(command.Args) != 1 {
diceRequest = "2d6"
} else {
diceRequest = command.Args[0]
}
apiURL := viper.GetString("MAZE_RATS_API_URL") // From global viper state
if apiURL == "" {
return "", fmt.Errorf("Could not determine Maze Rats API URL from config")
}
url := fmt.Sprintf(
"%v/api/roll/%v",
apiURL, diceRequest,
)
rolls := []int{}
err := web.GetJSON(url, &rolls)
if err != nil {
return "", err
}
nick := command.User.Nick
var total int
for _, r := range rolls {
total = total + r
}
var message string
switch len(rolls) {
case 1:
message = fmt.Sprintf("%v rolls a %v!", nick, total)
case 2:
message = fmt.Sprintf(
"%v rolls: %v (total: %v)",
nick, rollString(rolls), total)
default:
message = fmt.Sprintf(
"%v rolls %v, results: %v (total: %v)",
nick, diceRequest, rollString(rolls), total)
}
return message, nil
}
func rollString(roll []int) string {
var s []string
for _, r := range roll {
s = append(s, strconv.Itoa(r))
}
return strings.Join(s, ", ")
}
func init() {
bot.RegisterCommand(
"roll",
"Performs a roll of XdY dice (rolls 2d6 by default)",
"[1d6]",
roll,
)
}