aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--commands/deploy.go5
-rw-r--r--main.go43
2 files changed, 40 insertions, 8 deletions
diff --git a/commands/deploy.go b/commands/deploy.go
index 4974d50..c388228 100644
--- a/commands/deploy.go
+++ b/commands/deploy.go
@@ -71,6 +71,11 @@ func Deploy(ctx context.Context, dg bot.Session) error {
guildID := ""
if common.IsDebug(ctx) {
guildID = dg.GuildAPI().State.Guilds()[0]
+ gld, err := dg.GuildAPI().State.Guild(guildID)
+ if err != nil {
+ return err
+ }
+ bot.Logger(ctx).Debug("using guild as debug", "guild", gld.Name)
}
_, err := dg.InteractionAPI().CommandBulkOverwrite(dg.SessionState().Application().ID, guildID, commands).Do(ctx)
return err
diff --git a/main.go b/main.go
index 179df39..4428edb 100644
--- a/main.go
+++ b/main.go
@@ -46,6 +46,8 @@ var (
stopStatus context.CancelFunc
)
+var finishDeploy context.CancelFunc
+
//go:embed assets/inter-variable.ttf
var interTTF []byte
@@ -103,8 +105,29 @@ func main() {
events := dg.EventManager()
intrs := dg.InteractionManager()
- events.AddHandler(ready)
+ // handle deploy
+ if deploy {
+ var ctx2 context.Context
+ ctx2, finishDeploy = context.WithCancel(ctx)
+ events.AddHandler(deploying)
+
+ err = dg.Open(ctx)
+ if err != nil {
+ panic(err)
+ }
+
+ dg.Logger().Info("waiting deployment")
+ <-ctx2.Done()
+
+ dg.Logger().Info("commands deployed, closing")
+ err = dg.Close(ctx)
+ if err != nil {
+ panic(err)
+ }
+ return
+ }
+ events.AddHandler(ready)
events.AddHandler(func(ct context.Context, dg bot.Session, _ *event.Disconnect) {
user.PeriodicSaver(ctx, dg)
})
@@ -237,13 +260,7 @@ var statuses = []func(context.Context, bot.Session) error{
func ready(ctx context.Context, dg bot.Session, _ *event.Ready) {
logger := bot.Logger(ctx)
- logger.Info("bot started", "as", dg.SessionState().User().Username)
- if deploy || common.IsDebug(ctx) {
- err := commands.Deploy(ctx, dg)
- if err != nil {
- logger.Error("deploying commands", "error", err)
- }
- }
+ logger.Info("bot started", "as", dg.SessionState().User().Username, "usage", "main")
now := time.Now().Unix()
rd := rand.New(rand.NewPCG(uint64(now), uint64(len(statuses))))
stopStatus = common.NewTimer(ctx, 30*time.Second, func(ctx context.Context, _ context.CancelFunc) {
@@ -254,3 +271,13 @@ func ready(ctx context.Context, dg bot.Session, _ *event.Ready) {
}
})
}
+
+func deploying(ctx context.Context, dg bot.Session, _ *event.Connect) {
+ logger := bot.Logger(ctx)
+ logger.Info("bot started", "as", dg.SessionState().User().Username, "usage", "deploy")
+ err := commands.Deploy(ctx, dg)
+ if err != nil {
+ panic(err)
+ }
+ finishDeploy()
+}