discord-pocketbot-go/commands.go

71 lines
1.6 KiB
Go
Raw Normal View History

2019-08-06 00:49:23 +00:00
package main
import (
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
2019-08-06 23:59:18 +00:00
"github.com/nishanths/go-xkcd"
2019-08-06 00:49:23 +00:00
"io/ioutil"
"net/http"
2019-08-06 23:59:18 +00:00
"regexp"
"strconv"
2019-08-06 00:49:23 +00:00
)
//Generate a heckin swear word
func getSwear(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
resp := ""
swear, err := http.Get("https://swear.jawa.moe/")
if err != nil {
resp = "Error fetching swear: " + err.Error()
}
defer swear.Body.Close()
if swear.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(swear.Body)
if err != nil {
resp = "Error fetching swear: " + err.Error()
}
resp = string(body)
}
s.ChannelMessageSend(m.ChannelID, resp)
}
2019-08-06 01:05:30 +00:00
//Post the Maki Monday pic!
func makiMonday(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
resp := "https://maki.jawa.moe/monday.png"
s.ChannelMessageSend(m.ChannelID, resp)
}
2019-08-06 01:10:18 +00:00
//Post link to mixes B)
func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
resp := "https://maki.jawa.moe/mixes/"
s.ChannelMessageSend(m.ChannelID, resp)
}
2019-08-06 23:59:18 +00:00
//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)
}