WebhookCheck/main.go

172 lines
4.7 KiB
Go
Raw Normal View History

2024-06-14 17:59:45 +02:00
package main
import (
"fmt"
"os"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
Token string
)
func init() {
Token = os.Getenv("DISCORD_BOT_TOKEN")
if Token == "" {
fmt.Println("No token provided. Please set DISCORD_BOT_TOKEN environment variable.")
os.Exit(1)
}
}
func main() {
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
dg.AddHandler(ready)
dg.AddHandler(interactionCreate)
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
fmt.Println("Bot is now running. Press CTRL+C to exit.")
select {}
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
s.UpdateGameStatus(0, "Listing webhooks")
command := &discordgo.ApplicationCommand{
Name: "webhooks",
Description: "List all webhooks in the server",
}
_, err := s.ApplicationCommandCreate(s.State.User.ID, "", command)
if err != nil {
fmt.Printf("Cannot create slash command: %v\n", err)
return
}
fmt.Println("Slash command /webhooks created successfully")
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand {
switch i.ApplicationCommandData().Name {
case "webhooks":
handleWebhooksCommand(s, i)
}
}
}
func handleWebhooksCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Fetching webhooks...",
},
})
if err != nil {
fmt.Printf("Error responding to interaction: %v\n", err)
return
}
2024-06-14 18:22:57 +02:00
go fetchAndSendWebhookResponses(s, i)
2024-06-14 17:59:45 +02:00
}
2024-06-14 18:22:57 +02:00
func fetchAndSendWebhookResponses(s *discordgo.Session, i *discordgo.InteractionCreate) {
webhooks, channelNames, channelCategories, err := getAllWebhooks(s, i.GuildID)
2024-06-14 17:59:45 +02:00
if err != nil {
fmt.Printf("Error retrieving webhooks: %v\n", err)
editMessage := fmt.Sprintf("Error retrieving webhooks: %v", err)
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: &editMessage,
})
return
}
if len(webhooks) == 0 {
fmt.Println("No webhooks found in this server.")
editMessage := "No webhooks found in this server."
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: &editMessage,
})
return
}
2024-06-14 18:22:57 +02:00
webhooksByCategory := make(map[string][]*discordgo.Webhook)
2024-06-14 17:59:45 +02:00
for _, webhook := range webhooks {
2024-06-14 18:22:57 +02:00
categoryName := channelCategories[webhook.ChannelID]
webhooksByCategory[categoryName] = append(webhooksByCategory[categoryName], webhook)
2024-06-14 17:59:45 +02:00
}
2024-06-14 18:22:57 +02:00
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,
})
}
2024-06-14 17:59:45 +02:00
}
2024-06-14 18:22:57 +02:00
func getAllWebhooks(s *discordgo.Session, serverID string) ([]*discordgo.Webhook, map[string]string, map[string]string, error) {
2024-06-14 17:59:45 +02:00
channels, err := s.GuildChannels(serverID)
if err != nil {
2024-06-14 18:22:57 +02:00
return nil, nil, nil, fmt.Errorf("could not fetch channels: %v", err)
2024-06-14 17:59:45 +02:00
}
channelNames := make(map[string]string)
2024-06-14 18:22:57 +02:00
channelCategories := make(map[string]string)
categoryNames := make(map[string]string)
2024-06-14 17:59:45 +02:00
var allWebhooks []*discordgo.Webhook
2024-06-14 18:22:57 +02:00
2024-06-14 17:59:45 +02:00
for _, channel := range channels {
channelNames[channel.ID] = channel.Name
2024-06-14 18:22:57 +02:00
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
}
2024-06-14 17:59:45 +02:00
webhooks, err := s.ChannelWebhooks(channel.ID)
if err != nil {
if restErr, ok := err.(*discordgo.RESTError); ok && restErr.Message != nil && restErr.Message.Code == discordgo.ErrCodeUnknownChannel {
fmt.Printf("Channel %s does not exist or is not accessible\n", channel.ID)
} else {
fmt.Printf("Error retrieving webhooks for channel %s: %v\n", channel.ID, err)
}
continue
}
allWebhooks = append(allWebhooks, webhooks...)
}
2024-06-14 18:22:57 +02:00
return allWebhooks, channelNames, channelCategories, nil
2024-06-14 17:59:45 +02:00
}