82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"homestead/homestead_to_go/relay"
|
|
"homestead/homestead_to_go/relay/commands"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/disgoorg/disgo/bot"
|
|
"github.com/disgoorg/disgo/handler"
|
|
)
|
|
|
|
func main() {
|
|
path := flag.String("config", "config.toml", "path to config")
|
|
flag.Parse()
|
|
|
|
cfg, err := relay.LoadConfig(*path)
|
|
if err != nil {
|
|
slog.Error("Failed to read config", slog.Any("err", err))
|
|
os.Exit(-1)
|
|
}
|
|
|
|
if err := commands.InitStorage(); err != nil {
|
|
slog.Error("Error initializing storage", "err", err)
|
|
os.Exit(-1)
|
|
}
|
|
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
AddSource: cfg.Log.AddSource,
|
|
Level: cfg.Log.Level,
|
|
})))
|
|
|
|
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
|
|
|
|
//
|
|
|
|
b := relay.New(*cfg)
|
|
h := handler.New()
|
|
h.Command("/ping", commands.PingHandler)
|
|
h.Command("/sync", commands.SyncHandler)
|
|
|
|
if err = b.SetupBot(h, bot.NewListenerFunc(b.OnReady)); err != nil {
|
|
slog.Error("Failed to setup bot", slog.Any("err", err))
|
|
os.Exit(-1)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer b.Client.Close(ctx)
|
|
defer cancel()
|
|
|
|
//
|
|
|
|
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
|
|
slog.Error("Failed to sync commands", slog.Any("err", err))
|
|
}
|
|
|
|
if err = b.Client.OpenGateway(ctx); err != nil {
|
|
slog.Error("Failed to open gateway", slog.Any("err", err))
|
|
os.Exit(-1)
|
|
}
|
|
|
|
slog.Info("Bot is running.")
|
|
|
|
s := make(chan os.Signal, 1)
|
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-s
|
|
|
|
slog.Info("Shutting down bot...")
|
|
}
|
|
|
|
// $ git config user.name <user>
|
|
// $ git config user.email <email>
|
|
// $ go mod tidy; go mod download
|