probably completed implementation of voicechannelbot
This commit is contained in:
parent
981fc2cb46
commit
16ee889aae
2 changed files with 397 additions and 0 deletions
99
cmd/tempbot/main.go
Normal file
99
cmd/tempbot/main.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
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/log"
|
||||
"github.com/disgoorg/snowflake/v2"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
token = os.Getenv("disgo_token")
|
||||
registerGuildID = snowflake.GetEnv("disgo_guild_id")
|
||||
channelTempID snowflake.ID
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetLevel(log.LevelInfo)
|
||||
log.Info("starting joinbot...")
|
||||
log.Info("disgo version: ", disgo.Version)
|
||||
|
||||
// intents needed GUILD_MESSAGES
|
||||
client, err := disgo.New(token,
|
||||
bot.WithGatewayConfigOpts(
|
||||
gateway.WithIntents(gateway.IntentsAll),
|
||||
),
|
||||
bot.WithCacheConfigOpts(
|
||||
cache.WithCacheFlags(
|
||||
cache.FlagsAll,
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal("error while building disgo instance: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer client.Close(context.TODO())
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
// delete messages older then x min in 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 {
|
||||
client.Logger().Error("error getting messages: ", err)
|
||||
}
|
||||
var messageIDs []snowflake.ID
|
||||
for _, message := range messages {
|
||||
if message.CreatedAt.Add(30 * time.Minute).Before(time.Now()) {
|
||||
messageIDs = append(messageIDs, message.ID)
|
||||
}
|
||||
}
|
||||
client.Logger().Debug("deleting messages: ", len(messageIDs))
|
||||
if len(messageIDs) == 1 {
|
||||
err = client.Rest().DeleteMessage(channelTempID, messageIDs[0])
|
||||
if err != nil {
|
||||
client.Logger().Error("error deleting messages: ", err)
|
||||
}
|
||||
}
|
||||
if len(messageIDs) > 1 {
|
||||
err = client.Rest().BulkDeleteMessages(channelTempID, messageIDs)
|
||||
if err != nil {
|
||||
client.Logger().Error("error deleting messages: ", err)
|
||||
}
|
||||
}
|
||||
case <-quit:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
log.Infof("tempbot is now running. Press CTRL-C to exit.")
|
||||
s := make(chan os.Signal, 1)
|
||||
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||
<-s
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue