From f7475d684b20a5950e16133322171ddcde75c746 Mon Sep 17 00:00:00 2001 From: Logan Gartner Date: Fri, 1 Jul 2022 04:06:03 -0600 Subject: [PATCH] Implemented slash commands --- main.go | 53 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index d3a5cd1..b30d14f 100644 --- a/main.go +++ b/main.go @@ -10,13 +10,13 @@ import ( "strings" "regexp" "context" + "time" "github.com/bwmarrin/discordgo" "github.com/wader/goutubedl" "mvdan.cc/xurls/v2" // Peak lazy ) -/* // Variables used for command line parameters var ( commands = []*discordgo.ApplicationCommand { @@ -24,25 +24,46 @@ var ( Name: "ping", Description: "Hopefully replies with pong or else I'll be sad.", }, + { + Name: "slowping", + Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.", + }, } commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) { "ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Hey there! Congratulations, you just executed your first slash command", + //Flags: 1 << 6, // Only lets issuer see initial response + Content: "Pong!", }, }) }, + "slowping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + //Flags: 1 << 6, // Only lets issuer see initial response + Content: "No problem.", + }, + }) + + time.Sleep(time.Second * 10) + + _, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: "Pong!", + }) + if err != nil { + log.Println(err) + } + }, } ) -*/ // Bot params var ( GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally") BotToken = flag.String("token", "", "Bot access token") - RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") ) // Lazyinator 9001 @@ -60,13 +81,11 @@ func init() { return } - /* s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { h(s, i) } }) - */ } func main() { @@ -78,7 +97,7 @@ func main() { s.AddHandler(messageCreate) // In this example, we only care about receiving message events. - s.Identify.Intents = discordgo.IntentsGuildMessages + //s.Identify.Intents = discordgo.IntentsGuildMessages // Open a websocket connection to Discord and begin listening. err = s.Open() @@ -87,15 +106,19 @@ func main() { return } + log.Println("Adding commands...") + //registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) + + registeredCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands) + if err != nil { + log.Panic(err) + } + /* - fmt.Println("Adding commands...") - registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) for i, v := range commands { cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v) if err != nil { - //log.Panicf("Cannot create '%v' command: %v", v.Name, err) - fmt.Println("Bad") - panic(1) + log.Panicf("Cannot create '%v' command: %v", v.Name, err) } registeredCommands[i] = cmd } @@ -111,6 +134,12 @@ func main() { <-sc // Cleanly close down the Discord session. + for _, v := range registeredCommands { + err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID) + if err != nil { + log.Panicf("Cannot delete '%v' command: %v", v.Name, err) + } + } s.Close() }