Send channels in chunks per cat

This commit is contained in:
Joren 2024-06-14 18:22:57 +02:00
parent 1a076e5313
commit 94399d13b3
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

48
main.go
View File

@ -80,11 +80,11 @@ func handleWebhooksCommand(s *discordgo.Session, i *discordgo.InteractionCreate)
return return
} }
go fetchAndEditResponse(s, i) go fetchAndSendWebhookResponses(s, i)
} }
func fetchAndEditResponse(s *discordgo.Session, i *discordgo.InteractionCreate) { func fetchAndSendWebhookResponses(s *discordgo.Session, i *discordgo.InteractionCreate) {
webhooks, channelNames, err := getAllWebhooks(s, i.GuildID) webhooks, channelNames, channelCategories, err := getAllWebhooks(s, i.GuildID)
if err != nil { if err != nil {
fmt.Printf("Error retrieving webhooks: %v\n", err) fmt.Printf("Error retrieving webhooks: %v\n", err)
editMessage := fmt.Sprintf("Error retrieving webhooks: %v", err) editMessage := fmt.Sprintf("Error retrieving webhooks: %v", err)
@ -103,32 +103,58 @@ func fetchAndEditResponse(s *discordgo.Session, i *discordgo.InteractionCreate)
return return
} }
webhooksByCategory := make(map[string][]*discordgo.Webhook)
for _, webhook := range webhooks {
categoryName := channelCategories[webhook.ChannelID]
webhooksByCategory[categoryName] = append(webhooksByCategory[categoryName], webhook)
}
for categoryName, webhooks := range webhooksByCategory {
var response strings.Builder var response strings.Builder
response.WriteString(fmt.Sprintf("**Category: %s**\n", categoryName))
for _, webhook := range webhooks { for _, webhook := range webhooks {
channelName := channelNames[webhook.ChannelID] channelName := channelNames[webhook.ChannelID]
link := fmt.Sprintf("https://discord.com/api/webhooks/%s/%s", webhook.ID, webhook.Token) link := fmt.Sprintf("https://discord.com/api/webhooks/%s/%s", webhook.ID, webhook.Token)
str := fmt.Sprintf("Channel: %s, Name: %s, [Webhook Link](%s)\n", channelName, webhook.Name, link) str := fmt.Sprintf("Channel: %s (<#%s>), Name: %s, Webhook: %s\n",channelName,webhook.ChannelID, webhook.Name, link)
response.WriteString(str) response.WriteString(str)
fmt.Println(str)
} }
fmt.Println("Editing response with the list of webhooks.")
finalResponse := response.String() finalResponse := response.String()
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: &finalResponse, Content: finalResponse,
}) })
}
} }
func getAllWebhooks(s *discordgo.Session, serverID string) ([]*discordgo.Webhook, map[string]string, error) { func getAllWebhooks(s *discordgo.Session, serverID string) ([]*discordgo.Webhook, map[string]string, map[string]string, error) {
channels, err := s.GuildChannels(serverID) channels, err := s.GuildChannels(serverID)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("could not fetch channels: %v", err) return nil, nil, nil, fmt.Errorf("could not fetch channels: %v", err)
} }
channelNames := make(map[string]string) channelNames := make(map[string]string)
channelCategories := make(map[string]string)
categoryNames := make(map[string]string)
var allWebhooks []*discordgo.Webhook var allWebhooks []*discordgo.Webhook
for _, channel := range channels { for _, channel := range channels {
channelNames[channel.ID] = channel.Name channelNames[channel.ID] = channel.Name
if channel.Type == discordgo.ChannelTypeGuildCategory {
categoryNames[channel.ID] = channel.Name
}
}
for _, channel := range channels {
if channel.ParentID != "" {
channelCategories[channel.ID] = categoryNames[channel.ParentID]
} else {
channelCategories[channel.ID] = "Uncategorized"
}
if channel.Type != discordgo.ChannelTypeGuildText {
continue
}
webhooks, err := s.ChannelWebhooks(channel.ID) webhooks, err := s.ChannelWebhooks(channel.ID)
if err != nil { if err != nil {
if restErr, ok := err.(*discordgo.RESTError); ok && restErr.Message != nil && restErr.Message.Code == discordgo.ErrCodeUnknownChannel { if restErr, ok := err.(*discordgo.RESTError); ok && restErr.Message != nil && restErr.Message.Code == discordgo.ErrCodeUnknownChannel {
@ -140,6 +166,6 @@ func getAllWebhooks(s *discordgo.Session, serverID string) ([]*discordgo.Webhook
} }
allWebhooks = append(allWebhooks, webhooks...) allWebhooks = append(allWebhooks, webhooks...)
} }
return allWebhooks, channelNames, nil return allWebhooks, channelNames, channelCategories, nil
} }