Init
This commit is contained in:
commit
1a076e5313
10
go.mod
Normal file
10
go.mod
Normal file
@ -0,0 +1,10 @@
|
||||
module WebhookCheck
|
||||
|
||||
go 1.22.4
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.28.1 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||
)
|
12
go.sum
Normal file
12
go.sum
Normal file
@ -0,0 +1,12 @@
|
||||
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
||||
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
145
main.go
Normal file
145
main.go
Normal file
@ -0,0 +1,145 @@
|
||||
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
|
||||
}
|
||||
|
||||
go fetchAndEditResponse(s, i)
|
||||
}
|
||||
|
||||
func fetchAndEditResponse(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
webhooks, channelNames, err := getAllWebhooks(s, i.GuildID)
|
||||
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
|
||||
}
|
||||
|
||||
var response strings.Builder
|
||||
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)
|
||||
}
|
||||
|
||||
fmt.Println("Editing response with the list of webhooks.")
|
||||
finalResponse := response.String()
|
||||
s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: &finalResponse,
|
||||
})
|
||||
}
|
||||
|
||||
func getAllWebhooks(s *discordgo.Session, serverID string) ([]*discordgo.Webhook, map[string]string, error) {
|
||||
channels, err := s.GuildChannels(serverID)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not fetch channels: %v", err)
|
||||
}
|
||||
|
||||
channelNames := make(map[string]string)
|
||||
var allWebhooks []*discordgo.Webhook
|
||||
for _, channel := range channels {
|
||||
channelNames[channel.ID] = channel.Name
|
||||
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...)
|
||||
}
|
||||
return allWebhooks, channelNames, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user