Add command to post random mixes

pull/20/head
pocketjawa 2019-08-06 22:51:53 -04:00
parent ed09ca153c
commit 8ff9042596
2 changed files with 31 additions and 0 deletions

View File

@ -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 := ""

View File

@ -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)