Logs now have filenames based on the time of initialization.

This commit is contained in:
Elliott Pardee 2015-01-25 17:20:45 -05:00
parent f247e3c79c
commit ba8e5fd035

27
bot.go
View File

@ -5,6 +5,7 @@ import (
"github.com/thoj/go-ircevent" "github.com/thoj/go-ircevent"
"os" "os"
"strings" "strings"
"time"
) )
type Bot struct { type Bot struct {
@ -20,11 +21,12 @@ var (
rulemod = make([]string, len(modeopt)) rulemod = make([]string, len(modeopt))
modeopt = []string{"adminoverride", "saves", "logging"} modeopt = []string{"adminoverride", "saves", "logging"}
logging = false
initLog = true initLog = true
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)
filename = "log_" + time.Now().Local().Format("20060102") + "_" + time.Now().Local().Format("150405") + ".txt"
) )
var dict = map[string]string{ var dict = map[string]string{
@ -62,17 +64,13 @@ func fillCharmap(nick string, cat string, item string, val string) {
} }
} }
// TODO: Create functions related to character import/export.
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 = ""
if stringInSlice("logging", rulemod) { if stringInSlice(modeopt[2], rulemod) {
logging = true
} else {
logging = false
}
if logging {
b.Log(nick + ": " + msg, initLog) b.Log(nick + ": " + msg, initLog)
initLog = false initLog = false
} }
@ -94,14 +92,12 @@ func (b *Bot) Command(nick string, msg string) {
return return
} }
// 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(modeopt[0], 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!")
@ -165,10 +161,9 @@ func (b *Bot) Command(nick string, msg string) {
func (b *Bot) Log(line string, initLog bool) { func (b *Bot) Log(line string, initLog bool) {
if initLog { if initLog {
os.Remove("log.txt") os.Create(filename)
os.Create("log.txt")
file, fileerr := os.OpenFile("log.txt", os.O_RDWR|os.O_APPEND, 0660) file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if fileerr != nil { if fileerr != nil {
panic(fileerr) panic(fileerr)
@ -178,7 +173,7 @@ func (b *Bot) Log(line string, initLog bool) {
file.WriteString("----------------------------------\n\n") file.WriteString("----------------------------------\n\n")
file.WriteString(line + "\n") file.WriteString(line + "\n")
} else { } else {
file, fileerr := os.OpenFile("log.txt", os.O_RDWR|os.O_APPEND, 0660) file, fileerr := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if fileerr != nil { if fileerr != nil {
panic(fileerr) panic(fileerr)
@ -189,7 +184,7 @@ func (b *Bot) Log(line string, initLog bool) {
} }
func (b *Bot) Say(msg string) { func (b *Bot) Say(msg string) {
if logging { if stringInSlice(modeopt[2], rulemod) {
b.Log("bot: " + msg, initLog) b.Log("bot: " + msg, initLog)
} }