Skip to content

Commit

Permalink
feat(discord-bot): add sending message to Discord when Twitch API is …
Browse files Browse the repository at this point in the history
…unauthorized when checking Twitch streamers
  • Loading branch information
barbequeorbarbecue committed Dec 15, 2024
1 parent 42af683 commit d58ada7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
)

require (
github.com/bensch777/discord-webhook-golang v0.0.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/bensch777/discord-webhook-golang v0.0.6 h1:91BMU6vKgymAMfRwtXPMUrKX+SUoPPHTDJHTFA/1Kgk=
github.com/bensch777/discord-webhook-golang v0.0.6/go.mod h1:GcIorMZAZaHZyQJkjNoYKvZ6VpZo8XLib/eD51xN7Is=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
17 changes: 17 additions & 0 deletions service/twitch/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strings"

discordwebhook "github.com/bensch777/discord-webhook-golang"
"github.com/senchabot-opensource/monorepo/model"
"golang.org/x/oauth2/clientcredentials"
"golang.org/x/oauth2/twitch"
Expand Down Expand Up @@ -149,6 +150,22 @@ func CheckMultipleTwitchStreamer(usernames []string) []model.TwitchStreamerData
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Println("[CheckMultipleTwitchStreamer] Twitch API request failed with status code:", resp.StatusCode)
if resp.StatusCode == http.StatusUnauthorized {
hook := discordwebhook.Hook{
Username: "Senchabot Webhook",
Avatar_url: "https://avatars.githubusercontent.com/u/94869947?v=4",
Content: "[CheckMultipleTwitchStreamer] Twitch API token is not authorized",
}

payload, err := json.Marshal(hook)
if err != nil {
log.Println("[CheckMultipleTwitchStreamer] Error while webhook json Marshal:", err.Error())
}
err = discordwebhook.ExecuteWebhook(os.Getenv("DISCORD_WEBHOOK_URL"), payload)
if err != nil {
log.Println("[CheckMultipleTwitchStreamer] ExecuteWebhook failed:", err.Error())
}
}
return nil
}

Expand Down

0 comments on commit d58ada7

Please sign in to comment.