Add XKCD command

pull/20/head
pocketjawa 2019-08-06 19:59:18 -04:00
parent 538db40037
commit 491f28236c
2 changed files with 32 additions and 0 deletions

View File

@ -3,8 +3,11 @@ package main
import (
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
"github.com/nishanths/go-xkcd"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
//Generate a heckin swear word
@ -37,3 +40,31 @@ func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
resp := "https://maki.jawa.moe/mixes/"
s.ChannelMessageSend(m.ChannelID, resp)
}
//Post XKCD comic!
func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
resp := ""
xkclient := xkcd.NewClient()
var err error
var comic xkcd.Comic
var arg string
if len(ctx.Fields) > 1 {
arg = ctx.Fields[1]
} else {
arg = "random"
}
matchedNum, _ := regexp.MatchString(`[0-9]+`, arg)
if arg == "latest" {
comic, err = xkclient.Latest()
} else if matchedNum {
comicNum, _ := strconv.Atoi(arg)
comic, err = xkclient.Get(comicNum)
} else {
comic, err = xkclient.Random()
}
if err != nil {
resp = err.Error()
}
resp = comic.ImageURL
s.ChannelMessageSend(m.ChannelID, 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("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD)
//Open a connection to Discord
err = Session.Open()