diff --git a/commands.go b/commands.go index 7d9afb3..175b48b 100644 --- a/commands.go +++ b/commands.go @@ -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) +} diff --git a/pocketbot.go b/pocketbot.go index 040386a..c8d689c 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("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD) //Open a connection to Discord err = Session.Open()