70 lines
2.2 KiB
Go
70 lines
2.2 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(15 * time.Second)
|
|
for i, channel := range tempChannels {
|
|
if channelIsEmpty(channel.ID) {
|
|
session.ChannelDelete(channel.ID)
|
|
tempChannels = remove(tempChannels, i)
|
|
log.Printf("Deleted channel %s", channel.ID)
|
|
log.Printf("Tempchannels: %d", len(tempChannels))
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func channelIsEmpty(channelID string) bool {
|
|
for _, voiceState := range getServer().VoiceStates {
|
|
if channelID == voiceState.ChannelID {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|