mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-10 10:08:18 -06:00
Invoke reload() in main, location autocomplete testing
This commit is contained in:
61
main.go
61
main.go
@@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@@ -49,8 +52,54 @@ var RegisterCommandDefinition = &discordgo.ApplicationCommand{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterCommandHandler(s *discordgo.Session, m *discordgo.InteractionCreate) {
|
func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
||||||
|
switch interaction.Type {
|
||||||
|
case discordgo.InteractionApplicationCommand:
|
||||||
|
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{
|
||||||
|
{
|
||||||
|
Footer: &discordgo.MessageEmbedFooter{
|
||||||
|
Text: fmt.Sprintf("Fetched at %s", time.Now().Format("Monday, January 2, 2006 at 3:04:05PM")),
|
||||||
|
},
|
||||||
|
Description: "",
|
||||||
|
Fields: []*discordgo.MessageEmbedField{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
case discordgo.InteractionApplicationCommandAutocomplete:
|
||||||
|
data := interaction.ApplicationCommandData()
|
||||||
|
|
||||||
|
var choices []*discordgo.ApplicationCommandOptionChoice
|
||||||
|
|
||||||
|
if data.Options[0].Focused {
|
||||||
|
locations := GetLocations()
|
||||||
|
log.Printf("%d location%s provided", len(locations), Plural(len(locations)))
|
||||||
|
|
||||||
|
choices = make([]*discordgo.ApplicationCommandOptionChoice, min(len(locations), 25))
|
||||||
|
for i, location := range locations {
|
||||||
|
choices[i] = &discordgo.ApplicationCommandOptionChoice{
|
||||||
|
Name: location.name,
|
||||||
|
Value: strconv.Itoa(int(location.id)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
choices = []*discordgo.ApplicationCommandOptionChoice{}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Choices: choices, // This is basically the whole purpose of autocomplete interaction - return custom options to the user.
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func test() {
|
// func test() {
|
||||||
@@ -114,16 +163,18 @@ func main() {
|
|||||||
|
|
||||||
log.Printf("Adding %d command%s...", len(commandDefinitions), Plural(len(commandDefinitions)))
|
log.Printf("Adding %d command%s...", len(commandDefinitions), Plural(len(commandDefinitions)))
|
||||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
||||||
for i, v := range commandDefinitions {
|
for definitionIndex, commandDefinition := range commandDefinitions {
|
||||||
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)
|
command, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), commandDefinition)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
log.Panicf("Failed while registering '%v' command: %v", commandDefinition.Name, err)
|
||||||
}
|
}
|
||||||
registeredCommands[i] = cmd
|
registeredCommands[definitionIndex] = command
|
||||||
}
|
}
|
||||||
|
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
|
tryReload("")
|
||||||
|
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
log.Println("Press Ctrl+C to exit")
|
log.Println("Press Ctrl+C to exit")
|
||||||
|
|||||||
Reference in New Issue
Block a user