Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve guild when guild is not found in cache. #29

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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