discord-selphybot/voicechannel.go
2019-06-08 05:31:06 +02:00

75 lines
2.3 KiB
Go

package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strconv"
"strings"
"time"
)
var tempChannels []*discordgo.Channel
func parseVoiceChannelCommand(session *discordgo.Session, message *discordgo.MessageCreate) {
userLimit, err := strconv.Atoi(strings.Split(message.Content, " ")[1])
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Error: Expected a number after !vc")
log.Printf("Incorrect syntax for !vc, “%s” triggered by %s", message.Content, userToString(message.Author))
return
}
if userLimit > 99 {
session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Als ob %d Leute *mit dir* in einen Channel wollen.", userLimit-1))
log.Printf("%s tried to create a channel with %d slots", userToString(message.Author), userLimit)
return
}
createData := discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("%s’s Volatile Corner", message.Author.Username),
Type: discordgo.ChannelTypeGuildVoice,
UserLimit: userLimit,
ParentID: "410162599762853909",
}
channel, err := session.GuildChannelCreateComplex(message.GuildID, createData)
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Couldn’t create the voice channel. Please bug kageru about this.")
log.Printf("Failed to create voice channel, %s", err)
return
}
tempChannels = append(tempChannels, channel)
err = session.GuildMemberMove(config.ServerID, message.Author.ID, channel.ID)
if err != nil {
log.Printf("Couldn’t move user %s: %s", userToString(message.Author), err)
}
session.ChannelMessageSend(message.ChannelID, "haaaai~")
log.Printf("Created channel %s", channel.ID)
}
func checkAndDeleteUnusedChannels(session *discordgo.Session) {
for true {
time.Sleep(30 * time.Second)
server, err := getServer()
if err == nil {
for i, channel := range tempChannels {
if channelIsEmpty(channel.ID, server.VoiceStates) {
session.ChannelDelete(channel.ID)
tempChannels = remove(tempChannels, i)
log.Printf("Deleted channel %s", channel.ID)
log.Printf("Tempchannels: %d", len(tempChannels))
break
}
}
} else {
log.Printf("Could not retrieve voice state from API, %s", err)
}
}
}
func channelIsEmpty(channelID string, voiceStates []*discordgo.VoiceState) bool {
for _, state := range voiceStates {
if channelID == state.ChannelID {
return false
}
}
return true
}