irc-osric/bot.go

129 lines
4.1 KiB
Go
Raw Normal View History

2014-12-09 06:32:21 -05:00
package main
import (
"github.com/thoj/go-ircevent"
"strings"
"strconv"
"fmt"
"os"
)
var (
2015-01-02 05:44:29 -05:00
server = "irc.iotek.org"
2014-12-09 06:32:21 -05:00
port = 6667
2015-01-02 05:44:29 -05:00
channel = "#d20"
2014-12-09 06:32:21 -05:00
nickname = "bot"
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)
)
func stringInSlice(a string, list []string) bool {
// thanks stackoverflow
for _, b := range list {
if b == a { return true }
}
return false
}
func removeItemInSlice(a string, list []string) bool {
for c, d := range list {
if d == a {
list[c] = ""
return true
}
}
return false
}
func findArguments(msg string) []string {
2015-01-04 03:29:14 -05:00
var args = make([]string, len(strings.Split(msg, " ")) - 1) // thanks jmbi
var nmsg = strings.Split(msg, " ")
for i, j := range nmsg {
2015-01-04 03:29:14 -05:00
if j != " " && i != 0 {
args[i - 1] = nmsg[i]
}
}
return args
}
func fillCharmap(nick string, cat string, item string, val string) {
2015-01-04 03:29:14 -05:00
// thanks jmbi
charmap = map[string]map[string]map[string]string { nick: map[string]map[string]string{ cat: map[string]string{ item: val, }, }, }
}
func handleMessage(nick string, msg string, conn *irc.Connection) {
var args = findArguments(msg)
2015-01-04 03:29:14 -05:00
if strings.HasPrefix(msg, ".set") && len(args) == 4 {
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-02 05:44:29 -05:00
conn.Privmsg(channel, nick + " used override, it's super effective!")
}
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".print") && len(args) == 3 {
fmt.Println("[cmd] print - " + args[0] + "'s " + args[1] + " in " + args[3] + ".")
conn.Privmsg(channel, args[0] + "'s " + args[0] + " is set to " + charmap[args[0]][args[1]][args[2]] + ".")
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".mode") && len(args) == 1 {
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")
conn.Privmsg(channel, args[0] + " is already set to true.")
} else {
2015-01-04 03:29:14 -05:00
fmt.Println("[cmd] mode - " + args[0])
conn.Privmsg(channel, args[0] + " is now enabled.")
}
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".rmmode") && len(args) == 1 {
if removeItemInSlice(args[0], rulemod) {
fmt.Println("[cmd] rmmode - " + args[0])
conn.Privmsg(channel, args[0] + " has been removed from the list of modes.")
} else {
conn.Privmsg(channel, args[0] + " isn't in the list of modes.")
}
2015-01-04 03:29:14 -05:00
} else if strings.HasPrefix(msg, ".dm") && len(args) == 1 {
if len(dunmas) == 0 {
dunmas = args[0]
2014-12-09 06:32:21 -05:00
fmt.Println("[cmd] dm - " + dunmas)
conn.Privmsg(channel, "dm is now set to " + dunmas)
} else {
conn.Privmsg(channel, "dm has already been set, the current DM is " + dunmas)
}
2015-01-02 05:44:29 -05:00
} else if msg == ".resetdm" && (nick == dunmas || stringInSlice(nick, admins)) {
2014-12-09 06:32:21 -05:00
dunmas = ""
fmt.Println("[cmd] resetdm")
conn.Privmsg(channel, "dm has been reset")
2015-01-02 05:44:29 -05:00
} else if msg == ".quit" && stringInSlice(nick, admins) {
fmt.Println("[cmd] shutdown from " + nick)
2014-12-09 06:32:21 -05:00
os.Exit(1)
}
}
func main() {
conn := irc.IRC(nickname, nickname)
err := conn.Connect(server + ":" + strconv.Itoa(port))
if err != nil {
fmt.Print("[err] connection failed - ")
fmt.Println(err)
}
2015-01-02 05:44:29 -05:00
conn.AddCallback("001", func(e *irc.Event) { conn.Join(channel) })
2014-12-09 06:32:21 -05:00
conn.AddCallback("PRIVMSG", func(e *irc.Event) {
handleMessage(e.Nick, e.Message(), conn)
2014-12-09 06:32:21 -05:00
})
conn.Loop()
}