irc-osric/bot.go

221 lines
4.8 KiB
Go
Raw Normal View History

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"
"os"
"strings"
"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-01-30 10:44:46 -05:00
admins = []string{"vypr", "bajr"}
2015-01-16 15:23:31 -05:00
dunmas = ""
2014-12-09 06:32:21 -05:00
2015-01-16 15:23:31 -05:00
rulemod = make([]string, len(modeopt))
modeopt = []string{"adminoverride", "saves", "logging"}
initLog = true
2015-01-16 15:23:31 -05:00
charmap = make(map[string]map[string]map[string]string)
monsmap = make(map[string]string)
filename = "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",
"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-01-16 15:23:31 -05:00
".set": 4,
".print": 3,
".mode": 1,
".rmmode": 1,
".dm": 1,
".resetdm": 1,
".quit": 0,
2015-01-13 23:33:18 -05:00
}
func fillCharmap(nick string, cat string, item string, val string) {
2015-01-16 15:23:31 -05:00
charmap = map[string]map[string]map[string]string{
nick: map[string]map[string]string{
cat: map[string]string{
item: val,
},
},
}
}
// 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-01-16 15:23:31 -05:00
var args = make([]string, len(strings.Split(msg, " "))-1)
var command = ""
if stringInSlice(modeopt[2], rulemod) {
b.Log(nick + ": " + msg, initLog)
initLog = false
}
2015-01-16 15:23:31 -05:00
for i, j := range strings.Split(msg, " ") {
if j != " " && i != 0 {
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 {
case ".set":
if nick == dunmas {
fillCharmap(args[0], args[1], args[2], args[3])
fmt.Println("[cmd] set - " + args[0] + "'s " + args[2] + "in " + args[1] + " is set to " + args[3])
} else if stringInSlice(nick, admins) && !stringInSlice(modeopt[0], rulemod) {
2015-01-16 15:23:31 -05:00
fillCharmap(args[0], args[1], args[2], args[3])
fmt.Println("[cmd] set - " + args[0] + "'s " + args[2] + " in " + args[1] + " is set to " + args[3])
b.Say(nick + " used override, it's super effective!")
}
break
case ".print":
if len(charmap[args[0]][args[1]][args[2]]) == 0 {
b.Say("there is no setting for " + args[0] + "'s " + args[2])
} else {
fmt.Println("[cmd] print - " + args[0] + "'s " + args[2] + " in " + args[1])
b.Say(args[0] + "'s " + args[2] + " is set to " + charmap[args[0]][args[1]][args[2]])
}
2015-01-16 15:23:31 -05:00
break
case ".mode":
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])
}
break
case ".rmmode":
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")
}
break
case ".dm":
if len(dunmas) == 0 {
dunmas = args[0]
fmt.Println("[cmd] dm - " + dunmas)
b.Say("dm is now set to " + dunmas)
} else {
b.Say("dm has already been set, the current DM is " + dunmas)
}
break
case ".resetdm":
if nick == dunmas || stringInSlice(nick, admins) {
dunmas = ""
fmt.Println("[cmd] resetdm")
b.Say("dm has been reset")
}
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
}
func (b *Bot) Log(line string, initLog bool) {
if initLog {
os.Create(filename)
file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if fileerr != nil {
panic(fileerr)
}
file.WriteString("IRC-OSRIC by Elliott Pardee (vypr)\n")
file.WriteString("----------------------------------\n\n")
file.WriteString(line + "\n")
} else {
file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if fileerr != nil {
panic(fileerr)
}
file.WriteString(line + "\n")
}
}
func (b *Bot) Say(msg string) {
if stringInSlice(modeopt[2], rulemod) {
b.Log("bot: " + msg, initLog)
}
2015-01-16 15:23:31 -05:00
b.Conn.Privmsg(b.Channel, msg)
}
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
}