discordBots/cmd/welcomebot/main.go

107 lines
2.8 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/gateway"
2023-04-22 23:23:52 +00:00
"github.com/getsentry/sentry-go"
2023-03-08 20:27:50 +00:00
"grow.rievo.dev/discordBots/cmd/welcomebot/config"
2023-03-08 20:38:17 +00:00
"grow.rievo.dev/discordBots/cmd/welcomebot/event"
"log"
2023-03-08 20:27:50 +00:00
"os"
"os/signal"
"syscall"
2023-04-22 23:23:52 +00:00
"time"
)
2023-04-22 23:23:52 +00:00
// sentry
var release string
func main() {
2023-04-22 23:23:52 +00:00
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 welcomebot...")
log.Printf("INFO: disgo version: %v", disgo.Version)
2023-01-25 23:33:18 +00:00
// permissions: Manage Roles
// intent: Server Members Intent
2023-03-08 20:27:50 +00:00
client, err := disgo.New(config.Token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentGuildMembers),
2023-06-23 08:28:33 +00:00
gateway.WithAutoReconnect(true),
),
bot.WithCacheConfigOpts(
2023-01-25 23:33:18 +00:00
cache.WithCaches(
cache.FlagsNone,
),
),
2023-03-08 20:38:17 +00:00
bot.WithEventListenerFunc(event.JoinEvent),
bot.WithEventListenerFunc(event.LeaveEvent),
bot.WithEventListenerFunc(event.ReactionEvent),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
return
}
defer client.Close(context.TODO())
if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
}
2023-03-08 20:27:50 +00:00
channels, err := client.Rest().GetGuildChannels(config.RegisterGuildID)
for _, channel := range channels {
switch channel.Name() {
case "🔨-rules":
2023-03-08 20:27:50 +00:00
config.ChannelRulesMention = channel.Mention()
case "❕-info":
2023-03-08 20:27:50 +00:00
config.ChannelInfoMention = channel.Mention()
case "👋-welcome":
2023-03-08 20:27:50 +00:00
config.ChannelWelcomeID = channel.ID()
}
}
2023-03-08 20:27:50 +00:00
if config.ChannelRulesMention == "" || config.ChannelInfoMention == "" || config.ChannelWelcomeID == 0 {
log.Fatal("couldn't find needed channel")
}
2023-03-08 20:27:50 +00:00
roles, err := client.Rest().GetRoles(config.RegisterGuildID)
for _, role := range roles {
switch role.Name {
case "Apple":
2023-03-08 20:27:50 +00:00
config.RoleAppleID = role.ID
case "Apple Core":
2023-03-08 20:27:50 +00:00
config.RoleAppleCoreID = role.ID
}
}
2023-03-08 20:27:50 +00:00
if config.RoleAppleID == 0 || config.RoleAppleCoreID == 0 {
log.Fatal("couldn't find needed role")
}
log.Printf("INFO: welcomebot is now running. Press CTRL-C to exit.")
2023-04-22 23:23:52 +00:00
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelDebug)
sentry.CaptureMessage("DEBUG: welcomebot started")
})
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}