discordBots/cmd/vcbot/main.go
2024-05-21 20:56:31 +02:00

90 lines
2.3 KiB
Go

package main
import (
"context"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"grow.rievo.dev/discordBots/cmd/vcbot/config"
"grow.rievo.dev/discordBots/cmd/vcbot/event"
"log/slog"
"os"
"os/signal"
"syscall"
)
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
var release string
func main() {
logger.Info("starting vcbot...", slog.String("disgo version", disgo.Version))
err := config.LoadAppleList()
if err != nil {
logger.Error("could not load apple list", slog.Any("error", err))
config.FallbackAppleList()
}
// permissions: Manage Channels
// intents:
client, err := disgo.New(config.Token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuildVoiceStates,
gateway.IntentGuilds, // needed for current VoiceStates to be loaded on start
),
gateway.WithAutoReconnect(true),
),
bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagChannels,
cache.FlagVoiceStates,
cache.FlagMembers,
),
),
bot.WithEventListenerFunc(event.JoinEvent),
bot.WithEventListenerFunc(event.MoveEvent),
bot.WithEventListenerFunc(event.LeaveEvent),
bot.WithEventListenerFunc(func(event *events.Ready) {
logger.Debug("Connection Ready")
}),
bot.WithEventListenerFunc(func(event *events.Resumed) {
logger.Debug("Connection Resumed")
}),
)
if err != nil {
logger.Error("error while building vcbot instance", slog.Any("error", err))
return
}
defer client.Close(context.TODO())
if err = client.OpenGateway(context.TODO()); err != nil {
logger.Error("error while connecting to gateway", slog.Any("error", err))
return
}
channels, err := client.Rest().GetGuildChannels(config.RegisterGuildID)
for _, channel := range channels {
switch channel.Name() {
case "🔊-talk":
config.ChannelVoiceGroupID = channel.ID()
case "📋-voice":
config.ChannelVoiceLogID = channel.ID()
}
}
if config.ChannelVoiceGroupID == 0 {
logger.Error("couldn't find needed channel")
return
}
logger.Info("INFO: vcbot 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
}