diff --git a/main.go b/main.go
index ab48d39..7ec3f46 100644
--- a/main.go
+++ b/main.go
@@ -39,6 +39,9 @@ var (
// mutex for checking if voice connection already exists
mutex = &sync.Mutex{}
+
+ // connection refresher counter
+ rcCounter int = 0
)
// Play represents an individual use of the !airhorn command
@@ -57,11 +60,10 @@ type Play struct {
// SoundCollection of Sounds
type SoundCollection struct {
- Prefix string
- Commands []string
- Sounds []*Sound
- ChainWith *SoundCollection
-
+ Prefix string
+ Commands []string
+ Sounds []*Sound
+ ChainWith *SoundCollection
soundRange int
}
@@ -351,8 +353,8 @@ func playSound(play *Play, vc *discordgo.VoiceConnection) (err error) {
time.Sleep(time.Millisecond * time.Duration(play.Sound.PartDelay))
mutex.Lock()
delete(queues, play.GuildID)
- mutex.Unlock()
vc.Disconnect()
+ mutex.Unlock()
return nil
}
@@ -458,7 +460,35 @@ func setIdleStatus() {
}
}
+func checkReconnectCounter() {
+ mutex.Lock()
+ if rcCounter >= 20 {
+ reconnect()
+ rcCounter = 0
+ } else {
+ rcCounter++
+ }
+ mutex.Unlock()
+}
+
+func reconnect() {
+ err := discord.Close()
+ if err != nil {
+ log.WithFields(log.Fields{
+ "error": err,
+ }).Error("Failed to close connection.")
+ return
+ }
+ err = discord.Open()
+ if err != nil {
+ log.WithFields(log.Fields{
+ "error": err,
+ }).Fatal("Failed to create discord websocket connection")
+ }
+}
+
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
+ go checkReconnectCounter()
if m.Content == "ping" || m.Content == "pong" {
// If the message is "ping" reply with "Pong!"
if m.Content == "ping" {
diff --git a/templates/header.html b/templates/header.html
index 848fa95..a5474d4 100644
--- a/templates/header.html
+++ b/templates/header.html
@@ -15,11 +15,12 @@
body {
padding-top: 50px;
}
- .vcenter {
- display: inline-block;
- vertical-align: middle;
- float: none;
-}
+ .itembuffer {
+ margin: 1px;
+ padding: 5px;
+ border: 1px solid black;
+ background-color: #f2f2f2;
+ }
@@ -55,5 +56,3 @@
Gidbig
{{ template "sessionHandler" . }}
{{ end }}
-
-
diff --git a/templates/item.html b/templates/item.html
new file mode 100644
index 0000000..4ec4cd2
--- /dev/null
+++ b/templates/item.html
@@ -0,0 +1 @@
+
diff --git a/templates/itemrow.html b/templates/itemrow.html
deleted file mode 100644
index c52940a..0000000
--- a/templates/itemrow.html
+++ /dev/null
@@ -1,5 +0,0 @@
-
-{{ range . }}
-
-{{ end }}
-
diff --git a/templates/itemrowend.html b/templates/itemrowend.html
new file mode 100644
index 0000000..0c2eddc
--- /dev/null
+++ b/templates/itemrowend.html
@@ -0,0 +1 @@
+
diff --git a/templates/itemrowstart.html b/templates/itemrowstart.html
new file mode 100644
index 0000000..cc4da1a
--- /dev/null
+++ b/templates/itemrowstart.html
@@ -0,0 +1 @@
+
diff --git a/webserver.go b/webserver.go
index 33d053f..118b61c 100644
--- a/webserver.go
+++ b/webserver.go
@@ -48,7 +48,9 @@ type SoundItem struct {
func startWebServer(port string, ci string, cs string, redirectURL string) {
tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", header, footer))
tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", header, footer))
- tmpls["itemrow.html"] = template.Must(template.ParseFiles(templateDir + "itemrow.html"))
+ tmpls["item.html"] = template.Must(template.ParseFiles(templateDir + "item.html"))
+ tmpls["itemrowstart.html"] = template.Must(template.ParseFiles(templateDir + "itemrowstart.html"))
+ tmpls["itemrowend.html"] = template.Must(template.ParseFiles(templateDir + "itemrowend.html"))
store = sessions.NewCookieStore([]byte(cs))
discordOauthConfig.ClientID = ci
discordOauthConfig.ClientSecret = cs
@@ -67,7 +69,7 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
func handlePlaySound(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname"))
- session, _ := store.Get(r, "session-name")
+ session, _ := store.Get(r, "gidbig-session")
var guild *discordgo.Guild
user, _ := discord.User(session.Values["discordUserID"].(string))
for _, g := range discord.State.Guilds {
@@ -83,36 +85,45 @@ func handlePlaySound(w http.ResponseWriter, r *http.Request) {
}
func handleMain(w http.ResponseWriter, r *http.Request) {
- session, _ := store.Get(r, "session-name")
+ session, _ := store.Get(r, "gidbig-session")
if session.Values["discordUsername"] != nil {
err := tmpls["internal.html"].ExecuteTemplate(w, "header", map[string]interface{}{})
if err != nil {
fmt.Println(err)
return
}
- var si [4]SoundItem
- cnt := 0
+
+ var si []SoundItem
for _, sc := range COLLECTIONS {
for _, snd := range sc.Sounds {
- si[cnt%4].Itemcommand = "!" + sc.Prefix
- si[cnt%4].Itemsoundname = snd.Name
- si[cnt%4].Itemtext = si[cnt%4].Itemcommand + " " + si[cnt%4].Itemsoundname
- if cnt != 0 && cnt%4 == 3 {
- err = tmpls["itemrow.html"].Execute(w, si)
- if err != nil {
- fmt.Println(err)
- return
- }
- }
- cnt++
+ si = append(si, SoundItem{
+ Itemcommand: "!" + sc.Prefix,
+ Itemsoundname: snd.Name,
+ Itemtext: "!" + sc.Prefix + " " + snd.Name,
+ })
}
}
- if cnt%4 != 0 {
- err = tmpls["itemrow.html"].Execute(w, si)
+
+ for i, snd := range si {
+ if i%4 == 0 {
+ err = tmpls["itemrowstart.html"].Execute(w, nil)
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+ }
+ err = tmpls["item.html"].Execute(w, snd)
if err != nil {
fmt.Println(err)
return
}
+ if i%4 == 3 {
+ err = tmpls["itemrowend.html"].Execute(w, nil)
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+ }
}
err = tmpls["internal.html"].ExecuteTemplate(w, "footer", map[string]interface{}{})
@@ -127,7 +138,7 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
- Name: "session-name",
+ Name: "gidbig-session",
Value: "",
Path: "/",
MaxAge: -1,
@@ -140,7 +151,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
rand.Read(b)
oauthStateString = base64.URLEncoding.EncodeToString(b)
- session, _ := store.Get(r, "session-name")
+ session, _ := store.Get(r, "gidbig-session")
session.Values["state"] = oauthStateString
session.Save(r, w)
@@ -149,7 +160,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
}
func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
- session, err := store.Get(r, "session-name")
+ session, err := store.Get(r, "gidbig-session")
if err != nil {
fmt.Fprintln(w, "aborted")
return