2023-01-25 19:09:58 +00:00
|
|
|
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"
|
2023-04-22 23:23:52 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2023-04-22 22:33:20 +00:00
|
|
|
"log"
|
2023-01-25 19:09:58 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
token = os.Getenv("disgo_token")
|
|
|
|
registerGuildID = snowflake.GetEnv("disgo_guild_id")
|
|
|
|
channelTempID snowflake.ID
|
|
|
|
)
|
|
|
|
|
2023-04-22 23:23:52 +00:00
|
|
|
// sentry
|
|
|
|
var release string
|
|
|
|
|
2023-01-25 19:09:58 +00:00
|
|
|
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)
|
|
|
|
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("INFO: starting tempbot...")
|
|
|
|
log.Printf("INFO: disgo version: %v", disgo.Version)
|
2023-01-25 19:09:58 +00:00
|
|
|
|
2023-01-25 23:33:18 +00:00
|
|
|
// permissions: Manage Messages
|
|
|
|
// intent:
|
2023-01-25 19:09:58 +00:00
|
|
|
client, err := disgo.New(token,
|
|
|
|
bot.WithGatewayConfigOpts(
|
2023-01-25 23:33:18 +00:00
|
|
|
gateway.WithIntents(gateway.IntentsNone),
|
2023-01-25 19:09:58 +00:00
|
|
|
),
|
|
|
|
bot.WithCacheConfigOpts(
|
2023-01-25 23:33:18 +00:00
|
|
|
cache.WithCaches(
|
|
|
|
cache.FlagsNone,
|
2023-01-25 19:09:58 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("error while building disgo instance: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer client.Close(context.TODO())
|
|
|
|
|
2023-01-25 23:33:18 +00:00
|
|
|
if err = client.OpenGateway(context.TODO()); err != nil {
|
|
|
|
log.Fatal("error while connecting to gateway: ", err)
|
|
|
|
}
|
|
|
|
|
2023-01-25 19:09:58 +00:00
|
|
|
channels, err := client.Rest().GetGuildChannels(registerGuildID)
|
|
|
|
for _, channel := range channels {
|
|
|
|
switch channel.Name() {
|
|
|
|
case "⌛-temp":
|
|
|
|
channelTempID = channel.ID()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if channelTempID == 0 {
|
|
|
|
log.Fatal("couldn't find needed channel")
|
|
|
|
}
|
|
|
|
|
2023-03-04 11:54:08 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
2023-01-25 19:09:58 +00:00
|
|
|
quit := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
messages, err := client.Rest().GetMessages(channelTempID, 0, 0, 0, 100)
|
|
|
|
if err != nil {
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("ERROR: getting messages: %v", err)
|
2023-01-25 19:09:58 +00:00
|
|
|
}
|
|
|
|
var messageIDs []snowflake.ID
|
|
|
|
for _, message := range messages {
|
|
|
|
if message.CreatedAt.Add(30 * time.Minute).Before(time.Now()) {
|
|
|
|
messageIDs = append(messageIDs, message.ID)
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("DEBUG: deleting messages: %v", len(messageIDs))
|
2023-01-25 19:09:58 +00:00
|
|
|
if len(messageIDs) == 1 {
|
|
|
|
err = client.Rest().DeleteMessage(channelTempID, messageIDs[0])
|
|
|
|
if err != nil {
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("ERROR: deleting messages: %v", err)
|
2023-01-25 19:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(messageIDs) > 1 {
|
|
|
|
err = client.Rest().BulkDeleteMessages(channelTempID, messageIDs)
|
|
|
|
if err != nil {
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("ERROR: deleting messages: %v", err)
|
2023-01-25 19:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-04-22 22:33:20 +00:00
|
|
|
log.Printf("INFO: tempbot 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: tempbot started")
|
|
|
|
})
|
|
|
|
|
2023-01-25 19:09:58 +00:00
|
|
|
s := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
<-s
|
|
|
|
}
|