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

Prevent crash due to missing (optional) user info in a DM #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion discord_bot/discord_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
})
Expand Down
45 changes: 31 additions & 14 deletions imagine_queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -440,9 +450,9 @@ 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 = imagine.DiscordInteraction.Member.User.ID
newGeneration.MemberID = userID
newGeneration.SortOrder = 0

_, err = q.imageGenerationRepo.Create(context.Background(), newGeneration)
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -715,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)
Expand All @@ -725,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)

Expand All @@ -749,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,
Expand Down Expand Up @@ -790,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,
Expand Down Expand Up @@ -853,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,
Expand Down