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" "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 { client.Logger().Error("unable to send channel message: ", err) return } _, err = client.Rest().UpdateMessage(config.ChannelVoiceLogID, message.ID, discord.NewMessageUpdateBuilder().ClearContent().SetContent(msgEdit).Build()) if err != nil { client.Logger().Error("unable to update channel message: ", err) 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 { client.Logger().Error("error in getVoiceChannels: ", err) } 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 { client.Logger().Debug("all channels are empty") return } if len(voiceChannelsWithState) >= len(voiceChannels) { // if there are no empty voiceChannels create one client.Logger().Debug("new channel has to be created") name, err := GetName(voiceChannels) if err != nil { client.Logger().Error("no empty channels") 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 client.Logger().Debug("channel has to be deleted") client.Logger().Debug("empty channels: ", voiceChannelsEmpty) for id, s := range voiceChannelsEmpty { // get the oldest channel and delete it client.Logger().Debug("deleting: ", s) err := client.Rest().DeleteChannel(id) if err != nil { client.Logger().Error("not able to delete: ", err) return } break } UpdateVoiceChannels(client, true) } }