From ab852f001d20558e16a0a3725ffcc567c3c34b39 Mon Sep 17 00:00:00 2001 From: Jack Merrill Date: Fri, 18 Aug 2023 22:52:26 -0500 Subject: [PATCH] feat: xkcd command --- internal/commands/fun/xkcd.go | 145 +++++++++++++++++++++++ internal/commands/studentlife/laundry.go | 5 +- main.go | 10 ++ 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 internal/commands/fun/xkcd.go diff --git a/internal/commands/fun/xkcd.go b/internal/commands/fun/xkcd.go new file mode 100644 index 0000000..795f39f --- /dev/null +++ b/internal/commands/fun/xkcd.go @@ -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 ` - 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 +} diff --git a/internal/commands/studentlife/laundry.go b/internal/commands/studentlife/laundry.go index 81480e8..333ae50 100644 --- a/internal/commands/studentlife/laundry.go +++ b/internal/commands/studentlife/laundry.go @@ -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, diff --git a/main.go b/main.go index 77cdb4e..3ee2a15 100644 --- a/main.go +++ b/main.go @@ -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.")