Skip to content

Commit

Permalink
feat: 🥅 better error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhanio committed Jan 26, 2022
1 parent 7ff0f5b commit aa7b5fc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 81 deletions.
105 changes: 33 additions & 72 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bot

import (
"errors"

"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -69,12 +67,7 @@ func (wb *WordleBot) handleWordle(s *discordgo.Session, i *discordgo.Interaction
func (wb *WordleBot) handleWordleStart(s *discordgo.Session, i *discordgo.InteractionCreate) {
_, ok := wb.wordles[i.GuildID][i.Member.User.ID]
if ok {
err := errorRespond(s, i, errors.New("A wordle is already in progress. Use `/cancel` to cancel it."))
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
warningRespond(s, i, "A wordle is already in progress. Use `/cancel` to cancel it.")

return
}
Expand All @@ -86,24 +79,22 @@ func (wb *WordleBot) handleWordleStart(s *discordgo.Session, i *discordgo.Intera

wg, err := wb.newWordleGame(i, wordLength)
if err != nil {
err := errorRespond(s, i, err)
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
log.Error().
Err(err).
Msg("Failed to create wordle game")

errorRespond(s, i, err)

return
}

err = wg.responseCreate()
if err != nil {
err := errorRespond(s, i, err)
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
log.Error().
Err(err).
Msg("Failed to create response")

errorRespond(s, i, err)

return
}
Expand All @@ -112,25 +103,15 @@ func (wb *WordleBot) handleWordleStart(s *discordgo.Session, i *discordgo.Intera
func (wb *WordleBot) handleWordleGuess(s *discordgo.Session, i *discordgo.InteractionCreate) {
wg, ok := wb.wordles[i.GuildID][i.Member.User.ID]
if !ok {
err := errorRespond(s, i, errors.New("No wordle in progress. Use `/start` to start a new game."))
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
warningRespond(s, i, "No wordle in progress. Use `/start` to start a new game.")

return
}

guess := i.ApplicationCommandData().Options[0].StringValue()
_, err := wg.Guess(guess)
if err != nil {
err := errorRespond(s, i, err)
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
errorRespond(s, i, err)

return
}
Expand All @@ -143,18 +124,13 @@ func (wb *WordleBot) handleWordleGuess(s *discordgo.Session, i *discordgo.Intera
Msg("Failed to edit interaction")
}

return
}

err = successRespond(s, i, "Guess accepted")
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
errorRespond(s, i, err)

return
}

successRespond(s, i, "Guess accepted")

if wg.Done() {
delete(wb.wordles[i.GuildID], i.Member.User.ID)
}
Expand All @@ -163,12 +139,7 @@ func (wb *WordleBot) handleWordleGuess(s *discordgo.Session, i *discordgo.Intera
func (wb *WordleBot) handleWordleCancel(s *discordgo.Session, i *discordgo.InteractionCreate) {
wg, ok := wb.wordles[i.GuildID][i.Member.User.ID]
if !ok {
err := errorRespond(s, i, errors.New("No wordle in progress. Use `/start` to start a new game."))
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
warningRespond(s, i, "No wordle in progress. Use `/start` to start a new game.")

return
}
Expand All @@ -180,54 +151,44 @@ func (wb *WordleBot) handleWordleCancel(s *discordgo.Session, i *discordgo.Inter
log.Error().
Err(err).
Msg("Failed to delete interaction")
}

delete(wb.wordles[i.GuildID], i.Member.User.ID)

err = successRespond(s, i, "Wordle canceled")
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
errorRespond(s, i, err)

return
}

delete(wb.wordles[i.GuildID], i.Member.User.ID)

successRespond(s, i, "Wordle cancelled")
}

func (wb *WordleBot) handleWordleShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
wg, ok := wb.wordles[i.GuildID][i.Member.User.ID]
if !ok {
err := errorRespond(s, i, errors.New("No wordle in progress. Use `/start` to start a new game."))
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
warningRespond(s, i, "No wordle in progress. Use `/start` to start a new game.")

return
}

err := wg.responseDelete()
if err != nil {
err := errorRespond(s, i, err)
if err != nil {
log.Error().
Err(err).
Msg("Failed to delete interaction")
}
log.Error().
Err(err).
Msg("Failed to delete response")

errorRespond(s, i, err)

return
}

wg.setInteraction(i)
err = wg.responseCreate()
if err != nil {
err := errorRespond(s, i, err)
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
log.Error().
Err(err).
Msg("Failed to create response")

errorRespond(s, i, err)

return
}
Expand Down
53 changes: 44 additions & 9 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package bot

import "github.com/bwmarrin/discordgo"
import (
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
)

const ephemeralFlag = 1 << 6

const (
wordleGreen = 0x7AB457
wordleYellow = 0xF2E8B3
wordleBlack = 0x293137
wordleRed = 0xDF2640
)

func ephemeralify(r *discordgo.InteractionResponse) *discordgo.InteractionResponse {
r.Data.Flags |= ephemeralFlag
return r
Expand Down Expand Up @@ -32,19 +42,35 @@ func embedResponse(es ...*discordgo.MessageEmbed) *discordgo.InteractionResponse
}
}

func embed(title string, description string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: title,
Description: description,
Color: wordleBlack,
}
}

func successEmbed(m string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Success",
Description: m,
Color: 0x57F287,
Color: wordleGreen,
}
}

func warningEmbed(m string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Warning",
Description: m,
Color: wordleYellow,
}
}

func errorEmbed(err error) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Error",
Description: err.Error(),
Color: 0xED4245,
Color: wordleRed,
}
}

Expand All @@ -60,14 +86,23 @@ func embedMessage(es ...*discordgo.MessageEmbed) *discordgo.MessageSend {
}
}

func respond(s *discordgo.Session, i *discordgo.InteractionCreate, r *discordgo.InteractionResponse) error {
return s.InteractionRespond(i.Interaction, r)
func respond(s *discordgo.Session, i *discordgo.InteractionCreate, r *discordgo.InteractionResponse) {
err := s.InteractionRespond(i.Interaction, r)
if err != nil {
log.Error().
Err(err).
Msg("Failed to respond to interaction")
}
}

func successRespond(s *discordgo.Session, i *discordgo.InteractionCreate, m string) {
respond(s, i, ephemeralify(embedResponse(successEmbed(m))))
}

func successRespond(s *discordgo.Session, i *discordgo.InteractionCreate, m string) error {
return respond(s, i, ephemeralify(embedResponse(successEmbed(m))))
func warningRespond(s *discordgo.Session, i *discordgo.InteractionCreate, m string) {
respond(s, i, ephemeralify(embedResponse(warningEmbed(m))))
}

func errorRespond(s *discordgo.Session, i *discordgo.InteractionCreate, err error) error {
return respond(s, i, ephemeralify(embedResponse(errorEmbed(err))))
func errorRespond(s *discordgo.Session, i *discordgo.InteractionCreate, err error) {
respond(s, i, ephemeralify(embedResponse(errorEmbed(err))))
}

0 comments on commit aa7b5fc

Please sign in to comment.