diff --git a/command.go b/command.go index ef4e2fc..e8662c8 100644 --- a/command.go +++ b/command.go @@ -1,8 +1,6 @@ package bot import ( - "errors" - "github.com/bwmarrin/discordgo" "github.com/rs/zerolog/log" ) @@ -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 } @@ -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 } @@ -112,12 +103,7 @@ 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 } @@ -125,12 +111,7 @@ func (wb *WordleBot) handleWordleGuess(s *discordgo.Session, i *discordgo.Intera 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 } @@ -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) } @@ -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 } @@ -180,41 +151,32 @@ 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 } @@ -222,12 +184,11 @@ func (wb *WordleBot) handleWordleShow(s *discordgo.Session, i *discordgo.Interac 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 } diff --git a/utils.go b/utils.go index c7561ef..7f2bff6 100644 --- a/utils.go +++ b/utils.go @@ -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 @@ -32,11 +42,27 @@ 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, } } @@ -44,7 +70,7 @@ func errorEmbed(err error) *discordgo.MessageEmbed { return &discordgo.MessageEmbed{ Title: "Error", Description: err.Error(), - Color: 0xED4245, + Color: wordleRed, } } @@ -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)))) }