-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupscale.go
68 lines (54 loc) · 1.54 KB
/
upscale.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package midjourney
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"golang.org/x/exp/slices"
)
func (c *Client) Upscale(ctx context.Context, msg *Message, index Upscale) (*Image, error) {
fmt.Printf("Upscaling: %v\n", index)
var customID *string
for _, mc := range msg.Components {
indexComponent := slices.IndexFunc[MessageComponentNestedComponent](mc.Components, func(mc MessageComponentNestedComponent) bool {
return index == mc.Label
})
if indexComponent >= 0 {
customID = &mc.Components[indexComponent].CustomID
break
}
}
if customID == nil {
return nil, errors.New("couldn't find upscale action")
}
payload := InteractionPayload[UpscalePayloadData]{
Type: 3,
ApplicationID: c.MidJourneyApplicationID,
GuildID: c.GuildID,
ChannelID: c.ChannelID,
SessionID: c.MidJourneySessionID,
MessageID: msg.ID,
MessageFlags: 0,
Data: UpscalePayloadData{
ComponentType: 2,
CustomID: *customID,
},
}
payloadRequest, err := json.Marshal(payload)
if err != nil {
return nil, err
}
err = c.Request(ctx, http.MethodPost, Endpoint_Interactions, &payloadRequest, nil)
if err != nil {
return nil, err
}
fmt.Printf("upscaling finished: %v\n", index)
const keywordFinishedDetection = "Image #"
occurenceKeyword := strings.Count(msg.Content, keywordFinishedDetection)
return c.GetImage(ctx, func(msgIter *Message) bool {
occurenceKeywordIter := strings.Count(msgIter.Content, keywordFinishedDetection)
return occurenceKeywordIter > occurenceKeyword
})
}