From 94399d13b3d4c92fdfcded1f91decd7ea5624a3c Mon Sep 17 00:00:00 2001 From: Joren Date: Fri, 14 Jun 2024 18:22:57 +0200 Subject: [PATCH] Send channels in chunks per cat --- main.go | 60 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 1368117..3239d3b 100644 --- a/main.go +++ b/main.go @@ -80,11 +80,11 @@ func handleWebhooksCommand(s *discordgo.Session, i *discordgo.InteractionCreate) return } - go fetchAndEditResponse(s, i) + go fetchAndSendWebhookResponses(s, i) } -func fetchAndEditResponse(s *discordgo.Session, i *discordgo.InteractionCreate) { - webhooks, channelNames, err := getAllWebhooks(s, i.GuildID) +func fetchAndSendWebhookResponses(s *discordgo.Session, i *discordgo.InteractionCreate) { + webhooks, channelNames, channelCategories, err := getAllWebhooks(s, i.GuildID) if err != nil { fmt.Printf("Error retrieving webhooks: %v\n", err) editMessage := fmt.Sprintf("Error retrieving webhooks: %v", err) @@ -103,32 +103,58 @@ func fetchAndEditResponse(s *discordgo.Session, i *discordgo.InteractionCreate) return } - var response strings.Builder + webhooksByCategory := make(map[string][]*discordgo.Webhook) for _, webhook := range webhooks { - channelName := channelNames[webhook.ChannelID] - 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) - response.WriteString(str) - fmt.Println(str) + categoryName := channelCategories[webhook.ChannelID] + webhooksByCategory[categoryName] = append(webhooksByCategory[categoryName], webhook) } - fmt.Println("Editing response with the list of webhooks.") - finalResponse := response.String() - s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ - Content: &finalResponse, - }) + for categoryName, webhooks := range webhooksByCategory { + var response strings.Builder + response.WriteString(fmt.Sprintf("**Category: %s**\n", categoryName)) + for _, webhook := range webhooks { + channelName := channelNames[webhook.ChannelID] + link := fmt.Sprintf("https://discord.com/api/webhooks/%s/%s", webhook.ID, webhook.Token) + str := fmt.Sprintf("Channel: %s (<#%s>), Name: %s, Webhook: %s\n",channelName,webhook.ChannelID, webhook.Name, link) + response.WriteString(str) + } + + finalResponse := response.String() + s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + 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) 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) + channelCategories := make(map[string]string) + categoryNames := make(map[string]string) var allWebhooks []*discordgo.Webhook + for _, channel := range channels { 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) if err != nil { 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...) } - return allWebhooks, channelNames, nil + return allWebhooks, channelNames, channelCategories, nil }