discordBots/cmd/funbot/main.go

83 lines
2.2 KiB
Go
Raw Normal View History

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