feat: xkcd command

This commit is contained in:
Jack Merrill 2023-08-18 22:52:26 -05:00
parent 8e6ed1e9ce
commit ab852f001d
No known key found for this signature in database
GPG Key ID: B8E3CDF57DD80CA5
3 changed files with 159 additions and 1 deletions

View File

@ -0,0 +1,145 @@
package fun
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"github.com/jackmerrill/hampbot/internal/utils/config"
"github.com/jackmerrill/hampbot/internal/utils/embed"
"github.com/zekroTJA/shireikan"
)
type Xkcd struct {
Month string `json:"month"`
Num int64 `json:"num"`
Link string `json:"link"`
Year string `json:"year"`
News string `json:"news"`
SafeTitle string `json:"safe_title"`
Transcript string `json:"transcript"`
Alt string `json:"alt"`
Img string `json:"img"`
Title string `json:"title"`
Day string `json:"day"`
}
type XKCD struct {
}
func (c *XKCD) GetInvokes() []string {
return []string{"xkcd"}
}
func (c *XKCD) GetDescription() string {
return "Get a random, specific, or the latest XKCD comic"
}
func (c *XKCD) GetHelp() string {
return "`xkcd <number|latest|random>` - Get a random (default), specific, or the latest XKCD comic"
}
func (c *XKCD) GetGroup() string {
return config.GroupFun
}
func (c *XKCD) GetDomainName() string {
return "hamp.fun.xkcd"
}
func (c *XKCD) GetSubPermissionRules() []shireikan.SubPermission {
return nil
}
func (c *XKCD) IsExecutableInDMChannels() bool {
return true
}
func (c *XKCD) Exec(ctx shireikan.Context) error {
id := ctx.GetArgs().Get(0).AsString()
if id == "" {
id = "random"
}
var comic Xkcd
if id == "latest" {
res, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching the latest comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&comic)
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching the latest comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
} else if id == "random" {
latest, err := http.Get("https://xkcd.com/info.0.json")
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching a random comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
defer latest.Body.Close()
var tempComic Xkcd
err = json.NewDecoder(latest.Body).Decode(&tempComic)
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching a random comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
id = fmt.Sprintf("%d", rand.Intn((int(tempComic.Num)-1)+1))
res, err := http.Get(fmt.Sprintf("https://xkcd.com/%s/info.0.json", id))
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching a random comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&comic)
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching a random comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
} else {
res, err := http.Get(fmt.Sprintf("https://xkcd.com/%s/info.0.json", id))
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching the comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&comic)
if err != nil {
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewErrorEmbed(ctx).SetTitle("Error").SetDescription("An error occured while fetching the comic.").AddField("Error", err.Error(), false).MessageEmbed)
return err
}
}
ctx.GetSession().ChannelMessageSendEmbed(ctx.GetChannel().ID, embed.NewEmbed().
SetTitle(fmt.Sprintf("%s (%d)", comic.Title, comic.Num)).
SetDescription(comic.Alt).
SetImage(comic.Img).
SetColor(0x96A8C8).
SetURL(fmt.Sprintf("https://xkcd.com/%d", comic.Num)).
MessageEmbed)
return nil
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/bwmarrin/discordgo"
@ -69,7 +70,7 @@ func (c *Laundry) IsExecutableInDMChannels() bool {
}
func (c *Laundry) Exec(ctx shireikan.Context) error {
building := ctx.GetArgs().Get(0)
building := ctx.GetArgs().Get(0).AsString()
if building == "" {
ctx.GetSession().ChannelMessageSendComplex(ctx.GetChannel().ID, &discordgo.MessageSend{
@ -79,6 +80,8 @@ func (c *Laundry) Exec(ctx shireikan.Context) error {
return nil
}
building = strings.ToLower(building)
if building != "dakin" && building != "merrill" && building != "prescott" && building != "enfield" {
ctx.GetSession().ChannelMessageSendComplex(ctx.GetChannel().ID, &discordgo.MessageSend{
Embed: embed.NewErrorEmbed(ctx).SetDescription("Please specify a valid building to get the laundry status of.\n\n**Usage:** `laundry [dakin/merrill/prescott/enfield]`").MessageEmbed,

10
main.go
View File

@ -1,9 +1,11 @@
package main
import (
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/jackmerrill/hampbot/internal/commands/fun"
@ -47,6 +49,9 @@ func main() {
},
})
log.Info("Initializing Random")
rand.Seed(time.Now().UnixNano())
log.Info("Registering commands...")
handler.Register(&util.Ping{})
@ -61,6 +66,11 @@ func main() {
handler.Register(&fun.AI{})
log.Debug("Registered ai command")
handler.Register(&fun.XKCD{})
log.Debug("Registered xkcd command")
log.Info("Registered all commands")
handler.Setup(session)
log.Info("Bot is now running. Press CTRL-C to exit.")