stabilized command registering
This commit is contained in:
2
main.go
2
main.go
@@ -39,7 +39,6 @@ func main() {
|
|||||||
fmt.Println("Discord session created")
|
fmt.Println("Discord session created")
|
||||||
}
|
}
|
||||||
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent
|
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent
|
||||||
defer removeCommandFromAllGuilds(discord)
|
|
||||||
discord.AddHandler(ready)
|
discord.AddHandler(ready)
|
||||||
discord.AddHandler(interactionCreate)
|
discord.AddHandler(interactionCreate)
|
||||||
discord.AddHandler(messageCreate)
|
discord.AddHandler(messageCreate)
|
||||||
@@ -53,7 +52,6 @@ func main() {
|
|||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
<-sc
|
<-sc
|
||||||
fmt.Println("\nShutting down...")
|
fmt.Println("\nShutting down...")
|
||||||
defer removeCommandFromAllGuilds(discord)
|
|
||||||
discord.Close()
|
discord.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
@@ -15,18 +16,22 @@ type Command struct {
|
|||||||
ModalID string
|
ModalID string
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command}
|
var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command, sticky_command, cat_command}
|
||||||
|
|
||||||
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||||
|
fmt.Print("\nStarting up... (This may take a bit when Discord rate limits the bot)\n")
|
||||||
|
removeOldCommandFromAllGuilds(s)
|
||||||
for _, guild := range event.Guilds {
|
for _, guild := range event.Guilds {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
_, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition)
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition)
|
||||||
|
fmt.Printf("\nAdded command \"%s\"", cmd.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error creating command,", err)
|
fmt.Println("error creating command,", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Print("Successfully started the Discord bot!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
@@ -48,19 +53,25 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeCommandFromAllGuilds(s *discordgo.Session) {
|
func removeOldCommandFromAllGuilds(s *discordgo.Session) {
|
||||||
for _, guild := range s.State.Guilds {
|
for _, guild := range s.State.Guilds {
|
||||||
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID)
|
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error fetching existing commands for guild %s: %v\n", guild.Name, err)
|
fmt.Printf("error fetching existing commands for guild %s: %v\n", guild.Name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
var commandIDs []string
|
||||||
|
for _, command := range commands {
|
||||||
|
commandIDs = append(commandIDs, command.Definition.Name)
|
||||||
|
}
|
||||||
for _, existingCommand := range existingCommands {
|
for _, existingCommand := range existingCommands {
|
||||||
|
if !slices.Contains(commandIDs, existingCommand.Name) {
|
||||||
|
fmt.Printf("\nDeleting command \"%s\"", existingCommand.Name)
|
||||||
err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID)
|
err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error deleting command %s for guild %s: %v\n", existingCommand.Name, guild.Name, err)
|
fmt.Printf("error deleting command %s for guild %s: %v\n", existingCommand.Name, guild.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user