chore: Objectify managed guilds
All checks were successful
Build artifact / build (push) Successful in 22s
Build artifact / release (push) Has been skipped

This commit is contained in:
2024-07-11 12:44:30 +02:00
parent 30687e2271
commit bd2fe73102
2 changed files with 70 additions and 54 deletions

View File

@@ -20,27 +20,37 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var newChannelEndpoint = make(map[string]*discordgo.Channel) type AutoGuild struct {
var createdChannels = make(map[string]map[string][]string) id string
newChannelEndpoint *discordgo.Channel
createdChannels map[string][]string
}
var categoryName = "Gaming🎮" var managedGuilds = make(map[string]*AutoGuild)
const CATEGORY_NAME = "Gaming🎮"
const DEFAULT_CHANNEL_NAME = "Stanza"
func initChannels(s *discordgo.Session, guildID string) { func initChannels(s *discordgo.Session, guildID string) {
var guild AutoGuild
guild.id = guildID
guild.createdChannels = make(map[string][]string)
log.Debugf("Preparing guild %s for automatic channel management", guildID) log.Debugf("Preparing guild %s for automatic channel management", guildID)
category := searchChannel(s, guildID, categoryName, "") category := searchChannel(s, guildID, CATEGORY_NAME, "")
if category == nil { if category == nil {
log.Errorf("Cannot find category %s in guild %s", categoryName, guildID) log.Errorf("Cannot find category %s in guild %s", CATEGORY_NAME, guildID)
return return
} }
deleteAllChannelsUnderCategory(s, guildID, category.ID) deleteAllChannelsUnderCategory(s, guildID, category.ID)
createNewChannelEndpoint(s, guildID, category.ID) guild.createNewChannelEndpoint(s, category.ID)
createdChannels[guildID] = make(map[string][]string) managedGuilds[guildID] = &guild
} }
func createNewChannelEndpoint(s *discordgo.Session, guildID string, channelParent string) { func (guild *AutoGuild) createNewChannelEndpoint(s *discordgo.Session, channelParent string) {
newChannelData := discordgo.GuildChannelCreateData{ newChannelData := discordgo.GuildChannelCreateData{
Name: "Crea nuovo canale", Name: "Crea nuovo canale",
@@ -48,73 +58,76 @@ func createNewChannelEndpoint(s *discordgo.Session, guildID string, channelParen
ParentID: channelParent} ParentID: channelParent}
var err error var err error
newChannelEndpoint[guildID], err = s.GuildChannelCreateComplex(guildID, newChannelData) guild.newChannelEndpoint, err = s.GuildChannelCreateComplex(guild.id, newChannelData)
if err != nil { if err != nil {
log.Errorf("Cannot create channel with name %s in guild %s. This guild is in a corrupted state", newChannelData.Name, guildID) log.Errorf("Cannot create channel with name %s in guild %s. This guild is in a corrupted state", newChannelData.Name, guild.id)
return return
} }
log.Debugf("Created channel with name %s in guild %s", newChannelData.Name, guildID) log.Debugf("Created channel with name %s in guild %s", newChannelData.Name, guild.id)
} }
func createNewChannel(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { func (guild *AutoGuild) createNewChannel(s *discordgo.Session, channelID string, userID string) {
category := searchChannel(s, vs.GuildID, categoryName, "") category := searchChannel(s, guild.id, CATEGORY_NAME, "")
if category == nil { if category == nil {
log.Errorf("Cannot find category %s in guild %s", categoryName, vs.GuildID) log.Errorf("Cannot find category %s in guild %s", CATEGORY_NAME, guild.id)
return return
} }
channelName := getActivity(s.State, vs.GuildID, vs.UserID) channelName := guild.getActivity(s.State, userID)
editChannelData := discordgo.ChannelEdit{Name: channelName} editChannelData := discordgo.ChannelEdit{Name: channelName}
_, err := s.ChannelEdit(vs.ChannelID, &editChannelData) _, err := s.ChannelEdit(channelID, &editChannelData)
if err != nil { if err != nil {
log.Errorf("Cannot rename channel %s with new name %s in guild %s", vs.ChannelID, channelName, vs.GuildID) log.Errorf("Cannot rename channel %s with new name %s in guild %s", channelID, channelName, guild.id)
return return
} }
createdChannels[vs.GuildID][vs.ChannelID] = []string{vs.UserID} guild.createdChannels[channelID] = []string{userID}
log.Debugf("Renamed channel %s to %s in guild %s", vs.ChannelID, channelName, vs.GuildID) log.Debugf("Renamed channel %s to %s in guild %s", channelID, channelName, guild.id)
createNewChannelEndpoint(s, vs.GuildID, category.ID) guild.createNewChannelEndpoint(s, category.ID)
} }
func removeUserFromChannels(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { func (guild *AutoGuild) removeUserFromChannels(s *discordgo.Session, userID string) {
for channelID, channel := range createdChannels[vs.GuildID] { for channelID, channel := range guild.createdChannels {
index := contains(channel, vs.UserID) index := contains(channel, userID)
if index != -1 { if index != -1 {
if len(channel) == 1 { if len(channel) == 1 {
// If this user was the only one left in the channel, delete the channel
_, err := s.ChannelDelete(channelID) _, err := s.ChannelDelete(channelID)
if err != nil { if err != nil {
log.Errorf("Cannot delete channel %s in guild %s", channelID, vs.GuildID) log.Errorf("Cannot delete channel %s in guild %s", channelID, guild.id)
} else { } else {
log.Debugf("Channel %s deleted in guild %s", channelID, vs.GuildID) log.Debugf("Channel %s deleted in guild %s", channelID, guild.id)
delete(createdChannels[vs.GuildID], channelID) delete(guild.createdChannels, channelID)
} }
} else { } else {
createdChannels[vs.GuildID][channelID] = remove(channel, index) // Otherwise update the channel name and remove user from the list
updateChannelName(s, vs.GuildID, channelID) guild.createdChannels[channelID] = remove(channel, index)
guild.updateChannelName(s, channelID)
} }
} }
} }
} }
func updateNameOnJoin(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { func (guild *AutoGuild) trackUserJoinManagedChannel(s *discordgo.Session, channelID string, userID string) {
updateChannelName(s, vs.GuildID, vs.ChannelID) guild.createdChannels[channelID] = append(guild.createdChannels[channelID], userID)
guild.updateChannelName(s, channelID)
} }
func updateChannelName(s *discordgo.Session, guildID string, channelID string) { func (guild *AutoGuild) updateChannelName(s *discordgo.Session, channelID string) {
programs := make(map[string]int) programs := make(map[string]int)
for _, userID := range createdChannels[guildID][channelID] { for _, userID := range guild.createdChannels[channelID] {
programs[getActivity(s.State, guildID, userID)] += 1 programs[guild.getActivity(s.State, userID)] += 1
} }
max := -1 max := -1
name := "Stanza" name := DEFAULT_CHANNEL_NAME
for program, count := range programs { for program, count := range programs {
if program != "Stanza" && count > max { if program != DEFAULT_CHANNEL_NAME && count > max {
max = count max = count
name = program name = program
} }
@@ -122,7 +135,7 @@ func updateChannelName(s *discordgo.Session, guildID string, channelID string) {
channel, err := s.Channel(channelID) channel, err := s.Channel(channelID)
if err != nil { if err != nil {
log.Errorf("Channel %s not found in guild %s", channelID, guildID) log.Errorf("Channel %s not found in guild %s", channelID, guild.id)
return return
} }
@@ -135,16 +148,16 @@ func updateChannelName(s *discordgo.Session, guildID string, channelID string) {
editChannelData := discordgo.ChannelEdit{Name: name} editChannelData := discordgo.ChannelEdit{Name: name}
_, err = s.ChannelEdit(channelID, &editChannelData) _, err = s.ChannelEdit(channelID, &editChannelData)
if err != nil { if err != nil {
log.Errorf("Cannot rename channel %s with new name %s in guild %s", channelID, name, guildID) log.Errorf("Cannot rename channel %s with new name %s in guild %s", channelID, name, guild.id)
} }
log.Debugf("Renamed channel %s from %s to %s in guild %s", channelID, oldName, name, guildID) log.Debugf("Renamed channel %s from %s to %s in guild %s", channelID, oldName, name, guild.id)
} }
func getActivity(st *discordgo.State, guildID string, userID string) string { func (guild *AutoGuild) getActivity(st *discordgo.State, userID string) string {
presence, err := st.Presence(guildID, userID) presence, err := st.Presence(guild.id, userID)
if err != nil || len(presence.Activities) == 0 { if err != nil || len(presence.Activities) == 0 {
return "Stanza" return DEFAULT_CHANNEL_NAME
} }
for _, activity := range presence.Activities { for _, activity := range presence.Activities {
@@ -153,5 +166,5 @@ func getActivity(st *discordgo.State, guildID string, userID string) string {
} }
} }
return "Stanza" return DEFAULT_CHANNEL_NAME
} }

27
main.go
View File

@@ -70,20 +70,23 @@ func main() {
} }
func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) { func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
if val, ok := newChannelEndpoint[vs.GuildID]; ok { if autoGuild, ok := managedGuilds[vs.GuildID]; ok {
if vs.ChannelID == "" { if vs.ChannelID == "" {
removeUserFromChannels(s, vs) // The user left a channel
autoGuild.removeUserFromChannels(s, vs.UserID)
return return
} }
if vs.ChannelID == val.ID { if vs.ChannelID == autoGuild.newChannelEndpoint.ID {
removeUserFromChannels(s, vs) // User joined the new channel endpoint
createNewChannel(s, vs) autoGuild.removeUserFromChannels(s, vs.UserID)
} else if _, ok := createdChannels[vs.GuildID][vs.ChannelID]; ok { autoGuild.createNewChannel(s, vs.ChannelID, vs.UserID)
createdChannels[vs.GuildID][vs.ChannelID] = append(createdChannels[vs.GuildID][vs.ChannelID], vs.UserID) } else if _, ok := autoGuild.createdChannels[vs.ChannelID]; ok {
updateNameOnJoin(s, vs) // User joined a managed channel
autoGuild.trackUserJoinManagedChannel(s, vs.ChannelID, vs.UserID)
} else { } else {
removeUserFromChannels(s, vs) // User joined a non managed channel
autoGuild.removeUserFromChannels(s, vs.UserID)
} }
} else { } else {
initChannels(s, vs.GuildID) initChannels(s, vs.GuildID)
@@ -91,11 +94,11 @@ func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
} }
func handlePresenceUpdate(s *discordgo.Session, pu *discordgo.PresenceUpdate) { func handlePresenceUpdate(s *discordgo.Session, pu *discordgo.PresenceUpdate) {
if _, ok := newChannelEndpoint[pu.GuildID]; ok { if autoGuild, ok := managedGuilds[pu.GuildID]; ok {
for channelID, channel := range createdChannels[pu.GuildID] { for channelID, channel := range autoGuild.createdChannels {
for _, user := range channel { for _, user := range channel {
if user == pu.User.ID { if user == pu.User.ID {
updateChannelName(s, pu.GuildID, channelID) autoGuild.updateChannelName(s, channelID)
} }
} }
} }