From 0ffa002ba9e463507d0edd633dc43ac64c43dac5 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Thu, 19 Jan 2017 17:03:57 +0100 Subject: [PATCH] First test of sound commands through web interface --- main.go | 20 +++++++++++++++++++- templates/internal.html | 15 ++++++++++++--- webserver.go | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 1623f22..ab48d39 100644 --- a/main.go +++ b/main.go @@ -530,6 +530,24 @@ func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } // Find the collection for the command we got + findAndPlaySound(s, m, parts, guild) +} + +func findSoundAndCollection(command string, soundname string) (*Sound, *SoundCollection) { + for _, c := range COLLECTIONS { + if scontains(command, c.Commands...) { + for _, s := range c.Sounds { + if soundname == s.Name { + return s, c + } + } + } + } + return nil, nil +} + +// Find sound in collection and play it or do nothing if not found +func findAndPlaySound(s *discordgo.Session, m *discordgo.MessageCreate, parts []string, g *discordgo.Guild) { for _, coll := range COLLECTIONS { if scontains(parts[0], coll.Commands...) { go deleteCommandMessage(s, m.ChannelID, m.ID) @@ -548,7 +566,7 @@ func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } } - go enqueuePlay(m.Author, guild, coll, sound) + go enqueuePlay(m.Author, g, coll, sound) return } } diff --git a/templates/internal.html b/templates/internal.html index fff2431..9b31fa7 100644 --- a/templates/internal.html +++ b/templates/internal.html @@ -1,3 +1,12 @@ -{{ define "content" }} -Logout -{{ end }} +{{ define "content" }} + +Logout
+Play sound {{ end }} diff --git a/webserver.go b/webserver.go index 746f55f..15236b7 100644 --- a/webserver.go +++ b/webserver.go @@ -51,12 +51,31 @@ func startWebServer(port string, ci string, cs string, redirectURL string) { r.HandleFunc("/logout", handleLogout) r.HandleFunc("/discordLogin", handlediscordLogin) r.HandleFunc("/discordCallback", handlediscordCallback) + r.HandleFunc("/playsound", handlePlaySound) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/", r) http.ListenAndServe(":"+port, nil) } +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") + var guild *discordgo.Guild + user, _ := discord.User(session.Values["discordUserID"].(string)) + for _, g := range discord.State.Guilds { + for _, vs := range g.VoiceStates { + if vs.UserID == session.Values["discordUserID"].(string) { + guild = g + } + } + } + if user != nil && guild != nil && sound != nil && soundCollection != nil { + enqueuePlay(user, guild, soundCollection, sound) + } +} + func handleMain(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "session-name") if session.Values["discordUsername"] != nil {