From 01c8e40e9a3ec5fa20705ba5296a6a495b23830b Mon Sep 17 00:00:00 2001 From: Stuart Riffle Date: Fri, 14 Apr 2023 21:31:34 -0700 Subject: [PATCH 1/2] Prevent crash due to missing (optional) user info in a DM --- discord_bot/discord_bot.go | 10 +++++++++- imagine_queue/queue.go | 26 ++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/discord_bot/discord_bot.go b/discord_bot/discord_bot.go index 20446d5..70eed16 100644 --- a/discord_bot/discord_bot.go +++ b/discord_bot/discord_bot.go @@ -326,13 +326,21 @@ func (b *botImpl) processImagineCommand(s *discordgo.Session, i *discordgo.Inter } } + userID := "" + + if i.Member != nil { + userID = i.Member.User.ID + } else if i.User != nil { + userID = i.User.ID + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: fmt.Sprintf( "I'm dreaming something up for you. You are currently #%d in line.\n<@%s> asked me to imagine \"%s\".", position, - i.Member.User.ID, + userID, prompt), }, }) diff --git a/imagine_queue/queue.go b/imagine_queue/queue.go index 85ed1df..9bd8fd2 100644 --- a/imagine_queue/queue.go +++ b/imagine_queue/queue.go @@ -416,22 +416,32 @@ func (q *queueImpl) getPreviousGeneration(imagine *QueueItem, sortOrder int) (*e return generation, nil } -func imagineMessageContent(generation *entities.ImageGeneration, user *discordgo.User, progress float64) string { +func imagineMessageContent(generation *entities.ImageGeneration, userID string, progress float64) string { if progress >= 0 && progress < 1 { return fmt.Sprintf("<@%s> asked me to imagine \"%s\". Currently dreaming it up for them. Progress: %.0f%%", - user.ID, generation.Prompt, progress*100) + userID, generation.Prompt, progress*100) } else { return fmt.Sprintf("<@%s> asked me to imagine \"%s\", here is what I imagined for them.", - user.ID, + userID, generation.Prompt, ) } } func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, imagine *QueueItem) error { - log.Printf("Processing imagine #%s: %v\n", imagine.DiscordInteraction.ID, newGeneration.Prompt) - newContent := imagineMessageContent(newGeneration, imagine.DiscordInteraction.Member.User, 0) + interactionID := imagine.DiscordInteraction.ID + userID := "" + + if imagine.DiscordInteraction.Member != nil && imagine.DiscordInteraction.Member.User != nil { + userID = imagine.DiscordInteraction.Member.User.ID + } else if imagine.DiscordInteraction.User != nil { + userID = imagine.DiscordInteraction.User.ID + } + + log.Printf("Processing imagine #%s: %v\n", interactionID, newGeneration.Prompt) + + newContent := imagineMessageContent(newGeneration, userID, 0) message, err := q.botSession.InteractionResponseEdit(imagine.DiscordInteraction, &discordgo.WebhookEdit{ Content: &newContent, @@ -442,7 +452,7 @@ func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, newGeneration.InteractionID = imagine.DiscordInteraction.ID newGeneration.MessageID = message.ID - newGeneration.MemberID = imagine.DiscordInteraction.Member.User.ID + newGeneration.MemberID = userID newGeneration.SortOrder = 0 _, err = q.imageGenerationRepo.Create(context.Background(), newGeneration) @@ -469,7 +479,7 @@ func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, continue } - progressContent := imagineMessageContent(newGeneration, imagine.DiscordInteraction.Member.User, progress.Progress) + progressContent := imagineMessageContent(newGeneration, userID, progress.Progress) _, progressErr = q.botSession.InteractionResponseEdit(imagine.DiscordInteraction, &discordgo.WebhookEdit{ Content: &progressContent, @@ -514,7 +524,7 @@ func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, generationDone <- true - finishedContent := imagineMessageContent(newGeneration, imagine.DiscordInteraction.Member.User, 1) + finishedContent := imagineMessageContent(newGeneration, userID, 1) log.Printf("Seeds: %v Subseeds:%v", resp.Seeds, resp.Subseeds) From e48fb7ca9c2dc6367d209e1c19fa20c89eada446 Mon Sep 17 00:00:00 2001 From: Stuart Riffle Date: Fri, 14 Apr 2023 21:48:42 -0700 Subject: [PATCH 2/2] Also fix the upscale/variation buttons (same error) --- imagine_queue/queue.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/imagine_queue/queue.go b/imagine_queue/queue.go index 9bd8fd2..95b6950 100644 --- a/imagine_queue/queue.go +++ b/imagine_queue/queue.go @@ -450,7 +450,7 @@ func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, log.Printf("Error editing interaction: %v", err) } - newGeneration.InteractionID = imagine.DiscordInteraction.ID + newGeneration.InteractionID = interactionID newGeneration.MessageID = message.ID newGeneration.MemberID = userID newGeneration.SortOrder = 0 @@ -725,7 +725,7 @@ func (q *queueImpl) processImagineGrid(newGeneration *entities.ImageGeneration, return nil } -func upscaleMessageContent(user *discordgo.User, fetchProgress, upscaleProgress float64) string { +func upscaleMessageContent(userID string, fetchProgress, upscaleProgress float64) string { if fetchProgress >= 0 && fetchProgress <= 1 && upscaleProgress < 1 { if upscaleProgress == 0 { return fmt.Sprintf("Currently upscaling the image for you... Fetch progress: %.0f%%", fetchProgress*100) @@ -735,18 +735,25 @@ func upscaleMessageContent(user *discordgo.User, fetchProgress, upscaleProgress } } else { return fmt.Sprintf("<@%s> asked me to upscale their image. Here's the result:", - user.ID) + userID) } } func (q *queueImpl) processUpscaleImagine(imagine *QueueItem) { interactionID := imagine.DiscordInteraction.ID messageID := "" + userID := "" if imagine.DiscordInteraction.Message != nil { messageID = imagine.DiscordInteraction.Message.ID } + if imagine.DiscordInteraction.Member != nil && imagine.DiscordInteraction.Member.User != nil { + userID = imagine.DiscordInteraction.Member.User.ID + } else if imagine.DiscordInteraction.User != nil { + userID = imagine.DiscordInteraction.User.ID + } + log.Printf("Upscaling image: %v, Message: %v, Upscale Index: %d", interactionID, messageID, imagine.InteractionIndex) @@ -759,7 +766,7 @@ func (q *queueImpl) processUpscaleImagine(imagine *QueueItem) { log.Printf("Found generation: %v", generation) - newContent := upscaleMessageContent(imagine.DiscordInteraction.Member.User, 0, 0) + newContent := upscaleMessageContent(userID, 0, 0) _, err = q.botSession.InteractionResponseEdit(imagine.DiscordInteraction, &discordgo.WebhookEdit{ Content: &newContent, @@ -800,7 +807,7 @@ func (q *queueImpl) processUpscaleImagine(imagine *QueueItem) { lastProgress = progress.Progress - progressContent := upscaleMessageContent(imagine.DiscordInteraction.Member.User, fetchProgress, upscaleProgress) + progressContent := upscaleMessageContent(userID, fetchProgress, upscaleProgress) _, progressErr = q.botSession.InteractionResponseEdit(imagine.DiscordInteraction, &discordgo.WebhookEdit{ Content: &progressContent, @@ -863,7 +870,7 @@ func (q *queueImpl) processUpscaleImagine(imagine *QueueItem) { interactionID, messageID, imagine.InteractionIndex) finishedContent := fmt.Sprintf("<@%s> asked me to upscale their image. Here's the result:", - imagine.DiscordInteraction.Member.User.ID) + userID) _, err = q.botSession.InteractionResponseEdit(imagine.DiscordInteraction, &discordgo.WebhookEdit{ Content: &finishedContent,