1
1
import { AUTO1111 , ImagineOptions } from "./auto1111.ts"
2
- import {
3
- createBot ,
4
- Intents ,
5
- startBot ,
6
- CreateSlashApplicationCommand ,
7
- InteractionResponseTypes ,
8
- ApplicationCommandOptionTypes ,
9
- } from "./deps.ts"
2
+ import { createCommands , registerCommands } from "./command.ts"
3
+ import { createBot , Intents , startBot , InteractionResponseTypes } from "./deps.ts"
4
+ import { log } from "./log.ts"
10
5
import { Secret } from "./secret.ts"
11
6
import { base64ToBlob } from "./utils.ts"
12
7
@@ -16,12 +11,6 @@ const client = new AUTO1111({
16
11
host : Secret . AUTO1111_Host ,
17
12
} )
18
13
19
- const [ sdModels , samplers , promptStyles ] = await Promise . all ( [
20
- client . sdModels ( ) ,
21
- client . samplers ( ) ,
22
- client . promptStyles ( ) ,
23
- ] )
24
-
25
14
const bot = createBot ( {
26
15
token : DISCORD_TOKEN ,
27
16
intents : Intents . Guilds | Intents . GuildMessages | Intents . MessageContent | Intents . GuildMembers ,
@@ -32,118 +21,24 @@ const bot = createBot({
32
21
} ,
33
22
} )
34
23
35
- const switchModelCommand : CreateSlashApplicationCommand = {
36
- name : "switch" ,
37
- description : "Switch stable diffusion model" ,
38
- options : [
39
- {
40
- type : ApplicationCommandOptionTypes . String ,
41
- name : "name" ,
42
- description : "Stable diffusion model name" ,
43
- choices : ( sdModels . length > 25 ? sdModels . slice ( 0 , 25 ) : sdModels ) . map ( ( model ) => ( {
44
- name : model . model_name ,
45
- value : model . title ,
46
- } ) ) ,
47
- required : true ,
48
- } ,
49
- ] ,
50
- }
24
+ const refreshParameters = async ( ) => {
25
+ await client . refreshCheckpoints ( )
51
26
52
- const imagineCommand : CreateSlashApplicationCommand = {
53
- name : "imagine" ,
54
- description : "Thinking to the world of dreams..." ,
55
- options : [
56
- {
57
- type : ApplicationCommandOptionTypes . String ,
58
- name : "prompt" ,
59
- description : "Positive prompt" ,
60
- required : true ,
61
- } ,
62
- {
63
- type : ApplicationCommandOptionTypes . String ,
64
- name : "negative" ,
65
- description : "Negative prompt" ,
66
- required : false ,
67
- } ,
68
- {
69
- type : ApplicationCommandOptionTypes . String ,
70
- name : "prompt-style" ,
71
- description : "Prompt style" ,
72
- choices : promptStyles . map ( ( style ) => ( {
73
- name : style . name ,
74
- value : style . name ,
75
- } ) ) ,
76
- required : false ,
77
- } ,
78
- {
79
- type : ApplicationCommandOptionTypes . String ,
80
- name : "aspect" ,
81
- description : "Aspect ratio" ,
82
- choices : [
83
- {
84
- name : "2:3" ,
85
- value : "2:3" ,
86
- } ,
87
- {
88
- name : "1:1" ,
89
- value : "1:1" ,
90
- } ,
91
- {
92
- name : "3:2" ,
93
- value : "3:2" ,
94
- } ,
95
- ] ,
96
- required : false ,
97
- } ,
98
- {
99
- type : ApplicationCommandOptionTypes . Integer ,
100
- name : "seed" ,
101
- description : "Like finding a single shining star in the vastness of space" ,
102
- required : false ,
103
- } ,
104
- {
105
- type : ApplicationCommandOptionTypes . String ,
106
- name : "sampler" ,
107
- description : "Sampling method" ,
108
- choices : ( samplers . length > 25 ? samplers . slice ( 0 , 25 ) : samplers ) . map ( ( sampler ) => ( {
109
- name : sampler . name ,
110
- value : sampler . name ,
111
- } ) ) ,
112
- required : false ,
113
- } ,
114
- {
115
- type : ApplicationCommandOptionTypes . Integer ,
116
- name : "steps" ,
117
- description : "Number of steps" ,
118
- required : false ,
119
- } ,
120
- {
121
- type : ApplicationCommandOptionTypes . Number ,
122
- name : "scale" ,
123
- description : "CFG scale" ,
124
- required : false ,
125
- } ,
126
- {
127
- type : ApplicationCommandOptionTypes . Integer ,
128
- name : "count" ,
129
- description : "Number of images to generate" ,
130
- required : false ,
131
- minValue : 1 ,
132
- maxValue : 4 ,
133
- } ,
134
- ] ,
27
+ return await Promise . all ( [ client . sdModels ( ) , client . samplers ( ) , client . promptStyles ( ) ] )
135
28
}
136
29
137
- const commands = [ imagineCommand , switchModelCommand ]
30
+ const [ _sdModels , _samplers , promptStyles ] = await refreshParameters ( )
31
+
32
+ const refreshCommands = async ( ) => {
33
+ const commands = createCommands ( ...( await refreshParameters ( ) ) )
34
+ await registerCommands ( bot , commands , false )
35
+ log . info ( "Commands refreshed" )
36
+ }
138
37
139
- await Promise . all (
140
- commands . map ( ( command ) => {
141
- bot . helpers . createGuildApplicationCommand ( command , Secret . GUILD_ID )
142
- // bot.helpers.createGlobalApplicationCommand(command)
143
- } )
144
- )
145
- await bot . helpers . upsertGuildApplicationCommands ( Secret . GUILD_ID , commands )
146
- // await bot.helpers.upsertGlobalApplicationCommands(commands)
38
+ bot . events . ready = async ( ) => {
39
+ log . success ( "Successfully connected to gateway" )
40
+ await refreshCommands ( )
41
+ }
147
42
148
43
bot . events . interactionCreate = async ( b , interaction ) => {
149
44
switch ( interaction . data ?. name ) {
@@ -163,6 +58,8 @@ bot.events.interactionCreate = async (b, interaction) => {
163
58
return
164
59
}
165
60
61
+ log . info ( `Switching model from ${ auto1111options . sd_model_checkpoint } to ${ model ?. value } ...` )
62
+
166
63
await b . helpers . sendInteractionResponse ( interaction . id , interaction . token , {
167
64
type : InteractionResponseTypes . ChannelMessageWithSource ,
168
65
data : {
@@ -176,6 +73,8 @@ bot.events.interactionCreate = async (b, interaction) => {
176
73
content : `✅ Model switched to **${ model ?. value } ** successfully!` ,
177
74
} )
178
75
76
+ log . info ( `Model switched to ${ model ?. value } successfully!` )
77
+
179
78
break
180
79
}
181
80
case "imagine" : {
@@ -213,6 +112,8 @@ bot.events.interactionCreate = async (b, interaction) => {
213
112
count : interaction . data . options ?. find ( ( o ) => o . name === "count" ) ?. value as number ,
214
113
}
215
114
115
+ log . info ( "Imagine:" , options )
116
+
216
117
const promptStyleName = interaction . data . options ?. find ( ( o ) => o . name === "prompt-style" ) ?. value
217
118
if ( typeof promptStyleName === "string" ) {
218
119
const promptStyle = promptStyles . find ( ( style ) => style . name === promptStyleName )
@@ -237,6 +138,9 @@ bot.events.interactionCreate = async (b, interaction) => {
237
138
while ( ! finished ) {
238
139
await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) )
239
140
const progress = await client . progress ( )
141
+ if ( progress . progress === 0 ) {
142
+ continue
143
+ }
240
144
await b . helpers . editOriginalInteractionResponse ( interaction . token , {
241
145
content : `${ paramerters } \n${ ( progress . progress * 100 ) . toFixed (
242
146
1
@@ -259,6 +163,26 @@ bot.events.interactionCreate = async (b, interaction) => {
259
163
260
164
break
261
165
}
166
+ case "refresh" : {
167
+ log . info ( "Refreshing..." )
168
+
169
+ await b . helpers . sendInteractionResponse ( interaction . id , interaction . token , {
170
+ type : InteractionResponseTypes . ChannelMessageWithSource ,
171
+ data : {
172
+ content : "Refreshing..." ,
173
+ } ,
174
+ } )
175
+
176
+ await refreshCommands ( )
177
+
178
+ await b . helpers . editOriginalInteractionResponse ( interaction . token , {
179
+ content : "✅ Refreshed!" ,
180
+ } )
181
+
182
+ log . info ( "Refreshed" )
183
+
184
+ break
185
+ }
262
186
default : {
263
187
break
264
188
}
0 commit comments