From c6b83b883c01cfab8a21813f91e2db613d21909f Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Dec 2023 01:39:04 -0600 Subject: [PATCH] Move command definition/handler into commands.go --- commands.go | 93 +++++++++++++++++++++++++++++++++++++++++ main.go | 117 ---------------------------------------------------- 2 files changed, 93 insertions(+), 117 deletions(-) create mode 100644 commands.go diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..b4fea9d --- /dev/null +++ b/commands.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + "strconv" + "time" + + "github.com/bwmarrin/discordgo" +) + +var RegisterCommandDefinition = &discordgo.ApplicationCommand{ + Name: "register", + Description: "Register a vehicle for parking", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "location", + Description: "The complex to register with", + Required: true, + Autocomplete: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "make", + Description: "Make of Vehicle (e.g. Honda)", + Required: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "model", + Description: "Model of Vehicle (e.g. Civic)", + Required: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "plate", + Description: "License Plate Number (e.g. 123ABC)", + Required: true, + }, + }, +} + +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 { + // Seed value is based on the user ID + a 15 minute interval) + user_id, _ := strconv.Atoi(interaction.Member.User.ID) + seed_value := int64(user_id) + (time.Now().Unix() / 15 * 60) + locations := FilterLocations(GetLocations(), data.Options[0].StringValue(), 25, seed_value) + + choices = make([]*discordgo.ApplicationCommandOptionChoice, len(locations)) + 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) + } + } +} diff --git a/main.go b/main.go index 3608589..7fb420a 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,9 @@ package main import ( - "fmt" "log" "os" "os/signal" - "strconv" - "time" "github.com/bwmarrin/discordgo" "github.com/joho/godotenv" @@ -20,120 +17,6 @@ var ( } ) -var RegisterCommandDefinition = &discordgo.ApplicationCommand{ - Name: "register", - Description: "Register a vehicle for parking", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "location", - Description: "The complex to register with", - Required: true, - Autocomplete: true, - }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "make", - Description: "Make of Vehicle (e.g. Honda)", - Required: true, - }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "model", - Description: "Model of Vehicle (e.g. Civic)", - Required: true, - }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "plate", - Description: "License Plate Number (e.g. 123ABC)", - Required: true, - }, - }, -} - -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 { - // Seed value is based on the user ID + a 15 minute interval) - user_id, _ := strconv.Atoi(interaction.Member.User.ID) - seed_value := int64(user_id) + (time.Now().Unix() / 15 * 60) - locations := FilterLocations(GetLocations(), data.Options[0].StringValue(), 25, seed_value) - - choices = make([]*discordgo.ApplicationCommandOptionChoice, len(locations)) - 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() { -// body := EncodeForm( -// map[string]string{ -// "propertyIdSelected": "22167", -// "propertySource": "parking-snap", -// "guestCode": code, -// }) - -// req, err := http.NewRequest("POST", url, strings.NewReader(body)) -// if err != nil { -// log.Fatal(err) -// } - -// req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") -// req.Header.Set("Referer", "https://www.register2park.com/register?key=678zv9zzylvw") -// req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0") -// req.Header.Set("X-Requested-With", "XMLHttpRequest") -// req.Header.Set("Host", "www.register2park.com") -// req.Header.Set("Cookie", "PHPSESSID=dbc956tgnapqv6l81ue56uf1ng") - -// resp, err := client.Do(req) -// if err != nil { -// log.Fatal(err) -// } - -// if resp.StatusCode == 200 { -// } - -// } - func main() { // Load environment variables if err := godotenv.Load(); err != nil {