Working with Go's Style Guide
This commit is contained in:
parent
216cb5d62d
commit
3266cc0f7e
244
bot.go
244
bot.go
@ -1,168 +1,178 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/thoj/go-ircevent"
|
"fmt"
|
||||||
"strings"
|
"github.com/thoj/go-ircevent"
|
||||||
"fmt"
|
"os"
|
||||||
"os"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
Conn *irc.Connection
|
Conn *irc.Connection
|
||||||
Nick string
|
Nick string
|
||||||
Server string
|
Server string
|
||||||
Channel string
|
Channel string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
admins = []string{"vypr"}
|
admins = []string{"vypr"}
|
||||||
dunmas = ""
|
dunmas = ""
|
||||||
|
|
||||||
rulemod = make([]string, len(modeopt))
|
rulemod = make([]string, len(modeopt))
|
||||||
modeopt = []string{"adminoverride", "saves", "logging"}
|
modeopt = []string{"adminoverride", "saves", "logging"}
|
||||||
|
|
||||||
charmap = make(map[string]map[string]map[string]string)
|
charmap = make(map[string]map[string]map[string]string)
|
||||||
monsmap = make(map[string]string)
|
monsmap = make(map[string]string)
|
||||||
)
|
)
|
||||||
|
|
||||||
var dict = map[string]string{
|
var dict = map[string]string{
|
||||||
"hp": "health points",
|
"hp": "health points",
|
||||||
"ap": "armour points",
|
"ap": "armour points",
|
||||||
"algn": "alignment",
|
"algn": "alignment",
|
||||||
"xp": "experience points",
|
"xp": "experience points",
|
||||||
"str": "strength",
|
"str": "strength",
|
||||||
"dex": "dexterity",
|
"dex": "dexterity",
|
||||||
"wis": "wisdom",
|
"wis": "wisdom",
|
||||||
"cha": "charisma",
|
"cha": "charisma",
|
||||||
"lvl": "level",
|
"lvl": "level",
|
||||||
"hgt": "height",
|
"hgt": "height",
|
||||||
"wgt": "weight",
|
"wgt": "weight",
|
||||||
"cls": "class",
|
"cls": "class",
|
||||||
}
|
}
|
||||||
|
|
||||||
var argmap = map[string]int{
|
var argmap = map[string]int{
|
||||||
".set": 4,
|
".set": 4,
|
||||||
".print": 3,
|
".print": 3,
|
||||||
".mode": 1,
|
".mode": 1,
|
||||||
".rmmode": 1,
|
".rmmode": 1,
|
||||||
".dm": 1,
|
".dm": 1,
|
||||||
".resetdm": 1,
|
".resetdm": 1,
|
||||||
".quit": 0,
|
".quit": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillCharmap(nick string, cat string, item string, val string) {
|
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, }, }, }
|
charmap = map[string]map[string]map[string]string{
|
||||||
|
nick: map[string]map[string]string{
|
||||||
|
cat: map[string]string{
|
||||||
|
item: val,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Command(nick string, msg string) {
|
func (b *Bot) Command(nick string, msg string) {
|
||||||
var args = make([]string, len(strings.Split(msg, " ")) - 1)
|
var args = make([]string, len(strings.Split(msg, " "))-1)
|
||||||
var command = ""
|
var command = ""
|
||||||
|
|
||||||
for i, j := range strings.Split(msg, " ") {
|
for i, j := range strings.Split(msg, " ") {
|
||||||
if j != " " && i !=0 {
|
if j != " " && i != 0 {
|
||||||
args[i - 1] = strings.Split(msg, " ")[i]
|
args[i-1] = strings.Split(msg, " ")[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range argmap {
|
for i := range argmap {
|
||||||
if i == strings.Split(msg, " ")[0] {
|
if i == strings.Split(msg, " ")[0] {
|
||||||
command = strings.Split(msg, " ")[0]
|
command = strings.Split(msg, " ")[0]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if argmap[strings.Split(msg, " ")[0]] != len(args) { return }
|
if argmap[strings.Split(msg, " ")[0]] != len(args) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Check if mode is enabled and if command can be applied.
|
// TODO: Check if mode is enabled and if command can be applied.
|
||||||
|
|
||||||
switch command {
|
switch command {
|
||||||
case ".set":
|
case ".set":
|
||||||
if nick == dunmas {
|
if nick == dunmas {
|
||||||
fillCharmap(args[0], args[1], args[2], args[3])
|
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])
|
fmt.Println("[cmd] set - " + args[0] + "'s " + args[2] + "in " + args[1] + " is set to " + args[3])
|
||||||
} else if stringInSlice(nick, admins) && !stringInSlice("adminoverride", rulemod) {
|
} else if stringInSlice(nick, admins) && !stringInSlice("adminoverride", rulemod) {
|
||||||
fillCharmap(args[0], args[1], args[2], args[3])
|
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])
|
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!")
|
b.Say(nick + " used override, it's super effective!")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".print":
|
case ".print":
|
||||||
fmt.Println("[cmd] print - " + args[0] + "'s " + args[2] + " in " + args[1])
|
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]])
|
b.Say(args[0] + "'s " + args[2] + " is set to " + charmap[args[0]][args[1]][args[2]])
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".mode":
|
case ".mode":
|
||||||
if stringInSlice(args[0], rulemod) {
|
if stringInSlice(args[0], rulemod) {
|
||||||
b.Say(args[0] + " is already set to true")
|
b.Say(args[0] + " is already set to true")
|
||||||
} else if stringInSlice(args[0], modeopt) {
|
} else if stringInSlice(args[0], modeopt) {
|
||||||
fmt.Println("[cmd] mode - " + args[0])
|
fmt.Println("[cmd] mode - " + args[0])
|
||||||
b.Say(args[0] + " is now enabled")
|
b.Say(args[0] + " is now enabled")
|
||||||
rulemod = append(rulemod, args[0])
|
rulemod = append(rulemod, args[0])
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".rmmode":
|
case ".rmmode":
|
||||||
if removeItemInSlice(args[0], rulemod) {
|
if removeItemInSlice(args[0], rulemod) {
|
||||||
fmt.Println("[cmd] rmmode - " + args[0])
|
fmt.Println("[cmd] rmmode - " + args[0])
|
||||||
b.Say(args[0] + " has been removed from the list of modes")
|
b.Say(args[0] + " has been removed from the list of modes")
|
||||||
} else {
|
} else {
|
||||||
b.Say(args[0] + " isn't in the list of modes")
|
b.Say(args[0] + " isn't in the list of modes")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".dm":
|
case ".dm":
|
||||||
if len(dunmas) == 0 {
|
if len(dunmas) == 0 {
|
||||||
dunmas = args[0]
|
dunmas = args[0]
|
||||||
fmt.Println("[cmd] dm - " + dunmas)
|
fmt.Println("[cmd] dm - " + dunmas)
|
||||||
b.Say("dm is now set to " + dunmas)
|
b.Say("dm is now set to " + dunmas)
|
||||||
} else {
|
} else {
|
||||||
b.Say("dm has already been set, the current DM is " + dunmas)
|
b.Say("dm has already been set, the current DM is " + dunmas)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".resetdm":
|
case ".resetdm":
|
||||||
if nick == dunmas || stringInSlice(nick, admins) {
|
if nick == dunmas || stringInSlice(nick, admins) {
|
||||||
dunmas = ""
|
dunmas = ""
|
||||||
fmt.Println("[cmd] resetdm")
|
fmt.Println("[cmd] resetdm")
|
||||||
b.Say("dm has been reset")
|
b.Say("dm has been reset")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case ".quit":
|
case ".quit":
|
||||||
if stringInSlice(nick, admins) {
|
if stringInSlice(nick, admins) {
|
||||||
fmt.Println("[cmd] shutdown from " + nick)
|
fmt.Println("[cmd] shutdown from " + nick)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Say(msg string) {
|
func (b *Bot) Say(msg string) {
|
||||||
b.Conn.Privmsg(b.Channel, msg)
|
b.Conn.Privmsg(b.Channel, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) Listen() {
|
func (b *Bot) Listen() {
|
||||||
err := b.Conn.Connect(b.Server)
|
err := b.Conn.Connect(b.Server)
|
||||||
|
|
||||||
if err != nil { panic(err) }
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
b.Conn.AddCallback("001", func(e *irc.Event) {
|
b.Conn.AddCallback("001", func(e *irc.Event) {
|
||||||
b.Conn.Join(b.Channel)
|
b.Conn.Join(b.Channel)
|
||||||
})
|
})
|
||||||
|
|
||||||
b.Conn.AddCallback("PRIVMSG", func(e *irc.Event) {
|
b.Conn.AddCallback("PRIVMSG", func(e *irc.Event) {
|
||||||
b.Command(e.Nick, e.Message())
|
b.Command(e.Nick, e.Message())
|
||||||
})
|
})
|
||||||
|
|
||||||
b.Conn.Loop()
|
b.Conn.Loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(server string, channel string, nick string) *Bot {
|
func NewBot(server string, channel string, nick string) *Bot {
|
||||||
return &Bot{Conn: irc.IRC(nick, nick), Server: server, Channel: channel, Nick: nick}
|
return &Bot{Conn: irc.IRC(nick, nick), Server: server, Channel: channel, Nick: nick}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b := NewBot("irc.iotek.org:6667", "#d20", "bot")
|
b := NewBot("irc.iotek.org:6667", "#d20", "bot")
|
||||||
b.Listen()
|
b.Listen()
|
||||||
}
|
}
|
||||||
|
24
slices.go
24
slices.go
@ -1,20 +1,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func stringInSlice(a string, list []string) bool {
|
func stringInSlice(a string, list []string) bool {
|
||||||
for _, b := range list {
|
for _, b := range list {
|
||||||
if b == a { return true}
|
if b == a {
|
||||||
}
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeItemInSlice(a string, list []string) bool {
|
func removeItemInSlice(a string, list []string) bool {
|
||||||
for c, b := range list {
|
for c, b := range list {
|
||||||
if b == a {
|
if b == a {
|
||||||
list[c] = ""
|
list[c] = ""
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user