Auto rename channels
This commit is contained in:
@@ -73,6 +73,26 @@ func removeUserFromChannels(s *discordgo.Session, vs *discordgo.VoiceStateUpdate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateChannelName(s *discordgo.Session, guildID string, channelID string) {
|
||||||
|
programs := make(map[string]int)
|
||||||
|
|
||||||
|
for _, userID := range createdChannels[guildID][channelID] {
|
||||||
|
programs[getActivity(s.State, guildID, userID)] += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
max := -1
|
||||||
|
name := "Stanza"
|
||||||
|
|
||||||
|
for program, count := range programs {
|
||||||
|
if count > max {
|
||||||
|
max = count
|
||||||
|
name = program
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ChannelEdit(channelID, name)
|
||||||
|
}
|
||||||
|
|
||||||
func getActivity(st *discordgo.State, guildID string, userID string) string {
|
func getActivity(st *discordgo.State, guildID string, userID string) string {
|
||||||
presence, err := st.Presence(guildID, userID)
|
presence, err := st.Presence(guildID, userID)
|
||||||
|
|
||||||
|
|||||||
15
main.go
15
main.go
@@ -50,6 +50,7 @@ func main() {
|
|||||||
// Register the messageCreate func as a callback for MessageCreate events.
|
// Register the messageCreate func as a callback for MessageCreate events.
|
||||||
dg.AddHandler(handleMessage)
|
dg.AddHandler(handleMessage)
|
||||||
dg.AddHandler(handleVoiceActivity)
|
dg.AddHandler(handleVoiceActivity)
|
||||||
|
dg.AddHandler(handlePresenceUpdate)
|
||||||
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll)
|
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll)
|
||||||
|
|
||||||
// Open a websocket connection to Discord and begin listening.
|
// Open a websocket connection to Discord and begin listening.
|
||||||
@@ -89,6 +90,20 @@ func handleVoiceActivity(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handlePresenceUpdate(s *discordgo.Session, pu *discordgo.PresenceUpdate) {
|
||||||
|
if _, ok := newChannelEndpoint[pu.GuildID]; ok {
|
||||||
|
for channelID, channel := range createdChannels[pu.GuildID] {
|
||||||
|
for _, user := range channel {
|
||||||
|
if user == pu.User.ID {
|
||||||
|
updateChannelName(s, pu.GuildID, channelID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
initChannels(s, pu.GuildID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This function will be called (due to AddHandler above) every time a new
|
// 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.
|
// message is created on any channel that the autenticated bot has access to.
|
||||||
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|||||||
Reference in New Issue
Block a user