Add dice-roll plugin.
parent
e17f1f4f98
commit
dee7a7cd73
|
@ -337,6 +337,78 @@ var eventTests = []struct {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "roll multiple",
|
||||
in: irc.Message{
|
||||
Command: "PRIVMSG",
|
||||
Trailing: ":roll 1 3",
|
||||
Params: []string{"#testchan-1"},
|
||||
Prefix: &irc.Prefix{
|
||||
Name: "idontexist",
|
||||
},
|
||||
},
|
||||
expectedOut: []irc.Message{
|
||||
{
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{"#testchan-1"},
|
||||
Trailing: "0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "roll single",
|
||||
in: irc.Message{
|
||||
Command: "PRIVMSG",
|
||||
Trailing: ":roll 1",
|
||||
Params: []string{"#testchan-1"},
|
||||
Prefix: &irc.Prefix{
|
||||
Name: "idontexist",
|
||||
},
|
||||
},
|
||||
expectedOut: []irc.Message{
|
||||
{
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{"#testchan-1"},
|
||||
Trailing: "0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "roll too many arguments",
|
||||
in: irc.Message{
|
||||
Command: "PRIVMSG",
|
||||
Trailing: ":roll 0 1 2",
|
||||
Params: []string{"#testchan-1"},
|
||||
Prefix: &irc.Prefix{
|
||||
Name: "idontexist",
|
||||
},
|
||||
},
|
||||
expectedOut: []irc.Message{
|
||||
{
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{"#testchan-1"},
|
||||
Trailing: "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "roll 0",
|
||||
in: irc.Message{
|
||||
Command: "PRIVMSG",
|
||||
Trailing: ":roll 0",
|
||||
Params: []string{"#testchan-1"},
|
||||
Prefix: &irc.Prefix{
|
||||
Name: "idontexist",
|
||||
},
|
||||
},
|
||||
expectedOut: []irc.Message{
|
||||
{
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{"#testchan-1"},
|
||||
Trailing: "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestPlugins(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2015 Robert S. Gerus. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bot
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
func roll(output func(irc.Message), msg irc.Message) {
|
||||
args := strings.Split(msg.Trailing, " ")
|
||||
if args[0] != ":roll" {
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
rolls := 1
|
||||
|
||||
if len(args) == 3 {
|
||||
rolls, err = strconv.Atoi(args[1])
|
||||
if err != nil || rolls < 1 {
|
||||
output(reply(msg, "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0"))
|
||||
return
|
||||
}
|
||||
} else if len(args) != 2 {
|
||||
output(reply(msg, "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0"))
|
||||
return
|
||||
}
|
||||
|
||||
sides, err := strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
output(reply(msg, "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0"))
|
||||
return
|
||||
}
|
||||
if sides <= 0 {
|
||||
output(reply(msg, "Usage: :roll <sides int> <rolls int>, each roll is [0, n), size has to be >0"))
|
||||
return
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for i := 0; i < rolls; i++ {
|
||||
sum += rand.Intn(sides)
|
||||
}
|
||||
|
||||
output(reply(msg, strconv.Itoa(sum)))
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
addCallback("PRIVMSG", "roll", roll)
|
||||
}
|
Loading…
Reference in New Issue