First test of sound commands through web interface

This commit is contained in:
da
2017-01-19 17:03:57 +01:00
parent 9b8d1ead08
commit 0ffa002ba9
3 changed files with 50 additions and 4 deletions
+19 -1
View File
@@ -530,6 +530,24 @@ func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
// Find the collection for the command we got // 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 { for _, coll := range COLLECTIONS {
if scontains(parts[0], coll.Commands...) { if scontains(parts[0], coll.Commands...) {
go deleteCommandMessage(s, m.ChannelID, m.ID) 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 return
} }
} }
+11 -2
View File
@@ -1,3 +1,12 @@
{{ define "content" }} {{ define "content" }}
<a href="/logout">Logout</a> <script type="text/javascript">
{{ end }} function playSound() {
var data = {
command: "!bsth",
soundname: "unmoeglich"
};
$.post("/playsound", data, function(data, success) {});
}
</script>
<a href="/logout">Logout</a><br/>
<a href="javascript:playSound()">Play sound</a> {{ end }}
+19
View File
@@ -51,12 +51,31 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
r.HandleFunc("/logout", handleLogout) r.HandleFunc("/logout", handleLogout)
r.HandleFunc("/discordLogin", handlediscordLogin) r.HandleFunc("/discordLogin", handlediscordLogin)
r.HandleFunc("/discordCallback", handlediscordCallback) r.HandleFunc("/discordCallback", handlediscordCallback)
r.HandleFunc("/playsound", handlePlaySound)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.Handle("/", r) http.Handle("/", r)
http.ListenAndServe(":"+port, nil) 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) { func handleMain(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name") session, _ := store.Get(r, "session-name")
if session.Values["discordUsername"] != nil { if session.Values["discordUsername"] != nil {