Skip to content

Commit

Permalink
fix: Resolve guild when guild is not found in cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
myrkvi committed Sep 30, 2024
1 parent 49aebd3 commit 7caae9c
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions commands/gatekeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ var ApproveSlashCommand = discord.SlashCommandCreate{
func ApproveUserCommandHandler(e *handler.CommandEvent) error {
utils.LogInteractionContext("approve", e, e.Ctx)

guild, inGuild := e.Guild()
guild, success, inGuild := getGuild(e)
if !inGuild {
slog.Warn("Approve command supplied in DMs or guild ID is otherwise nil",
"guild_id_is_nil", e.GuildID() == nil)
slog.Warn("approve command supplied in DMs or guild ID is otherwise nil")
return nil
}
if !success {
slog.Warn("approve command: failed to get guild")
return nil
}

member := e.UserCommandInteractionData().TargetMember()

return approvedInnerHandler(e, guild, member)
Expand All @@ -57,17 +61,43 @@ func ApproveUserCommandHandler(e *handler.CommandEvent) error {
func ApproveSlashCommandHandler(e *handler.CommandEvent) error {
utils.LogInteractionContext("Approve", e, e.Ctx)

guild, inGuild := e.Guild()
guild, success, inGuild := getGuild(e)
if !inGuild {
slog.Warn("Approve command supplied in DMs or guild ID is otherwise nil",
"guild_id_is_nil", e.GuildID() == nil)
slog.Warn("approve command supplied in DMs or guild ID is otherwise nil")
return nil
}
if !success {
slog.Warn("approve command: failed to get guild")
return nil
}

member := e.SlashCommandInteractionData().Member("user")

return approvedInnerHandler(e, guild, member)
}

func getGuild(e *handler.CommandEvent) (guild discord.Guild, success bool, inGuild bool) {
if e.GuildID() == nil {
return
}
inGuild = true
guild, success = e.Guild()

if success {
return
}

restGuild, err := e.Client().Rest().GetGuild(*e.GuildID(), false)
if err != nil {
slog.Warn("Failed to get guild", "guild_id", *e.GuildID(), "err", err)
return
}

guild = restGuild.Guild
success = true
return
}

func approvedInnerHandler(e *handler.CommandEvent, guild discord.Guild, member discord.ResolvedMember) error {
slog.InfoContext(e.Ctx, "Entered approvedInnerHandler")
err := e.DeferCreateMessage(true)
Expand Down

0 comments on commit 7caae9c

Please sign in to comment.