discord-pocketbot-go/commands.go

103 lines
2.4 KiB
Go

package main
import (
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
"github.com/nishanths/go-xkcd"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
)
//Check member roles
func checkRole(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context, roleid string) bool {
if ctx.IsPrivate {
return false
}
channel, _ := s.Channel(m.ChannelID)
guild := channel.GuildID
member, _ := s.GuildMember(guild, m.Author.ID)
for _, role := range member.Roles {
if role == roleid {
return true
}
}
return false
}
//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)
}
//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)
}
//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)
}
//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)
}
//Restart the bot
func restartBot(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
if !checkRole(s, m, ctx, os.Getenv("POCKETBOT_ADMIN_ROLE")) {
resp := "OwO you aren't my daddy..."
s.ChannelMessageSend(m.ChannelID, resp)
return
}
resp := "Goodnight ;-;"
s.ChannelMessageSend(m.ChannelID, resp)
Session.Close()
log.Println("brb dying at the request of ", m.Author.String())
os.Exit(0)
}