Skip to content

Instantly share code, notes, and snippets.

@ochaochaocha3
Created December 7, 2014 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochaochaocha3/1b73f8ae481c433c2a01 to your computer and use it in GitHub Desktop.
Save ochaochaocha3/1b73f8ae481c433c2a01 to your computer and use it in GitHub Desktop.
Go 言語でダイスロール IRC ボット
package main
import (
"fmt"
"github.com/thoj/go-ircevent"
"log"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
)
func main() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ircobj := irc.IRC("go-dicebot", "go-dicebot")
if err := ircobj.Connect("irc.cre.ne.jp:6667"); err != nil {
log.Fatal(err)
}
const chName string = "#irc_test"
ircobj.AddCallback("001", func(e *irc.Event) {
ircobj.Join(chName)
})
ircobj.AddCallback("PRIVMSG", func(e *irc.Event) {
message := e.Message()
nick := e.Nick
rollRe := regexp.MustCompile("^.goroll ([1-9]\\d*)d([1-9]\\d*)$")
if m := rollRe.FindStringSubmatch(message); m != nil {
nDice, _ := strconv.Atoi(m[1])
max, _ := strconv.Atoi(m[2])
values := make([]int, nDice)
valuesStrs := make([]string, nDice)
sum := 0
for i := 0; i < nDice; i++ {
values[i] = r.Intn(max) + 1
valuesStrs[i] = fmt.Sprint(values[i])
sum += values[i]
}
ircobj.Notice(chName, nick+" -> "+m[1]+"d"+m[2]+" = ["+strings.Join(valuesStrs, ", ")+"] = "+fmt.Sprint(sum))
}
})
ircobj.Loop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment