From 8ff9042596750cd0342e5851e27495cf790c86d1 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 22:51:53 -0400 Subject: [PATCH] Add command to post random mixes --- commands.go | 30 ++++++++++++++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 31 insertions(+) diff --git a/commands.go b/commands.go index 1de9ed7..48afb5a 100644 --- a/commands.go +++ b/commands.go @@ -1,11 +1,13 @@ package main import ( + "encoding/json" "github.com/bwmarrin/discordgo" "github.com/bwmarrin/disgord/x/mux" "github.com/nishanths/go-xkcd" "io/ioutil" "log" + "math/rand" "net/http" "os" "regexp" @@ -59,6 +61,34 @@ func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { s.ChannelMessageSend(m.ChannelID, resp) } +//Post a link to a random mix +func getRandomMix(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + + //Define mix details + type Mix struct { + Name string `json:"name"` + Type string `json:"type"` + Mtime string `json:"mtime"` + Size int `json:"size"` + } + + mixresp, err := http.Get("https://maki.jawa.moe/mixesjson/") + if err != nil { + resp := "Error fetching mixes: " + err.Error() + s.ChannelMessageSend(m.ChannelID, resp) + return + } + defer mixresp.Body.Close() + + if mixresp.StatusCode == http.StatusOK { + var mixes []*Mix + err = json.NewDecoder(mixresp.Body).Decode(&mixes) + rmix := mixes[rand.Intn(len(mixes))] + resp := "https://maki.jawa.moe/mixes/" + rmix.Name + s.ChannelMessageSend(m.ChannelID, resp) + } +} + //Post XKCD comic! func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp := "" diff --git a/pocketbot.go b/pocketbot.go index 3ddd08f..8ec88ce 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -56,6 +56,7 @@ func main() { Router.Route("maki", "It's Maki Monday my dudes!", makiMonday) Router.Route("monday", "It's Maki Monday my dudes!", makiMonday) Router.Route("mixes", "Post the link to my fire mixtapes!", postMixes) + Router.Route("rmix", "Post a random mix!", getRandomMix) Router.Route("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD) Router.Route("restart", "Restart the bot.", restartBot) Router.Route("kill", "Restart the bot.", restartBot)