From f7f38788cf37ede1eef0ea165ba9028e54a39e11 Mon Sep 17 00:00:00 2001 From: Juan Llamas <38849891+xoltia@users.noreply.github.com> Date: Fri, 13 Oct 2023 11:59:35 -0500 Subject: [PATCH] Use errors.Is for comparing errors --- internal/bot/commands/chart.go | 4 ++-- internal/bot/commands/leaderboard.go | 4 ++-- internal/bot/commands/log.go | 18 +++++++++--------- internal/bot/commands/undo.go | 4 ++-- internal/guilds/guild.go | 3 ++- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/internal/bot/commands/chart.go b/internal/bot/commands/chart.go index 29d7fe7..c6ca8da 100644 --- a/internal/bot/commands/chart.go +++ b/internal/bot/commands/chart.go @@ -286,7 +286,7 @@ func (c *ChartCommand) Handle(ctx *bot.InteractionContext) error { guildID := ctx.Interaction().GuildID user, err := c.ur.FindByID(ctx.ResponseContext(), userID) - if err == pgx.ErrNoRows { + if errors.Is(err, pgx.ErrNoRows) { return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{ Content: "You have no activity!", }) @@ -301,7 +301,7 @@ func (c *ChartCommand) Handle(ctx *bot.InteractionContext) error { } else if guildID != "" { guild, err := c.gr.FindByID(ctx.ResponseContext(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } diff --git a/internal/bot/commands/leaderboard.go b/internal/bot/commands/leaderboard.go index e2c0698..241e147 100644 --- a/internal/bot/commands/leaderboard.go +++ b/internal/bot/commands/leaderboard.go @@ -116,7 +116,7 @@ func (c *LeaderboardCommand) Handle(ctx *bot.InteractionContext) error { user, err := c.u.FindByID(ctx.Context(), i.Member.User.ID) guildID := i.GuildID - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } @@ -127,7 +127,7 @@ func (c *LeaderboardCommand) Handle(ctx *bot.InteractionContext) error { } else if guildID != "" { guild, err := c.g.FindByID(ctx.ResponseContext(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } diff --git a/internal/bot/commands/log.go b/internal/bot/commands/log.go index d3d5ac5..6390234 100755 --- a/internal/bot/commands/log.go +++ b/internal/bot/commands/log.go @@ -324,13 +324,13 @@ func (c *LogCommand) Handle(ctx *bot.InteractionContext) error { func (c *LogCommand) handleAutocomplete(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) error { data := i.ApplicationCommandData() subcommand := data.Options[0].Name - focuedOption := discordutil.GetFocusedOption(data.Options[0].Options) + focusedOption := discordutil.GetFocusedOption(data.Options[0].Options) - if focuedOption == nil { + if focusedOption == nil { return nil } - if focuedOption.Name != "name" { + if focusedOption.Name != "name" { return nil } @@ -342,7 +342,7 @@ func (c *LogCommand) handleAutocomplete(ctx context.Context, s *discordgo.Sessio mediaType = activities.ActivityMediaTypeVisualNovel } - input := focuedOption.StringValue() + input := focusedOption.StringValue() results, err := c.createAutocompleteResult(ctx, mediaType, input) if err != nil { @@ -431,7 +431,7 @@ func (c *LogCommand) handleAnime(ctx *bot.InteractionContext, subcommand *discor } else if guildID != "" { guild, err := c.guildRepo.FindByID(ctx.Context(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } @@ -567,7 +567,7 @@ func (c *LogCommand) handleBook(ctx *bot.InteractionContext, subcommand *discord } else if guildID != "" { guild, err := c.guildRepo.FindByID(ctx.Context(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } @@ -712,7 +712,7 @@ func (c *LogCommand) handleVisualNovel(ctx *bot.InteractionContext, subcommand * } else if guildID != "" { guild, err := c.guildRepo.FindByID(ctx.Context(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } @@ -813,7 +813,7 @@ func (c *LogCommand) handleVideo(ctx *bot.InteractionContext, subcommand *discor } else if guildID != "" { guild, err := c.guildRepo.FindByID(ctx.Context(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } @@ -928,7 +928,7 @@ func (c *LogCommand) handleManual(ctx *bot.InteractionContext, subcommand *disco } else if guildID != "" { guild, err := c.guildRepo.FindByID(ctx.Context(), guildID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && !errors.Is(err, pgx.ErrNoRows) { return err } diff --git a/internal/bot/commands/undo.go b/internal/bot/commands/undo.go index 4e6e4d8..6325c65 100644 --- a/internal/bot/commands/undo.go +++ b/internal/bot/commands/undo.go @@ -49,7 +49,7 @@ func (c *UndoCommand) Handle(ctx *bot.InteractionContext) error { func (c *UndoCommand) undoActivity(ctx *bot.InteractionContext, id uint64) error { activity, err := c.r.GetByID(ctx.ResponseContext(), id, ctx.Interaction().GuildID) - if err == pgx.ErrNoRows { + if errors.Is(err, pgx.ErrNoRows) { return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{ Content: "Activity not found.", }) @@ -161,7 +161,7 @@ func (c *UndoCommand) undoLastActivity(ctx *bot.InteractionContext) error { userID := discordutil.GetInteractionUser(ctx.Interaction()).ID activity, err := c.r.GetLatestByUserID(ctx.ResponseContext(), userID, ctx.Interaction().GuildID) - if err == pgx.ErrNoRows { + if errors.Is(err, pgx.ErrNoRows) { return ctx.Respond(discordgo.InteractionResponseChannelMessageWithSource, &discordgo.InteractionResponseData{ Content: "You have no activities to undo.", }) diff --git a/internal/guilds/guild.go b/internal/guilds/guild.go index 40d5722..253910c 100644 --- a/internal/guilds/guild.go +++ b/internal/guilds/guild.go @@ -2,6 +2,7 @@ package guilds import ( "context" + "errors" "sync" "github.com/jackc/pgx/v5" @@ -97,7 +98,7 @@ func (r *GuildRepository) FindOrCreate(ctx context.Context, id string) (*Guild, guild, err := r.FindByID(ctx, id) if err != nil { - if err == pgx.ErrNoRows { + if errors.Is(err, pgx.ErrNoRows) { guild = NewGuild(id) err = r.Create(ctx, guild)