Skip to content

Commit

Permalink
Merge pull request #5 from venetanji/main
Browse files Browse the repository at this point in the history
Mainly docker and some ui
  • Loading branch information
SpenserCai authored Sep 22, 2023
2 parents 1dd15ac + 1ceb9ed commit 6ca161d
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data
output
config.json
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
release/*
release/*
data/*
output/*
config.json
tmp/*
.air.toml
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:latest

WORKDIR /usr/src/app

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .
RUN go build -v -o /usr/local/bin/app .

CMD ["app"]
41 changes: 41 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
volumes:
models:
extensions:

services:
app:
build: .
volumes:
- ./config.json:/usr/local/bin/config.json
- ./location:/usr/local/bin/location
- ./db/user_center.db:/usr/local/bin/user_center.db
app-dev:
image: cosmtrek/air
working_dir: /project
environment:
- GOFLAGS=-buildvcs=false
profiles:
- dev
volumes:
- ./config.json:/project/tmp/config.json
- ./location:/project/tmp/location
- ./:/project/

automatic:
image: goolashe/automatic1111-sd-webui
environment:
- CLI_ARGS=--allow-code --medvram --xformers --enable-insecure-extension-access --api --disable-safe-unpickle
ports:
- "${WEBUI_PORT:-7860}:7860"
volumes:
- ./data:/data
- ./output:/output
stop_signal: SIGKILL
tty: true
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0']
capabilities: [compute, utility]
11 changes: 5 additions & 6 deletions dbot/dbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

type DiscordBot struct {
AppCommand []*discordgo.ApplicationCommand
AddedCommand []*discordgo.ApplicationCommand
AppCommands []*discordgo.ApplicationCommand
RegisteredCommands []*discordgo.ApplicationCommand
Session *discordgo.Session
ServerID string
SlashHandlerMap map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
Expand All @@ -35,8 +35,7 @@ func NewDiscordBot(token string, serverID string) (*DiscordBot, error) {
Session: session,
ServerID: serverID,
SlashHandlerMap: make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)),
AppCommand: make([]*discordgo.ApplicationCommand, 0),
AddedCommand: make([]*discordgo.ApplicationCommand, 0),
AppCommands: make([]*discordgo.ApplicationCommand, 0),
}

// 预存长选项
Expand All @@ -61,13 +60,13 @@ func (d *DiscordBot) Run() {
log.Println(err)
return
}
d.AddCommand()
d.SyncCommands()
defer d.Session.Close()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
d.RemoveCommand()
//d.RemoveCommand()
log.Println("Gracefully shutting down.")
}
123 changes: 97 additions & 26 deletions dbot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,22 @@ func (dbot *DiscordBot) InteractionCreate(s *discordgo.Session, i *discordgo.Int

}

func (dbot *DiscordBot) AddCommand() {
dbot.ClearCommand()
log.Println("Adding commands...")
dbot.AddedCommand = make([]*discordgo.ApplicationCommand, len(dbot.AppCommand))
for i, v := range dbot.AppCommand {
log.Printf("Adding '%v' command...", v.Name)
cmd, err := dbot.Session.ApplicationCommandCreate(dbot.Session.State.User.ID, dbot.ServerID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
continue
}
log.Printf("Added '%v' command", v.Name)
dbot.AddedCommand[i] = cmd
}
}

func (dbot *DiscordBot) ClearCommand() {
func (dbot *DiscordBot) SyncCommands() {
commands, err := dbot.Session.ApplicationCommands(dbot.Session.State.User.ID, dbot.ServerID)
dbot.RegisteredCommands = commands
if err != nil {
log.Panicf("Cannot get commands: %v", err)
}
if len(commands) > 0 {
log.Println("Clearing commands...")
for _, v := range commands {
if len(dbot.RegisteredCommands) > 0 {
log.Println("Clearing other bots commands...")
for _, v := range dbot.RegisteredCommands {

// if command is not in the command list, remove it
if dbot.CheckCommandInList(v.Name) {
log.Printf("'%v' command is in the command list, skip...", v.Name)
continue
}
log.Printf("Removing '%v' command...", v.Name)
err := dbot.Session.ApplicationCommandDelete(dbot.Session.State.User.ID, dbot.ServerID, v.ID)
if err != nil {
log.Panicf("Cannot remove '%v' command: %v", v.Name, err)
Expand All @@ -77,15 +69,94 @@ func (dbot *DiscordBot) ClearCommand() {
}
}

}
log.Println("Adding/Updating commands...")
for _, v := range dbot.AppCommands {

func (dbot *DiscordBot) RemoveCommand() {
log.Println("Removing commands...")
for _, v := range dbot.AddedCommand {
err := dbot.Session.ApplicationCommandDelete(dbot.Session.State.User.ID, dbot.ServerID, v.ID)
// check if command needs update
if !dbot.CommandNeedsUpdate(v) {
log.Printf("'%v' command options are unchanged, skipping...", v.Name)
continue
}
// delete old version of command
for _, r := range dbot.RegisteredCommands {
if r.Name == v.Name {
err := dbot.Session.ApplicationCommandDelete(dbot.Session.State.User.ID, dbot.ServerID, r.ID)
if err != nil {
log.Panicf("Cannot remove '%v' command: %v", v.Name, err)
continue
}
}
}

// add new version of command
log.Printf("Adding '%v' command...", v.Name)
_, err := dbot.Session.ApplicationCommandCreate(dbot.Session.State.User.ID, dbot.ServerID, v)
if err != nil {
log.Panicf("Cannot remove '%v' command: %v", v.Name, err)
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
continue
}
}

}

func (dbot *DiscordBot) CheckCommandInList(name string) bool {
for _, v := range dbot.AppCommands {
if v.Name == name {
return true
}
}
return false
}

func (dbot *DiscordBot) CommandNeedsUpdate(command *discordgo.ApplicationCommand) bool {
for _, registeredCommand := range dbot.RegisteredCommands {
if registeredCommand.Name == command.Name {

// new description
if registeredCommand.Description != command.Description { return true }

// new options
if len(registeredCommand.Options) != len(command.Options) { return true }


for i, option := range command.Options {
// new option description
if option.Description != registeredCommand.Options[i].Description {
log.Println("Registered description '", registeredCommand.Options[i].Description, "' is different from command description '", option.Description, "' for command", command.Name)
return true
}

if option.Autocomplete {return true}

// new choices
for k, choice := range option.Choices {

if len(registeredCommand.Options[i].Choices) != len(option.Choices) {
log.Println("Length of choices is different for command", command.Name)
log.Println("Registered command:", registeredCommand.Options[i].Choices)
// print all the choices names and their description
for _, v := range command.Options[i].Choices {
log.Println("Command", v.Name, v.Value)
}
for _, v := range registeredCommand.Options[i].Choices {
log.Println("RegisteredCommand", v.Name, v.Value)
}
return true
}
if choice.Name == registeredCommand.Options[i].Choices[k].Name {
if choice.Value != registeredCommand.Options[i].Choices[k].Value {
continue
}
} else {
return false
}

}
// no new choices
}
// no new options or no options
return false
}
}
return true
}
2 changes: 1 addition & 1 deletion dbot/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (dbot *DiscordBot) SetLocation() error {
fmt.Println(err)
return err
}
for _, cmd := range dbot.AppCommand {
for _, cmd := range dbot.AppCommands {
AddLocationDescriptionMap(cmd)
for k, localCmdItemList := range locationMap {
locale := GetLocation(k)
Expand Down
27 changes: 14 additions & 13 deletions dbot/slash_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (

func (dbot *DiscordBot) GenerateSlashMap() error {
// 遍历AppCommands,取name
for _, v := range dbot.AppCommand {
for _, v := range dbot.AppCommands {
commandName := v.Name
// 如果name中有_则用下划线分割后每个首字母专大写,如果没有_则直接首字母转大写
commandName = utils.FormatCommand(commandName) + "CommandHandler"
log.Println("commandName:", commandName)
// 通过反射找到对应的方法赋值给map
pkgValue := reflect.ValueOf(slash_handler.SlashHandler{})
methodValue := pkgValue.MethodByName(commandName)
Expand All @@ -38,19 +39,19 @@ func (dbot *DiscordBot) GenerateSlashMap() error {
}

func (dbot *DiscordBot) GenerateCommandList() {
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.DeoldifyOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.SamOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.RembgOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.ExtraSingleOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.PngInfoOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.ControlnetDetectOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.RoopImageOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.Txt2imgOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.Img2imgOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.DeoldifyOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.SamOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.RembgOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.ExtraSingleOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.PngInfoOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.ControlnetDetectOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.RoopImageOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.Txt2imgOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.Img2imgOptions())
if global.Config.UserCenter.Enable {
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.RegisterOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.SettingOptions())
dbot.AppCommand = append(dbot.AppCommand, slash_handler.SlashHandler{}.SettingUiOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.RegisterOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.SettingOptions())
dbot.AppCommands = append(dbot.AppCommands, slash_handler.SlashHandler{}.SettingUiOptions())
}
}

Expand Down
27 changes: 9 additions & 18 deletions dbot/slash_handler/img2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
func (shdl SlashHandler) Img2imgOptions() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: "img2img",
Description: "Remove background from image",
Description: "Modify an image.",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "init_image",
Description: "Initial image, a file upload. ",
Description: "Initial image, a file upload.",
Type: discordgo.ApplicationCommandOptionAttachment,
Required: true,
},
Expand All @@ -36,7 +36,7 @@ func (shdl SlashHandler) Img2imgOptions() *discordgo.ApplicationCommand {
},
{
Name: "mask",
Description: "Mask image, a file upload. ",
Description: "Mask image, a file upload.",
Type: discordgo.ApplicationCommandOptionAttachment,
Required: false,
},
Expand Down Expand Up @@ -160,16 +160,7 @@ func (shdl SlashHandler) Img2imgOptions() *discordgo.ApplicationCommand {
Name: "inpaint_mask_only", // 重绘区域, False: whole picture True:only masked
Description: "Inpaint Area. Default: Whole picture",
Required: false,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "Whole picture",
Value: false,
},
{
Name: "Only masked",
Value: true,
},
},

},
{
Type: discordgo.ApplicationCommandOptionInteger,
Expand Down Expand Up @@ -208,13 +199,13 @@ func (shdl SlashHandler) Img2imgOptions() *discordgo.ApplicationCommand {
{
Type: discordgo.ApplicationCommandOptionString,
Name: "controlnet_args",
Description: "Controlnet args of the generated image. Default: {}",
Description: "Controlnet args of the generated image.",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "checkpoints",
Description: "Sd model checkpoints. Default: SDXL 1.0",
Name: "checkpoint",
Description: "Sd model checkpoint. Default: SDXL 1.0",
Required: false,
Autocomplete: true,
},
Expand Down Expand Up @@ -300,7 +291,7 @@ func (shdl SlashHandler) Img2imgSetOptions(cmd discordgo.ApplicationCommandInter
tmpAScript["controlnet"] = script
opt.AlwaysonScripts = tmpAScript
}
case "checkpoints":
case "checkpoint":
tmpOverrideSettings := opt.OverrideSettings.(map[string]interface{})
tmpOverrideSettings["sd_model_checkpoint"] = v.StringValue()
opt.OverrideSettings = tmpOverrideSettings
Expand Down Expand Up @@ -420,7 +411,7 @@ func (shdl SlashHandler) Img2imgCommandHandler(s *discordgo.Session, i *discordg
data := i.ApplicationCommandData()

for _, opt := range data.Options {
if opt.Name == "checkpoints" && opt.Focused {
if opt.Name == "checkpoint" && opt.Focused {
repChoices = shdl.FilterChoice(global.LongDBotChoice["sd_model_checkpoint"], opt)
continue
}
Expand Down
Loading

0 comments on commit 6ca161d

Please sign in to comment.