2014-12-09 06:32:21 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-16 15:23:31 -05:00
|
|
|
"fmt"
|
|
|
|
"github.com/thoj/go-ircevent"
|
2015-02-10 09:24:36 -05:00
|
|
|
"math/rand"
|
2015-01-16 15:23:31 -05:00
|
|
|
"os"
|
2015-02-10 09:24:36 -05:00
|
|
|
"strconv"
|
2015-01-16 15:23:31 -05:00
|
|
|
"strings"
|
2015-02-10 09:24:36 -05:00
|
|
|
"time"
|
2014-12-09 06:32:21 -05:00
|
|
|
)
|
|
|
|
|
2015-01-11 10:52:06 -05:00
|
|
|
type Bot struct {
|
2015-01-16 15:23:31 -05:00
|
|
|
Conn *irc.Connection
|
|
|
|
Nick string
|
|
|
|
Server string
|
|
|
|
Channel string
|
2015-01-11 10:52:06 -05:00
|
|
|
}
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-01-11 10:52:06 -05:00
|
|
|
var (
|
2015-04-18 20:06:36 -04:00
|
|
|
admins = []string{"vypr", "dami", "kirby"}
|
2015-02-10 09:06:59 -05:00
|
|
|
dieopt = []string{"4", "6", "8", "10", "12", "20"}
|
2015-02-10 09:24:36 -05:00
|
|
|
dunmas = ""
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-02-12 06:54:43 -05:00
|
|
|
rulemod = []string{"voting"}
|
2015-04-18 20:06:36 -04:00
|
|
|
modeopt = []string{"adminoverride", "logging", "voting"}
|
2015-01-16 19:24:36 -05:00
|
|
|
initLog = true
|
2015-01-02 06:27:23 -05:00
|
|
|
|
2015-02-12 06:54:43 -05:00
|
|
|
votemap = make(map[string]int)
|
2015-01-25 17:20:45 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
filename = "log/log_" + time.Now().Local().Format("20060102") + "_" + time.Now().Local().Format("150405") + ".txt"
|
2014-12-09 06:32:21 -05:00
|
|
|
)
|
|
|
|
|
2015-01-04 22:09:23 -05:00
|
|
|
var dict = map[string]string{
|
2015-01-16 15:23:31 -05:00
|
|
|
"hp": "health points",
|
|
|
|
"ap": "armour points",
|
|
|
|
"algn": "alignment",
|
|
|
|
"xp": "experience points",
|
|
|
|
"str": "strength",
|
2015-01-30 10:37:40 -05:00
|
|
|
"con": "constitution",
|
2015-02-10 09:24:36 -05:00
|
|
|
"dex": "dexterity",
|
2015-01-16 15:23:31 -05:00
|
|
|
"wis": "wisdom",
|
|
|
|
"cha": "charisma",
|
|
|
|
"lvl": "level",
|
|
|
|
"hgt": "height",
|
|
|
|
"wgt": "weight",
|
|
|
|
"cls": "class",
|
2015-01-04 22:09:23 -05:00
|
|
|
}
|
|
|
|
|
2015-01-13 23:33:18 -05:00
|
|
|
var argmap = map[string]int{
|
2015-02-28 06:46:44 -05:00
|
|
|
".set": 4,
|
2015-01-16 15:23:31 -05:00
|
|
|
".print": 3,
|
2015-02-12 06:54:43 -05:00
|
|
|
".vote": 1,
|
2015-02-10 09:24:36 -05:00
|
|
|
".d": 1,
|
2015-01-16 15:23:31 -05:00
|
|
|
".mode": 1,
|
|
|
|
".rmmode": 1,
|
|
|
|
".dm": 1,
|
2015-02-26 05:24:31 -05:00
|
|
|
".import": 1,
|
2015-02-12 06:54:43 -05:00
|
|
|
".choose": 0,
|
2015-02-10 09:43:44 -05:00
|
|
|
".resetdm": 0,
|
2015-01-16 15:23:31 -05:00
|
|
|
".quit": 0,
|
2015-01-13 23:33:18 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 09:06:59 -05:00
|
|
|
func roll(amount int, side int) int {
|
2015-02-10 09:24:36 -05:00
|
|
|
var numbers = make([]int, amount)
|
|
|
|
var finaln = 0
|
2015-02-08 21:40:31 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
for i := 0; i < amount; i++ {
|
|
|
|
number := rand.Intn(side) + 1
|
|
|
|
numbers[i] = number
|
|
|
|
}
|
2015-02-08 21:40:31 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
for i := 0; i < len(numbers); i++ {
|
|
|
|
if i == len(numbers) {
|
|
|
|
return finaln
|
|
|
|
}
|
2015-02-08 21:40:31 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
finaln = finaln + numbers[i]
|
|
|
|
}
|
2015-02-10 09:06:59 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
return finaln
|
2015-02-08 21:40:31 -05:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:54:43 -05:00
|
|
|
func vote(nick string) {
|
|
|
|
if _, j := votemap[nick]; j {
|
|
|
|
var tmp = votemap[nick] + 1
|
|
|
|
votemap[nick] = tmp
|
|
|
|
} else {
|
|
|
|
votemap[nick] = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func choose() (string, int) {
|
|
|
|
nmap := sortMapByValue(votemap)
|
|
|
|
return nmap[0].Key, nmap[0].Value
|
|
|
|
}
|
|
|
|
|
2015-01-25 17:20:45 -05:00
|
|
|
// TODO: Create functions related to character import/export.
|
|
|
|
|
2015-01-11 10:52:06 -05:00
|
|
|
func (b *Bot) Command(nick string, msg string) {
|
2015-02-10 09:24:36 -05:00
|
|
|
var args = make([]string, len(strings.Split(msg, " "))-1)
|
2015-01-16 15:23:31 -05:00
|
|
|
var command = ""
|
|
|
|
|
2015-01-25 17:20:45 -05:00
|
|
|
if stringInSlice(modeopt[2], rulemod) {
|
2015-02-10 09:24:36 -05:00
|
|
|
b.Log(nick+": "+msg, initLog)
|
|
|
|
initLog = false
|
|
|
|
}
|
2015-01-16 19:24:36 -05:00
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
for i, j := range strings.Split(msg, " ") {
|
|
|
|
if j != " " && i != 0 {
|
2015-02-10 09:24:36 -05:00
|
|
|
args[i-1] = strings.Split(msg, " ")[i]
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range argmap {
|
|
|
|
if i == strings.Split(msg, " ")[0] {
|
|
|
|
command = strings.Split(msg, " ")[0]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argmap[strings.Split(msg, " ")[0]] != len(args) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch command {
|
2015-02-26 05:24:31 -05:00
|
|
|
case ".import":
|
|
|
|
if importChar(args[0]) {
|
|
|
|
b.Say("importing " + args[0] + " successful")
|
|
|
|
fmt.Println("[cmd] import " + args[0])
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
|
2015-02-28 06:46:44 -05:00
|
|
|
case ".set":
|
|
|
|
if nick == dunmas {
|
|
|
|
if len(args) == 5 {
|
|
|
|
if setChar(args[0], args[1], args[2], args[3], args[4]) {
|
|
|
|
fmt.Println("[cmd] set")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if setChar(args[0], args[1], "nil", args[2], args[3]) {
|
|
|
|
fmt.Println("[cmd] set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) {
|
|
|
|
if len(args) == 5 {
|
|
|
|
if setChar(args[0], args[1], args[2], args[3], args[4]) {
|
|
|
|
fmt.Println("[cmd] set")
|
|
|
|
b.Say(nick + " used override, it's super effective!")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if setChar(args[0], args[1], "nil", args[2], args[3]) {
|
|
|
|
fmt.Println("[cmd] set")
|
|
|
|
b.Say(nick + " used override, it's super effective!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
case ".print":
|
2015-02-26 05:24:31 -05:00
|
|
|
if len(args) == 4 {
|
|
|
|
b.Say(args[0] + "[" + args[3] + "] = " + printChar(args[0], args[1], args[2], args[3]))
|
|
|
|
fmt.Println("[cmd] print")
|
2015-01-16 19:24:36 -05:00
|
|
|
} else {
|
2015-02-26 05:24:31 -05:00
|
|
|
b.Say(args[0] + "[" + args[2] + "] = " + printChar(args[0], args[1], "nil", args[2]))
|
|
|
|
fmt.Println("[cmd] print")
|
2015-01-16 19:24:36 -05:00
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
break
|
|
|
|
|
2015-02-12 06:54:43 -05:00
|
|
|
case ".vote":
|
|
|
|
if stringInSlice(modeopt[3], rulemod) {
|
|
|
|
vote(args[0])
|
|
|
|
fmt.Println("[cmd] vote - " + args[0])
|
|
|
|
} else {
|
|
|
|
b.Say(nick + " - the dm has already been chosen")
|
|
|
|
b.Say(nick + " - have the dm or an admin use .resetdm if necessary")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
case ".d":
|
|
|
|
amount, _ := strconv.Atoi(strings.Split(args[0], "d")[0])
|
|
|
|
side, _ := strconv.Atoi(strings.Split(args[0], "d")[1])
|
2015-02-10 09:06:59 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
if stringInSlice(strconv.Itoa(side), dieopt) && len(strings.Split(args[0], "d")) < 3 && amount > 0 && amount <= 20 {
|
|
|
|
fmt.Println("[cmd] rolling " + args[0])
|
|
|
|
b.Say(nick + " rolled a " + strconv.Itoa(roll(amount, side)))
|
|
|
|
}
|
|
|
|
break
|
2015-02-08 21:40:31 -05:00
|
|
|
|
2015-02-10 09:24:36 -05:00
|
|
|
case ".mode":
|
2015-02-12 06:54:43 -05:00
|
|
|
if nick == dunmas || stringInSlice(nick, admins) {
|
|
|
|
if stringInSlice(args[0], rulemod) {
|
|
|
|
b.Say(args[0] + " is already set to true")
|
|
|
|
} else if stringInSlice(args[0], modeopt) {
|
|
|
|
fmt.Println("[cmd] mode - " + args[0])
|
|
|
|
b.Say(args[0] + " is now enabled")
|
|
|
|
rulemod = append(rulemod, args[0])
|
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case ".rmmode":
|
2015-02-12 06:54:43 -05:00
|
|
|
if nick == dunmas || stringInSlice(nick, admins) {
|
|
|
|
if removeItemInSlice(args[0], rulemod) {
|
|
|
|
fmt.Println("[cmd] rmmode - " + args[0])
|
|
|
|
b.Say(args[0] + " has been removed from the list of modes")
|
|
|
|
} else {
|
|
|
|
b.Say(args[0] + " isn't in the list of modes")
|
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case ".dm":
|
2015-02-12 06:54:43 -05:00
|
|
|
if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) {
|
2015-01-16 15:23:31 -05:00
|
|
|
dunmas = args[0]
|
|
|
|
fmt.Println("[cmd] dm - " + dunmas)
|
|
|
|
b.Say("dm is now set to " + dunmas)
|
2015-02-12 06:54:43 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case ".choose":
|
|
|
|
var val = 0
|
|
|
|
|
|
|
|
if stringInSlice(modeopt[3], rulemod) {
|
|
|
|
dunmas, val = choose()
|
|
|
|
fmt.Println("[cmd] choosing " + dunmas + " as dm")
|
|
|
|
b.Say("the dm is now " + dunmas + " after " + strconv.Itoa(val) + " vote(s)")
|
|
|
|
|
|
|
|
if removeItemInSlice(modeopt[3], rulemod) {
|
|
|
|
b.Say("voting is now disabled")
|
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case ".resetdm":
|
2015-02-26 05:24:31 -05:00
|
|
|
if nick == dunmas && len(dunmas) > 0 {
|
2015-02-10 09:43:44 -05:00
|
|
|
dunmas = ""
|
|
|
|
fmt.Println("[cmd] resetdm")
|
|
|
|
b.Say("dm has been reset")
|
2015-02-26 05:24:31 -05:00
|
|
|
|
|
|
|
if stringInSlice(modeopt[3], rulemod) {
|
|
|
|
rulemod = append(rulemod, modeopt[3])
|
|
|
|
b.Say("voting is now enabled")
|
|
|
|
}
|
|
|
|
} else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) && len(dunmas) > 0 {
|
2015-01-16 15:23:31 -05:00
|
|
|
dunmas = ""
|
|
|
|
fmt.Println("[cmd] resetdm")
|
|
|
|
b.Say("dm has been reset")
|
2015-02-10 09:43:44 -05:00
|
|
|
b.Say(nick + " used override, it's super effective!")
|
2015-02-26 05:24:31 -05:00
|
|
|
|
|
|
|
if stringInSlice(modeopt[3], rulemod) {
|
|
|
|
rulemod = append(rulemod, modeopt[3])
|
|
|
|
b.Say("voting is now enabled")
|
|
|
|
}
|
2015-01-16 15:23:31 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case ".quit":
|
|
|
|
if stringInSlice(nick, admins) {
|
|
|
|
fmt.Println("[cmd] shutdown from " + nick)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2014-12-09 06:32:21 -05:00
|
|
|
}
|
|
|
|
|
2015-01-16 19:24:36 -05:00
|
|
|
func (b *Bot) Log(line string, initLog bool) {
|
2015-02-10 09:24:36 -05:00
|
|
|
if initLog {
|
2015-01-25 17:20:45 -05:00
|
|
|
os.Create(filename)
|
2015-01-16 19:24:36 -05:00
|
|
|
|
2015-01-25 17:20:45 -05:00
|
|
|
file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
|
2015-01-16 19:24:36 -05:00
|
|
|
|
|
|
|
if fileerr != nil {
|
|
|
|
panic(fileerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.WriteString("IRC-OSRIC by Elliott Pardee (vypr)\n")
|
|
|
|
file.WriteString("----------------------------------\n\n")
|
|
|
|
file.WriteString(line + "\n")
|
|
|
|
} else {
|
2015-01-25 17:20:45 -05:00
|
|
|
file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
|
2015-01-16 19:24:36 -05:00
|
|
|
|
|
|
|
if fileerr != nil {
|
|
|
|
panic(fileerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.WriteString(line + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:18:20 -05:00
|
|
|
func (b *Bot) Say(msg string) {
|
2015-01-25 17:20:45 -05:00
|
|
|
if stringInSlice(modeopt[2], rulemod) {
|
2015-02-10 09:24:36 -05:00
|
|
|
b.Log("bot: "+msg, initLog)
|
2015-01-16 19:24:36 -05:00
|
|
|
}
|
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
b.Conn.Privmsg(b.Channel, msg)
|
2015-01-11 21:18:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 10:52:06 -05:00
|
|
|
func (b *Bot) Listen() {
|
2015-01-16 15:23:31 -05:00
|
|
|
err := b.Conn.Connect(b.Server)
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
b.Conn.AddCallback("001", func(e *irc.Event) {
|
|
|
|
b.Conn.Join(b.Channel)
|
|
|
|
})
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
b.Conn.AddCallback("PRIVMSG", func(e *irc.Event) {
|
|
|
|
b.Command(e.Nick, e.Message())
|
|
|
|
})
|
2014-12-09 06:32:21 -05:00
|
|
|
|
2015-01-16 15:23:31 -05:00
|
|
|
b.Conn.Loop()
|
2015-01-11 10:52:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBot(server string, channel string, nick string) *Bot {
|
2015-01-16 15:23:31 -05:00
|
|
|
return &Bot{Conn: irc.IRC(nick, nick), Server: server, Channel: channel, Nick: nick}
|
2015-01-11 10:52:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-01-16 15:23:31 -05:00
|
|
|
b := NewBot("irc.iotek.org:6667", "#d20", "bot")
|
|
|
|
b.Listen()
|
2014-12-09 06:32:21 -05:00
|
|
|
}
|