Skip to content

Commit

Permalink
Merge pull request #9 from Esfands/8-dad-joke
Browse files Browse the repository at this point in the history
Added dad joke command
  • Loading branch information
Mahcks authored Jul 13, 2024
2 parents 069fb1f + 0f08438 commit 71ed69a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/bot/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"github.com/esfands/retpaladinbot/internal/bot/commands/accountage"
"github.com/esfands/retpaladinbot/internal/bot/commands/dadjoke"
"github.com/esfands/retpaladinbot/internal/bot/commands/followage"
"github.com/esfands/retpaladinbot/internal/bot/commands/game"
"github.com/esfands/retpaladinbot/internal/bot/commands/ping"
Expand Down Expand Up @@ -41,5 +42,6 @@ func (cm *CommandManager) loadDefaultCommands() []domain.DefaultCommand {
game.NewGameCommand(cm.gctx),
followage.NewFollowageCommand(cm.gctx),
uptime.NewUptimeCommand(cm.gctx),
dadjoke.NewDadJokeCommand(cm.gctx),
}
}
86 changes: 86 additions & 0 deletions internal/bot/commands/dadjoke/dadjoke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dadjoke

import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/dghubble/sling"
"github.com/esfands/retpaladinbot/internal/global"
"github.com/esfands/retpaladinbot/pkg/domain"
"github.com/esfands/retpaladinbot/pkg/utils"
"github.com/gempir/go-twitch-irc/v4"
)

type DadJokeCommand struct {
gctx global.Context
}

func NewDadJokeCommand(gctx global.Context) *DadJokeCommand {
return &DadJokeCommand{
gctx: gctx,
}
}

func (c *DadJokeCommand) Name() string {
return "dadjoke"
}

func (c *DadJokeCommand) Aliases() []string {
return []string{}
}

func (c *DadJokeCommand) Description() string {
return "Get a dad joke 4Head"
}

func (c *DadJokeCommand) Conditions() domain.DefaultCommandConditions {
return domain.DefaultCommandConditions{
EnabledOnline: true,
EnabledOffline: true,
}
}

func (c *DadJokeCommand) UserCooldown() int {
return 10
}

func (c *DadJokeCommand) GlobalCooldown() int {
return 30
}

func (c *DadJokeCommand) Code(user twitch.User, context []string) (string, error) {
target := utils.GetTarget(user, context)

url := "https://icanhazdadjoke.com/"

s := sling.New().Base(url).Set("Accept", "application/json")
req, err := s.New().Get("/").Request()
if err != nil {
slog.Error("Failed to create dad joke request", "error", err)
return fmt.Sprintf("@%v failed to get a dad joke. FeelsBadMan", user.Name), err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
slog.Error("Failed to get dad joke", "error", err)
return fmt.Sprintf("@%v failed to get a dad joke. FeelsBadMan", user.Name), err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("Failed to read dad joke response body", "error", err)
return fmt.Sprintf("@%v failed to get a dad joke. FeelsBadMan", user.Name), err
}

var joke DadJokeResponse
if err := json.Unmarshal(body, &joke); err != nil {
slog.Error("Failed to unmarshal dad joke response", "error", err)
return fmt.Sprintf("@%v failed to get a dad joke. FeelsBadMan", user.Name), err
}

return fmt.Sprintf("@%v %v", target, joke.Joke), nil
}
7 changes: 7 additions & 0 deletions internal/bot/commands/dadjoke/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dadjoke

type DadJokeResponse struct {
ID string `json:"id"`
Joke string `json:"joke"`
Status int `json:"status"`
}

0 comments on commit 71ed69a

Please sign in to comment.