Fix command creation
This commit is contained in:
parent
2598aa854a
commit
e57d3ff4fe
119
main.go
119
main.go
@ -6,8 +6,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
@ -105,27 +105,7 @@ var (
|
|||||||
commands = []discordgo.ApplicationCommand{
|
commands = []discordgo.ApplicationCommand{
|
||||||
{
|
{
|
||||||
Name: "rickroll-em",
|
Name: "rickroll-em",
|
||||||
Type: discordgo.UserApplicationCommand,
|
Description: "List all webhooks in the server",
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "google-it",
|
|
||||||
Type: discordgo.MessageApplicationCommand,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "stackoverflow-it",
|
|
||||||
Type: discordgo.MessageApplicationCommand,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "godoc-it",
|
|
||||||
Type: discordgo.MessageApplicationCommand,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "discordjs-it",
|
|
||||||
Type: discordgo.MessageApplicationCommand,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "discordpy-it",
|
|
||||||
Type: discordgo.MessageApplicationCommand,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
@ -161,76 +141,6 @@ var (
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"google-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: searchLink(
|
|
||||||
i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
|
|
||||||
"https://google.com/search?q=%s", "+"),
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"stackoverflow-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: searchLink(
|
|
||||||
i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
|
|
||||||
"https://stackoverflow.com/search?q=%s", "+"),
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"godoc-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: searchLink(
|
|
||||||
i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
|
|
||||||
"https://pkg.go.dev/search?q=%s", "+"),
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"discordjs-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: searchLink(
|
|
||||||
i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
|
|
||||||
"https://discord.js.org/#/docs/main/stable/search?query=%s", "+"),
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"discordpy-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: searchLink(
|
|
||||||
i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
|
|
||||||
"https://discordpy.readthedocs.io/en/stable/search.html?q=%s", "+"),
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -242,8 +152,19 @@ func main() {
|
|||||||
|
|
||||||
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
||||||
log.Println("Bot is online")
|
log.Println("Bot is online")
|
||||||
})
|
|
||||||
|
|
||||||
|
cmdIDs := make(map[string]string, len(commands))
|
||||||
|
|
||||||
|
for _, cmd := range commands {
|
||||||
|
fmt.Println(cmd)
|
||||||
|
rcmd, err := client.ApplicationCommandCreate(client.State.User.ID, config.Discord.GuildID, &cmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdIDs[rcmd.ID] = rcmd.Name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
client.AddHandler(func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
client.AddHandler(func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
|
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
|
||||||
@ -251,18 +172,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
cmdIDs := make(map[string]string, len(commands))
|
|
||||||
|
|
||||||
for _, cmd := range commands {
|
|
||||||
rcmd, err := client.ApplicationCommandCreate(config.Discord.AppID, config.Discord.GuildID, &cmd)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdIDs[rcmd.ID] = rcmd.Name
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
err := client.Open()
|
err := client.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error opening connection:", err)
|
log.Println("Error opening connection:", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user