diff --git a/README.md b/README.md index bb9b2fc..8f6cfba 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ * @Date: 2023-08-17 18:23:21 * @version: * @LastEditors: SpenserCai - * @LastEditTime: 2023-08-23 17:58:04 + * @LastEditTime: 2023-08-27 00:37:58 * @Description: file content -->
@@ -146,6 +146,22 @@ Edit the `config.json` file and fill in the token and other information. } ``` +If you want set default value with sd-webui +```json +{ + "sd_webui":{ + "servers":[...], + "default_setting": { + "cfg_scale": 8, + "negative_prompt": "bad,text,watermask", + "height":1024, + "width":1024 + } + } + ... +} +``` + Start The Bot ```bash # if you can't connect discord,you need use proxy and run this command: diff --git a/config/config.go b/config/config.go index 929eabc..d7d5291 100644 --- a/config/config.go +++ b/config/config.go @@ -3,7 +3,7 @@ * @Date: 2023-08-16 11:05:40 * @version: * @LastEditors: SpenserCai - * @LastEditTime: 2023-08-17 13:39:42 + * @LastEditTime: 2023-08-27 00:01:34 * @Description: file content */ package config @@ -18,10 +18,18 @@ type ServerItem struct { type Config struct { SDWebUi struct { - Servers []ServerItem `json:"servers"` + Servers []ServerItem `json:"servers"` + DefaultSetting DefaultSetting `json:"default_setting"` } `json:"sd_webui"` Discord struct { Token string `json:"token"` ServerId string `json:"server_id"` } `json:"discord"` } + +type DefaultSetting struct { + CfgScale float64 `json:"cfg_scale"` + NegativePrompt string `json:"negative_prompt"` + Height int64 `json:"height"` + Width int64 `json:"width"` +} diff --git a/dbot/slash_handler/base.go b/dbot/slash_handler/base.go index 60e9a26..13acece 100644 --- a/dbot/slash_handler/base.go +++ b/dbot/slash_handler/base.go @@ -3,7 +3,7 @@ * @Date: 2023-08-17 09:52:25 * @version: * @LastEditors: SpenserCai - * @LastEditTime: 2023-08-23 15:49:22 + * @LastEditTime: 2023-08-27 00:29:05 * @Description: file content */ package slash_handler @@ -11,7 +11,9 @@ package slash_handler import ( "encoding/json" "fmt" + "reflect" + "github.com/SpenserCai/sd-webui-discord/global" "github.com/SpenserCai/sd-webui-discord/utils" "github.com/SpenserCai/sd-webui-go/intersvc" @@ -80,3 +82,27 @@ func (shdl SlashHandler) GetControlNetScript(jsonStr string) (*intersvc.Controln return script, nil } + +// Only Step 1,will be change to support every user every setting +func (shdl SlashHandler) GetSdDefaultSetting(key string, defaultValue interface{}) interface{} { + // 把global.Config.SDWebUi.DefaultSetting转成map[string]interface{} + defaultSettingMap := make(map[string]interface{}) + defaultSettingJson, _ := json.Marshal(global.Config.SDWebUi.DefaultSetting) + json.Unmarshal(defaultSettingJson, &defaultSettingMap) + + // 判断key是否被赋值如果没有返回defaultValue + keyValue, ok := defaultSettingMap[key] + if ok && utils.IsZeroValue(keyValue) { + return defaultValue + } else { + defaultValueType := reflect.TypeOf(defaultValue) + if defaultValueType.Kind() == reflect.Ptr { + defaultValueType = defaultValueType.Elem() + } + if keyValue != nil && reflect.TypeOf(keyValue) != defaultValueType { + convertedValue := reflect.ValueOf(keyValue).Convert(defaultValueType).Interface() + return convertedValue + } + return keyValue + } +} diff --git a/dbot/slash_handler/txt2img.go b/dbot/slash_handler/txt2img.go index d595cdb..769368e 100644 --- a/dbot/slash_handler/txt2img.go +++ b/dbot/slash_handler/txt2img.go @@ -3,7 +3,7 @@ * @Date: 2023-08-22 17:13:19 * @version: * @LastEditors: SpenserCai - * @LastEditTime: 2023-08-23 15:49:31 + * @LastEditTime: 2023-08-27 00:31:13 * @Description: file content */ package slash_handler @@ -109,17 +109,18 @@ func (shdl SlashHandler) Txt2imgOptions() *discordgo.ApplicationCommand { } func (shdl SlashHandler) Txt2imgSetOptions(dsOpt []*discordgo.ApplicationCommandInteractionDataOption, opt *intersvc.SdapiV1Txt2imgRequest) { - opt.NegativePrompt = "" - opt.Height = func() *int64 { v := int64(512); return &v }() - opt.Width = func() *int64 { v := int64(512); return &v }() + opt.NegativePrompt = shdl.GetSdDefaultSetting("negative_prompt", "").(string) + opt.Height = func() *int64 { v := shdl.GetSdDefaultSetting("height", int64(512)).(int64); return &v }() + opt.Width = func() *int64 { v := shdl.GetSdDefaultSetting("width", int64(512)).(int64); return &v }() opt.SamplerIndex = func() *string { v := "Euler"; return &v }() opt.Steps = func() *int64 { v := int64(20); return &v }() - opt.CfgScale = func() *float64 { v := 7.0; return &v }() + opt.CfgScale = func() *float64 { v := shdl.GetSdDefaultSetting("cfg_scale", 7.0).(float64); return &v }() opt.Seed = func() *int64 { v := int64(-1); return &v }() opt.NIter = func() *int64 { v := int64(1); return &v }() opt.Styles = []string{} opt.ScriptArgs = []interface{}{} opt.AlwaysonScripts = map[string]interface{}{} + opt.OverrideSettings = map[string]interface{}{} for _, v := range dsOpt { switch v.Name { @@ -167,6 +168,7 @@ func (shdl SlashHandler) Txt2imgAction(s *discordgo.Session, i *discordgo.Intera }) } else { files := make([]*discordgo.File, 0) + outinfo := txt2img.GetResponse().Info for j, v := range txt2img.GetResponse().Images { imageReader, err := utils.GetImageReaderByBase64(v) if err != nil { @@ -185,7 +187,7 @@ func (shdl SlashHandler) Txt2imgAction(s *discordgo.Session, i *discordgo.Intera files = files[0:4] } s.FollowupMessageEdit(i.Interaction, msg.ID, &discordgo.WebhookEdit{ - Content: func() *string { v := "Success"; return &v }(), + Content: func() *string { v := fmt.Sprintf("```\n%v```\n", *outinfo); return &v }(), Files: files, }) } diff --git a/utils/encoding.go b/utils/encoding.go index 9f02490..cf3ecfb 100644 --- a/utils/encoding.go +++ b/utils/encoding.go @@ -3,7 +3,7 @@ * @Date: 2023-08-17 00:40:47 * @version: * @LastEditors: SpenserCai - * @LastEditTime: 2023-08-23 14:52:23 + * @LastEditTime: 2023-08-26 23:58:29 * @Description: file content */ package utils @@ -11,6 +11,7 @@ package utils import ( "encoding/base64" "encoding/json" + "reflect" "strings" "golang.org/x/text/cases" @@ -42,3 +43,14 @@ func IsJsonString(str string) bool { var js map[string]interface{} return json.Unmarshal([]byte(str), &js) == nil } + +func IsZeroValue(value interface{}) bool { + if value == nil { + return true + } + + reflectValue := reflect.ValueOf(value) + zeroValue := reflect.Zero(reflectValue.Type()).Interface() + + return reflect.DeepEqual(value, zeroValue) +}