Fix command creation

This commit is contained in:
Joren 2024-07-06 18:06:55 +02:00
parent 2598aa854a
commit e57d3ff4fe
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

121
main.go
View File

@ -6,8 +6,8 @@ import (
"log"
"os"
"os/signal"
"syscall"
"strings"
"syscall"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
@ -27,7 +27,7 @@ type Config struct {
type Discord struct {
Token string `toml:"token"`
AppID string `toml:"appid"`
AppID string `toml:"appid"`
GuildID string `toml:"guildid"`
}
@ -105,27 +105,7 @@ var (
commands = []discordgo.ApplicationCommand{
{
Name: "rickroll-em",
Type: discordgo.UserApplicationCommand,
},
{
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,
Description: "List all webhooks in the server",
},
}
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
@ -161,76 +141,6 @@ var (
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,27 +152,26 @@ func main() {
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
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) {
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
h(client, i)
}
})
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()
if err != nil {
log.Println("Error opening connection:", err)