discordBots/cmd/funbot/main.go
2023-06-23 08:28:33 +00:00

100 lines
2.7 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"
"github.com/getsentry/sentry-go"
"grow.rievo.dev/discordBots/cmd/funbot/command"
"grow.rievo.dev/discordBots/cmd/funbot/config"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
// sentry
var release string
func main() {
err := sentry.Init(sentry.ClientOptions{
// Either set your DSN here or set the SENTRY_DSN environment variable.
//Dsn: "",
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
Release: release,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
log.Printf("INFO: starting funbot...")
log.Printf("INFO: disgo version: %v", 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 {
log.Fatal("error while building disgo instance: ", 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 {
log.Fatal("error while registering commands: ", err)
}
if config.RegisterGuildID != 0 {
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.NoCommands); err != nil {
log.Printf("INFO: error deleting guild commands %v", err)
}
}
} else {
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.Commands); err != nil {
log.Fatal("error while registering commands: ", err)
}
if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), config.NoCommands); err != nil {
log.Printf("INFO: error deleting global commands %v", err)
}
}
if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
}
log.Printf("INFO: funbot (%v) is now running. Press CTRL-C to exit.", release)
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelDebug)
sentry.CaptureMessage("DEBUG: funbot started")
})
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}