discordBots/cmd/vcbot/helper/helper.go

139 lines
3.9 KiB
Go
Raw Permalink Normal View History

2023-03-08 20:39:42 +00:00
package helper
import (
"errors"
"fmt"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/snowflake/v2"
"grow.rievo.dev/discordBots/cmd/vcbot/config"
"log"
2023-03-08 20:39:42 +00:00
"math/rand"
)
func SendNoMention(client bot.Client, msg, msgEdit string) {
message, err := client.Rest().CreateMessage(config.ChannelVoiceLogID, discord.NewMessageCreateBuilder().
SetContent(msg).Build())
if err != nil {
log.Printf("ERROR: unable to send channel message: %v", err)
2023-03-08 20:39:42 +00:00
return
}
_, err = client.Rest().UpdateMessage(config.ChannelVoiceLogID,
message.ID,
discord.NewMessageUpdateBuilder().ClearContent().SetContent(msgEdit).Build())
if err != nil {
log.Printf("ERROR: unable to update channel message: %v", err)
2023-03-08 20:39:42 +00:00
return
}
}
func GetVoiceChannels(client bot.Client) (map[snowflake.ID]string, error) {
allChannels, err := client.Rest().GetGuildChannels(config.RegisterGuildID)
if err != nil {
return nil, errors.New(fmt.Sprintf("error getting channels: %v", err.Error()))
}
voiceChannels := make(map[snowflake.ID]string)
channelVoiceGroupIDStr := config.ChannelVoiceGroupID.String()
for _, channel := range allChannels {
if channel.Type() == discord.ChannelTypeGuildCategory {
continue
}
parentIDStr := channel.ParentID().String()
channelType := channel.Type()
if parentIDStr == channelVoiceGroupIDStr && channelType == discord.ChannelTypeGuildVoice {
voiceChannels[channel.ID()] = channel.Name()
}
}
return voiceChannels, nil
}
func GetName(channels map[snowflake.ID]string) (string, error) {
channelsReverse := make(map[string]snowflake.ID)
for id, name := range channels {
channelsReverse[name] = id
}
appleListLen := len(config.AppleList)
for i := 0; i < appleListLen; i++ {
randomIndex := rand.Intn(appleListLen)
pick := config.AppleList[randomIndex]
newName := fmt.Sprintf("%v%v", config.ChannelVoicePrefix, pick)
_, exists := channelsReverse[newName]
if exists == false {
return newName, nil
}
}
return "", errors.New("could not find unused name")
}
func UpdateVoiceChannels(client bot.Client, deleteChannels bool) {
voiceChannels, err := GetVoiceChannels(client)
if err != nil {
log.Printf("ERROR: error in getVoiceChannels: %v", err)
2023-03-08 20:39:42 +00:00
}
voiceChannelsWithState := make(map[snowflake.ID]string)
voiceChannelsEmpty := make(map[snowflake.ID]string)
for key, val := range voiceChannels {
voiceChannelsEmpty[key] = val
}
client.Caches().VoiceStatesForEach(config.RegisterGuildID, func(state discord.VoiceState) {
if state.ChannelID == nil {
return
}
channelId := *state.ChannelID
name, ok := voiceChannels[channelId]
if !ok {
// not channel of group
return
}
voiceChannelsWithState[channelId] = name
delete(voiceChannelsEmpty, channelId)
})
if len(voiceChannels) == 2 && len(voiceChannelsWithState) == 0 {
log.Printf("DEBUG: all channels are empty")
2023-03-08 20:39:42 +00:00
return
}
if len(voiceChannelsWithState) >= len(voiceChannels) {
// if there are no empty voiceChannels create one
log.Printf("DEBUG: new channel has to be created")
2023-03-08 20:39:42 +00:00
name, err := GetName(voiceChannels)
if err != nil {
log.Printf("ERROR: no empty channels")
2023-03-08 20:39:42 +00:00
return
}
_, err = client.Rest().CreateGuildChannel(config.RegisterGuildID, discord.GuildVoiceChannelCreate{
Name: name,
UserLimit: 77,
ParentID: config.ChannelVoiceGroupID,
})
if err != nil {
return
}
return
}
if len(voiceChannels)-len(voiceChannelsWithState) > 1 && deleteChannels {
// if there are more than one empty voiceChannels delete all but 1
log.Printf("DEBUG: channel has to be deleted")
log.Printf("DEBUG: empty channels: %v", voiceChannelsEmpty)
2023-03-08 20:39:42 +00:00
for id, s := range voiceChannelsEmpty {
// get the oldest channel and delete it
log.Printf("DEBUG: deleting: %v", s)
2023-03-08 20:39:42 +00:00
err := client.Rest().DeleteChannel(id)
if err != nil {
log.Printf("ERROR: not able to delete: %v", err)
2023-03-08 20:39:42 +00:00
return
}
break
}
UpdateVoiceChannels(client, true)
}
}