discordBots/cmd/vcbot/main.go

91 lines
2.3 KiB
Go
Raw Normal View History

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"
2023-03-08 20:39:42 +00:00
"grow.rievo.dev/discordBots/cmd/vcbot/config"
"grow.rievo.dev/discordBots/cmd/vcbot/event"
2023-11-05 16:03:52 +00:00
"log/slog"
"os"
"os/signal"
"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
func main() {
2023-04-22 23:23:52 +00:00
2023-11-05 16:03:52 +00:00
logger.Info("starting vcbot...", slog.String("disgo version", disgo.Version))
2023-11-05 16:03:52 +00:00
err := config.LoadAppleList()
if err != nil {
2023-11-05 16:03:52 +00:00
logger.Error("could not load apple list", slog.Any("error", err))
2024-01-28 19:52:00 +00:00
config.FallbackAppleList()
}
2023-01-25 23:33:18 +00:00
// permissions: Manage Channels
// intents:
2023-03-08 20:39:42 +00:00
client, err := disgo.New(config.Token,
bot.WithGatewayConfigOpts(
2024-05-21 18:28:34 +00:00
gateway.WithIntents(
gateway.IntentGuildVoiceStates,
gateway.IntentGuilds, // needed for current VoiceStates to be loaded on start
),
2023-06-23 08:28:33 +00:00
gateway.WithAutoReconnect(true),
),
bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagChannels,
cache.FlagVoiceStates,
cache.FlagMembers,
),
),
2023-03-08 20:39:42 +00:00
bot.WithEventListenerFunc(event.JoinEvent),
bot.WithEventListenerFunc(event.MoveEvent),
bot.WithEventListenerFunc(event.LeaveEvent),
bot.WithEventListenerFunc(func(event *events.Ready) {
2023-11-05 16:03:52 +00:00
logger.Debug("Connection Ready")
}),
bot.WithEventListenerFunc(func(event *events.Resumed) {
2023-11-05 16:03:52 +00:00
logger.Debug("Connection Resumed")
}),
)
if err != nil {
2023-11-05 16:03:52 +00:00
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 {
2023-11-05 16:03:52 +00:00
logger.Error("error while connecting to gateway", slog.Any("error", err))
return
}
2023-03-08 20:39:42 +00:00
channels, err := client.Rest().GetGuildChannels(config.RegisterGuildID)
for _, channel := range channels {
switch channel.Name() {
case "🔊-talk":
2023-03-08 20:39:42 +00:00
config.ChannelVoiceGroupID = channel.ID()
case "📋-voice":
2023-03-08 20:39:42 +00:00
config.ChannelVoiceLogID = channel.ID()
}
}
2023-03-08 20:39:42 +00:00
if config.ChannelVoiceGroupID == 0 {
2023-11-05 16:03:52 +00:00
logger.Error("couldn't find needed channel")
return
}
2023-11-05 16:03:52 +00:00
logger.Info("INFO: vcbot is now running. Press CTRL-C to exit.", slog.String("version", release))
2023-04-22 23:23:52 +00:00
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}