irc-osric/bot.go

158 lines
4.3 KiB
Go
Raw Normal View History

2014-12-09 06:32:21 -05:00
package main
import (
"github.com/thoj/go-ircevent"
"strings"
"fmt"
"os"
)
2015-01-11 10:52:06 -05:00
type Bot struct {
Conn *irc.Connection
Nick string
Server string
Channel string
}
2014-12-09 06:32:21 -05:00
2015-01-11 10:52:06 -05:00
var (
2015-01-02 05:44:29 -05:00
admins = []string{"vypr"}
2014-12-09 06:32:21 -05:00
dunmas = ""
2015-01-04 03:29:14 -05:00
rulemod = make([]string, len(modeopt))
modeopt = []string{"adminoverride", "saves", "logging"}
2015-01-02 05:44:29 -05:00
charmap = make(map[string]map[string]map[string]string)
2014-12-09 06:32:21 -05:00
monsmap = make(map[string]string)
)
2015-01-04 22:09:23 -05:00
var dict = map[string]string{
"hp": "health points",
"ap": "armour points",
"algn": "alignment",
"xp": "experience points",
"str": "strength",
"dex": "dexterity",
"wis": "wisdom",
"cha": "charisma",
"lvl": "level",
"hgt": "height",
"wgt": "weight",
"cls": "class",
}
2015-01-13 23:33:18 -05:00
var argmap = map[string]int{
".set": 4,
".print": 3,
".mode": 1,
".rmmode": 1,
".dm": 1,
".resetdm": 1,
}
func fillCharmap(nick string, cat string, item string, val string) {
charmap = map[string]map[string]map[string]string { nick: map[string]map[string]string{ cat: map[string]string{ item: val, }, }, }
}
2015-01-11 10:52:06 -05:00
func (b *Bot) Command(nick string, msg string) {
var args = make([]string, len(strings.Split(msg, " ")) - 1)
for i, j := range strings.Split(msg, " ") {
if j != " " && i !=0 {
args[i - 1] = strings.Split(msg, " ")[i]
}
}
2015-01-13 23:33:18 -05:00
if argmap[strings.Split(msg, " ")[0]] != 1 { return }
if argmap[strings.Split(msg, " ")[0]] != len(args) { return }
fmt.Println("hi");
2015-01-07 03:53:30 -05:00
// TODO: Check if mode is enabled and if command can be applied.
2015-01-13 23:33:18 -05:00
if strings.HasPrefix(msg, ".set") {
2015-01-12 18:48:59 -05:00
2015-01-02 05:44:29 -05:00
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("nocharoverride", rulemod) {
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] + ".")
2015-01-11 10:52:06 -05:00
b.Say(nick + " used override, it's super effective!")
2015-01-02 05:44:29 -05:00
}
2015-01-12 18:48:59 -05:00
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".print") && len(args) == 3 {
2015-01-12 18:48:59 -05:00
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-12 18:48:59 -05:00
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".mode") && len(args) == 1 {
2015-01-12 18:48:59 -05:00
if stringInSlice(args[0], rulemod) {
2015-01-04 03:29:14 -05:00
fmt.Println("[cmd] mode - change to " + args[0] + " failed, already set to true")
2015-01-11 10:52:06 -05:00
b.Say(args[0] + " is already set to true.")
} else {
2015-01-04 03:29:14 -05:00
fmt.Println("[cmd] mode - " + args[0])
2015-01-11 10:52:06 -05:00
b.Say(args[0] + " is now enabled.")
}
2015-01-12 18:48:59 -05:00
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".rmmode") && len(args) == 1 {
2015-01-12 18:48:59 -05:00
if removeItemInSlice(args[0], rulemod) {
fmt.Println("[cmd] rmmode - " + args[0])
2015-01-11 10:52:06 -05:00
b.Say(args[0] + " has been removed from the list of modes.")
} else {
2015-01-11 10:52:06 -05:00
b.Say(args[0] + " isn't in the list of modes.")
}
2015-01-12 18:48:59 -05:00
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".dm") && len(args) == 1 {
2015-01-12 18:48:59 -05:00
if len(dunmas) == 0 {
dunmas = args[0]
2014-12-09 06:32:21 -05:00
fmt.Println("[cmd] dm - " + dunmas)
2015-01-11 10:52:06 -05:00
b.Say("dm is now set to " + dunmas)
2014-12-09 06:32:21 -05:00
} else {
2015-01-11 10:52:06 -05:00
b.Say("dm has already been set, the current DM is " + dunmas)
2014-12-09 06:32:21 -05:00
}
2015-01-12 18:48:59 -05:00
2015-01-02 05:44:29 -05:00
} else if msg == ".resetdm" && (nick == dunmas || stringInSlice(nick, admins)) {
2015-01-12 18:48:59 -05:00
2014-12-09 06:32:21 -05:00
dunmas = ""
fmt.Println("[cmd] resetdm")
2015-01-11 10:52:06 -05:00
b.Say("dm has been reset")
2015-01-12 18:48:59 -05:00
2015-01-02 05:44:29 -05:00
} else if msg == ".quit" && stringInSlice(nick, admins) {
2015-01-12 18:48:59 -05:00
2015-01-02 05:44:29 -05:00
fmt.Println("[cmd] shutdown from " + nick)
2014-12-09 06:32:21 -05:00
os.Exit(1)
2015-01-12 18:48:59 -05:00
2014-12-09 06:32:21 -05:00
}
}
func (b *Bot) Say(msg string) {
b.Conn.Privmsg(b.Channel, msg)
}
2015-01-11 10:52:06 -05:00
func (b *Bot) Listen() {
err := b.Conn.Connect(b.Server)
2014-12-09 06:32:21 -05:00
2015-01-11 10:52:06 -05:00
if err != nil { panic(err) }
2014-12-09 06:32:21 -05:00
2015-01-11 10:52:06 -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-11 10:52:06 -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-11 10:52:06 -05:00
b.Conn.Loop()
}
func NewBot(server string, channel string, nick string) *Bot {
return &Bot{Conn: irc.IRC(nick, nick), Server: server, Channel: channel, Nick: nick}
}
func main() {
b := NewBot("irc.iotek.org:6667", "#d20", "bot")
b.Listen()
2014-12-09 06:32:21 -05:00
}