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
}
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
}
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
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, 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)
fmt.Println(str)
}
fmt.Println("Editing response with the list of webhooks.")
finalResponse := response.String()
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: &finalResponse,
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
}