discordBots/cmd/funbot/main.go
2023-11-05 16:03:52 +00:00

82 lines
2.2 KiB
Go

package main
import (
"context"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/gateway"
"grow.rievo.dev/discordBots/cmd/funbot/command"
"grow.rievo.dev/discordBots/cmd/funbot/config"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
)
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
var release string
func main() {
logger.Info("starting funbot...", slog.String("disgo version", disgo.Version))
// permissions:
// intent:
client, err := disgo.New(config.Token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentsNone),
gateway.WithAutoReconnect(true),
),
bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagsNone,
),
),
bot.WithEventListenerFunc(command.Listener),
)
if err != nil {
logger.Error("error while building disgo instance", slog.Any("error", err))
return
}
defer client.Close(context.TODO())
var globalComands bool
if strings.ToUpper(config.GlobalCommands) == "TRUE" {
globalComands = true
}
if globalComands {
if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), config.Commands); err != nil {
logger.Error("error while registering commands", slog.Any("error", err))
return
}
if config.RegisterGuildID != 0 {
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.NoCommands); err != nil {
logger.Info("error deleting guild commands", slog.Any("error", err))
}
}
} else {
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.Commands); err != nil {
logger.Error("error while registering commands", slog.Any("error", err))
return
}
if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), config.NoCommands); err != nil {
logger.Info("error deleting global commands", slog.Any("error", err))
}
}
if err = client.OpenGateway(context.TODO()); err != nil {
logger.Error("error while connecting to gateway", slog.Any("error", err))
return
}
logger.Info("funbot is now running. Press CTRL-C to exit.", slog.String("version", release))
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}