parent
4803122aad
commit
11c96d20d0
|
@ -2,8 +2,8 @@ package irc
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -16,8 +16,8 @@ import (
|
|||
"time"
|
||||
|
||||
rgb "github.com/foresthoffman/rgblog"
|
||||
scribble "github.com/nanobox-io/golang-scribble"
|
||||
uuid "github.com/google/uuid"
|
||||
scribble "github.com/nanobox-io/golang-scribble"
|
||||
)
|
||||
|
||||
const PSTFormat = "Jan 2 15:04:05 PST"
|
||||
|
@ -54,16 +54,17 @@ type KardBot struct {
|
|||
Server string
|
||||
startTime time.Time
|
||||
Prompts []string
|
||||
Database scribble.Driver
|
||||
Database scribble.Driver
|
||||
ChannelData map[string]ChannelData
|
||||
}
|
||||
|
||||
type ChannelData struct {
|
||||
Name string `json:"name"`
|
||||
AdminKey string `json:"value,omitempty"`
|
||||
Command string `json:"customcommand,omitempty"`
|
||||
ExtraStrings string `json:"extrastrings,omitempty"`
|
||||
JoinTime time.Time `json:"jointime"`
|
||||
Name string `json:"name"`
|
||||
AdminKey string `json:"value,omitempty"`
|
||||
Command string `json:"customcommand,omitempty"`
|
||||
ExtraStrings string `json:"extrastrings,omitempty"`
|
||||
JoinTime time.Time `json:"jointime"`
|
||||
ControlChannel bool
|
||||
}
|
||||
|
||||
// Connects the bot to the Twitch IRC server. The bot will continue to try to connect until it
|
||||
|
@ -185,7 +186,6 @@ func (bb *KardBot) Login() {
|
|||
bb.conn.Write([]byte("NICK " + bb.Name + "\r\n"))
|
||||
}
|
||||
|
||||
|
||||
func (bb *KardBot) LeaveChannel(channels ...string) {
|
||||
for _, channel := range channels {
|
||||
rgb.YPrintf("[%s] Leaving #%s...\n", TimeStamp(), channel)
|
||||
|
@ -275,7 +275,7 @@ func (bb *KardBot) Start() {
|
|||
bb.Connect()
|
||||
bb.Login()
|
||||
if len(bb.ChannelData) > 0 {
|
||||
for channelName := range(bb.ChannelData) {
|
||||
for channelName := range bb.ChannelData {
|
||||
bb.JoinChannel(channelName)
|
||||
}
|
||||
} else {
|
||||
|
@ -304,13 +304,13 @@ func (bb *KardBot) readChannelData() error {
|
|||
return err
|
||||
}
|
||||
bb.ChannelData = make(map[string]ChannelData)
|
||||
bb.ChannelData[bb.Channel] = record;
|
||||
bb.ChannelData[bb.Channel] = record
|
||||
} else {
|
||||
bb.ChannelData = make(map[string]ChannelData)
|
||||
}
|
||||
for _, data := range records {
|
||||
record := ChannelData{}
|
||||
err := json.Unmarshal([]byte(data), &record);
|
||||
err := json.Unmarshal([]byte(data), &record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -346,7 +346,7 @@ func (bb *KardBot) readOrCreateChannelKey(channel string) string {
|
|||
var record ChannelData
|
||||
if record, ok := bb.ChannelData[channel]; !ok {
|
||||
rgb.YPrintf("[%s] No channel data for #%s exists, creating\n", TimeStamp(), channel)
|
||||
err = bb.Database.Read("channelData", channel, &record);
|
||||
err = bb.Database.Read("channelData", channel, &record)
|
||||
if err == nil {
|
||||
bb.ChannelData[channel] = record
|
||||
}
|
||||
|
|
|
@ -93,38 +93,38 @@ func AdminHandler(response http.ResponseWriter, request *http.Request) {
|
|||
return
|
||||
}
|
||||
type TemplateData struct {
|
||||
Channel string
|
||||
Command string
|
||||
ExtraStrings string
|
||||
SinceTime time.Time
|
||||
Leaving bool
|
||||
Channel string
|
||||
Command string
|
||||
ExtraStrings string
|
||||
SinceTime time.Time
|
||||
Leaving bool
|
||||
}
|
||||
channelData := ircBot.ChannelData[vars["channel"]]
|
||||
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, false}
|
||||
|
||||
if request.Method == "POST" {
|
||||
request.ParseForm()
|
||||
if strings.Join(request.PostForm["leave"],",") == "Leave twitch channel" {
|
||||
if strings.Join(request.PostForm["leave"], ",") == "Leave twitch channel" {
|
||||
td.Leaving = true
|
||||
} else if strings.Join(request.PostForm["reallyleave"],",") == "Really leave twitch channel" {
|
||||
delete(ircBot.ChannelData,vars["channel"])
|
||||
} else if strings.Join(request.PostForm["reallyleave"], ",") == "Really leave twitch channel" {
|
||||
delete(ircBot.ChannelData, vars["channel"])
|
||||
ircBot.Database.Delete("channelData", vars["channel"])
|
||||
ircBot.LeaveChannel(vars["channel"])
|
||||
LeaveHandler(response, request)
|
||||
return
|
||||
}
|
||||
sourceData := ircBot.ChannelData[vars["channel"]]
|
||||
if strings.Join(request.PostForm["Command"],",") != "" {
|
||||
sourceData.Command = strings.Join(request.PostForm["Command"],",")
|
||||
if strings.Join(request.PostForm["Command"], ",") != "" {
|
||||
sourceData.Command = strings.Join(request.PostForm["Command"], ",")
|
||||
td.Command = sourceData.Command
|
||||
ircBot.ChannelData[vars["channel"]] = sourceData
|
||||
}
|
||||
if strings.Join(request.PostForm["ExtraStrings"],",") != sourceData.ExtraStrings {
|
||||
sourceData.ExtraStrings = strings.Join(request.PostForm["ExtraStrings"],",")
|
||||
if strings.Join(request.PostForm["ExtraStrings"], ",") != sourceData.ExtraStrings {
|
||||
sourceData.ExtraStrings = strings.Join(request.PostForm["ExtraStrings"], ",")
|
||||
td.ExtraStrings = sourceData.ExtraStrings
|
||||
ircBot.ChannelData[vars["channel"]] = sourceData
|
||||
}
|
||||
ircBot.Database.Write("channelData", vars["channel"], sourceData);
|
||||
ircBot.Database.Write("channelData", vars["channel"], sourceData)
|
||||
}
|
||||
tmpl := template.Must(template.ParseFiles("web/admin.html"))
|
||||
tmpl.Execute(response, td)
|
||||
|
|
8
main.go
8
main.go
|
@ -51,7 +51,7 @@ func readConfig() {
|
|||
if _, err := os.Stat(exPath + "/config.json"); os.IsNotExist(err) {
|
||||
rgb.YPrintf("[%s] Warning, KARAOKARDS_CONFIGFILE env var unset and `config.json` not alongside executable!\n", irc.TimeStamp())
|
||||
if _, err := os.Stat("/etc/karaokards/config.json"); os.IsNotExist(err) {
|
||||
rgb.RPrintf("[%s] Error, KARAOKARDS_CONFIGFILE env var unset and neither '%s' nor '%s' exist!\n", irc.TimeStamp(), exPath + "/config.json", "/etc/karaokards/config.json")
|
||||
rgb.RPrintf("[%s] Error, KARAOKARDS_CONFIGFILE env var unset and neither '%s' nor '%s' exist!\n", irc.TimeStamp(), exPath+"/config.json", "/etc/karaokards/config.json")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
configFile = "/etc/karaokards/config.json"
|
||||
|
@ -92,10 +92,10 @@ func openDatabase() *scribble.Driver {
|
|||
}
|
||||
exPath := filepath.Dir(ex)
|
||||
if _, err := os.Stat(exPath + "/data"); os.IsNotExist(err) {
|
||||
rgb.YPrintf("[%s] Warning %s doesn't exist, trying to create it.\n", irc.TimeStamp(), exPath + "/data")
|
||||
err = os.Mkdir(exPath + "/data", 0770)
|
||||
rgb.YPrintf("[%s] Warning %s doesn't exist, trying to create it.\n", irc.TimeStamp(), exPath+"/data")
|
||||
err = os.Mkdir(exPath+"/data", 0770)
|
||||
if err != nil {
|
||||
rgb.RPrintf("[%s] Error cannot create %s: %s!\n", irc.TimeStamp(), exPath + "/data", err)
|
||||
rgb.RPrintf("[%s] Error cannot create %s: %s!\n", irc.TimeStamp(), exPath+"/data", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue