Implemented slash commands

This commit is contained in:
Logan G 2022-07-01 04:06:03 -06:00
parent 2db56669f8
commit f7475d684b
Signed by: logan
GPG key ID: E328528C921E7A7A

53
main.go
View file

@ -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()
}