diff --git a/autoCreate.go b/autoCreate.go new file mode 100644 index 0000000..2e904b2 --- /dev/null +++ b/autoCreate.go @@ -0,0 +1,68 @@ +package main + +import ( + "github.com/bwmarrin/discordgo" +) + +var newChannelEndpoint = make(map[string]*discordgo.Channel) +var createdChannels = make(map[string]map[string][]string) + +var categoryName = "Gaming🎮" + +func initChannels(s *discordgo.Session, guildID string) { + category := searchChannel(s, guildID, categoryName, "") + if category == nil { + return + } + + deleteAllChannelsUnderCategory(s, guildID, category.ID) + createNewChannelEndpoint(s, guildID) + + createdChannels[guildID] = make(map[string][]string) +} + +func createNewChannelEndpoint(s *discordgo.Session, guildID string) { + category := searchChannel(s, guildID, categoryName, "") + if category == nil { + return + } + + newChannelData := discordgo.GuildChannelCreateData{ + Name: "Crea nuovo canale", + Type: discordgo.ChannelTypeGuildVoice, + ParentID: category.ID} + + newChannelEndpoint[guildID], _ = s.GuildChannelCreateComplex(guildID, newChannelData) +} + +func createNewChannel(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { + channelName := getActivity(s.State, vs.GuildID, vs.UserID) + s.ChannelEdit(vs.ChannelID, channelName) + createdChannels[vs.GuildID][vs.ChannelID] = []string{vs.UserID} + + createNewChannelEndpoint(s, vs.GuildID) +} + +func removeUserFromChannels(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { + for channelID, channel := range createdChannels[vs.GuildID] { + index := contains(channel, vs.UserID) + + if index != -1 { + if len(channel) == 1 { + s.ChannelDelete(channelID) + } else { + createdChannels[vs.GuildID][channelID] = remove(channel, index) + } + } + } +} + +func getActivity(st *discordgo.State, guildID string, userID string) string { + presence, err := st.Presence(guildID, userID) + + if err != nil || len(presence.Activities) == 0 { + return "Stanza" + } + + return presence.Activities[len(presence.Activities)-1].Name +} diff --git a/main.go b/main.go index 07c947e..09c10dc 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,8 @@ func main() { // Register the messageCreate func as a callback for MessageCreate events. dg.AddHandler(handleMessage) + dg.AddHandler(handleVoiceActivity) + dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll) // Open a websocket connection to Discord and begin listening. err = dg.Open() @@ -67,6 +69,26 @@ func main() { dg.Close() } +func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { + if val, ok := newChannelEndpoint[vs.GuildID]; ok { + if vs.ChannelID == "" { + removeUserFromChannels(s, vs) + return + } + + if vs.ChannelID == val.ID { + removeUserFromChannels(s, vs) + createNewChannel(s, vs) + } else if _, ok := createdChannels[vs.GuildID][vs.ChannelID]; ok { + createdChannels[vs.GuildID][vs.ChannelID] = append(createdChannels[vs.GuildID][vs.ChannelID], vs.UserID) + } else { + removeUserFromChannels(s, vs) + } + } else { + initChannels(s, vs.GuildID) + } +} + // This function will be called (due to AddHandler above) every time a new // message is created on any channel that the autenticated bot has access to. func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) { diff --git a/utils.go b/utils.go index 14ec940..e852cf9 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,8 @@ package main -import "github.com/bwmarrin/discordgo" +import ( + "github.com/bwmarrin/discordgo" +) func searchChannel(s *discordgo.Session, guildID string, channelName string, channelParent string) (channel *discordgo.Channel) { channels, _ := s.GuildChannels(guildID) @@ -16,6 +18,24 @@ func searchChannel(s *discordgo.Session, guildID string, channelName string, cha return nil } +func getAllChannelsUnderCategory(s *discordgo.Session, guildID string, channelParent string) (channels []*discordgo.Channel) { + allChannels, _ := s.GuildChannels(guildID) + + for _, channel := range allChannels { + if channelParent == "" || channel.ParentID == channelParent { + channels = append(channels, channel) + } + } + + return channels +} + +func deleteAllChannelsUnderCategory(s *discordgo.Session, guildID string, channelParent string) { + for _, channel := range getAllChannelsUnderCategory(s, guildID, channelParent) { + s.ChannelDelete(channel.ID) + } +} + func searchRole(s *discordgo.Session, guildID string, roleName string) (role *discordgo.Role) { roles, _ := s.GuildRoles(guildID) @@ -39,3 +59,17 @@ func searchUser(s *discordgo.Session, guildID string, userName string) (role *di return nil } + +func contains(s []string, e string) int { + for i, a := range s { + if a == e { + return i + } + } + return -1 +} + +func remove(s []string, i int) []string { + s[i] = s[len(s)-1] + return s[:len(s)-1] +}