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/disgoorg/snowflake/v2" "log/slog" "os" "os/signal" "syscall" "time" ) var ( token = os.Getenv("disgo_token") registerGuildID = snowflake.GetEnv("disgo_guild_id") channelTempID snowflake.ID ) var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) var release string func main() { logger.Info("starting tempbot...", slog.String("disgo version", disgo.Version)) // permissions: Manage Messages // intent: client, err := disgo.New(token, bot.WithGatewayConfigOpts( gateway.WithIntents(gateway.IntentsNone), gateway.WithAutoReconnect(true), ), bot.WithCacheConfigOpts( cache.WithCaches( cache.FlagsNone, ), ), ) if err != nil { logger.Error("error while building disgo 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)) } channels, err := client.Rest().GetGuildChannels(registerGuildID) for _, channel := range channels { switch channel.Name() { case "⌛-temp": channelTempID = channel.ID() } } if channelTempID == 0 { logger.Error("couldn't find needed channel") } ticker := time.NewTicker(1 * time.Minute) quit := make(chan struct{}) go func() { for { select { case <-ticker.C: messages, err := client.Rest().GetMessages(channelTempID, 0, 0, 0, 100) if err != nil { logger.Error("getting messages", slog.Any("error", err)) } var messageIDs []snowflake.ID for _, message := range messages { if message.CreatedAt.Add(30 * time.Minute).Before(time.Now()) { messageIDs = append(messageIDs, message.ID) } } logger.Debug("deleting messages", slog.Int("number", len(messageIDs))) if len(messageIDs) == 1 { err = client.Rest().DeleteMessage(channelTempID, messageIDs[0]) if err != nil { logger.Error("failed deleting messages", slog.Any("error", err)) } } if len(messageIDs) > 1 { err = client.Rest().BulkDeleteMessages(channelTempID, messageIDs) if err != nil { logger.Error("failed deleting messages", slog.Any("error", err)) } } case <-quit: ticker.Stop() return } } }() logger.Info("tempbot 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 }