Skip to content

Commit

Permalink
Fast bulk-register commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Distortions81 committed Nov 1, 2024
1 parent c15b910 commit b6ea0b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
48 changes: 24 additions & 24 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package commands
import (
"ChatWire/cfg"
"ChatWire/commands/moderator"
"ChatWire/constants"
"ChatWire/cwlog"
"ChatWire/disc"
"ChatWire/fact"
"ChatWire/glob"
"ChatWire/sclean"
"ChatWire/support"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -140,17 +139,15 @@ func DeregisterCommands() {
return
}
if *glob.DoDeregisterCommands && disc.DS != nil {
cmds, _ := disc.DS.ApplicationCommands(cfg.Global.Discord.Application, cfg.Global.Discord.Guild)
for _, v := range cmds {
cwlog.DoLogCW("Deregistered command: %s", v.Name)
err := disc.DS.ApplicationCommandDelete(disc.DS.State.User.ID, cfg.Global.Discord.Guild, v.ID)
if err != nil {
cwlog.DoLogCW(err.Error())
}

time.Sleep(constants.ApplicationCommandSleep)
cwlog.DoLogCW("Bulk deregistering commands!")
_, err := disc.DS.ApplicationCommandBulkOverwrite(cfg.Global.Discord.Application, cfg.Global.Discord.Guild, []*discordgo.ApplicationCommand{})
if err != nil {
fact.LogGameCMS(true, cfg.Local.Channel.ChatChannel, "Deregister commands failed: "+err.Error())
return
}
cwlog.DoLogCW("Deregister commands complete.")
fact.LogGameCMS(true, cfg.Local.Channel.ChatChannel, "Deregister commands complete.")
time.Sleep(time.Second * 5)
os.Exit(0)
}
}

Expand All @@ -166,11 +163,10 @@ func RegisterCommands() {
/* Bypasses init loop compile error. */
CL = cmds

//Bypass register, very slow
//TODO: Cache info and correct for changes when needed

if *glob.DoRegisterCommands {
cwlog.DoLogCW("Bulk registering commands!")

cmdList := []*discordgo.ApplicationCommand{}
for i, o := range CL {

if o.Disabled {
Expand All @@ -180,7 +176,6 @@ func RegisterCommands() {
cwlog.DoLogCW("Command has no name or description, skipping")
continue
}
time.Sleep(constants.ApplicationCommandSleep)

if strings.EqualFold(o.AppCmd.Name, "config-server") {
LinkConfigData(i, false)
Expand Down Expand Up @@ -224,16 +219,21 @@ func RegisterCommands() {

tempAppCmd.Options = append(tempAppCmd.Options, tmpOption)
}

_, err := disc.DS.ApplicationCommandCreate(cfg.Global.Discord.Application, cfg.Global.Discord.Guild, tempAppCmd)
if err != nil {
log.Println("Failed to create command: ",
CL[i].AppCmd.Name, ": ", err)
continue
cmdList = append(cmdList, tempAppCmd)
}
created, err := disc.DS.ApplicationCommandBulkOverwrite(cfg.Global.Discord.Application, cfg.Global.Discord.Guild, cmdList)
if err != nil {
fact.LogGameCMS(true, cfg.Local.Channel.ChatChannel, "Register commands failed: "+err.Error())
return
}
cList := ""
for _, ctmp := range created {
if cList != "" {
cList = cList + ", "
}
cwlog.DoLogCW("Registered command: %s", CL[i].AppCmd.Name)
cList = cList + ctmp.Name
}
cwlog.DoLogCW("Register commands complete.")
fact.LogGameCMS(true, cfg.Local.Channel.ChatChannel, "Register commands complete: "+cList)
}

}
Expand Down
11 changes: 5 additions & 6 deletions constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

const (
ProgName = "ChatWire"
Version = "2648-1101.2024-1228"
Version = "2648-1101.2024-0358p"
CWEpoch = 1653239822390688174
SeenDivisor = 60
SeenEpoch = 1546326000
Expand Down Expand Up @@ -85,11 +85,10 @@ const (
WatchdogInterval = time.Second

/* Throttle Discord chat */
CMSRate = 500 * time.Millisecond //Time we spend waiting for buffer to fill up once active
CMSRestTime = 2000 * time.Millisecond //Time to sleep after sending a message
CMSPollRate = 100 * time.Millisecond //Time between polls
MaxDiscordAttempts = 95
ApplicationCommandSleep = time.Second * 3
CMSRate = 500 * time.Millisecond //Time we spend waiting for buffer to fill up once active
CMSRestTime = 2000 * time.Millisecond //Time to sleep after sending a message
CMSPollRate = 100 * time.Millisecond //Time between polls
MaxDiscordAttempts = 95
)

/* Factorio map preset names */
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func main() {
glob.DoRegisterCommands = flag.Bool("regCommands", false, "Register discord commands")
glob.DoDeregisterCommands = flag.Bool("deregCommands", false, "Deregister discord commands")
glob.DoDeregisterCommands = flag.Bool("deregCommands", false, "Deregister discord commands and quit.")
glob.LocalTestMode = flag.Bool("localTest", false, "Turn off public/auth mode for testing")
glob.NoAutoLaunch = flag.Bool("noAutoLaunch", false, "Turn off auto-launch")
cleanDB := flag.Bool("cleanDB", false, "Clean/minimize player database and exit.")
Expand Down

0 comments on commit b6ea0b8

Please sign in to comment.