From a4c00af1fe5e38abd1a79ba8a20f93d80033596a Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 7 Jan 2024 08:38:17 -0600 Subject: [PATCH] Setup search command stub, remove unused term cmd option for term lookup cmd --- commands.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/commands.go b/commands.go index cbd5de8..ca53c41 100644 --- a/commands.go +++ b/commands.go @@ -10,12 +10,69 @@ import ( var ( commandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition} commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ - "time": TimeCommandHandler, - "term": TermCommandHandler, + "time": TimeCommandHandler, + "term": TermCommandHandler, + "search": SearchCommandHandler, } minLength = 0 ) +var SearchCommandDefinition = &discordgo.ApplicationCommand{ + Name: "search", + Description: "Search for a course", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + MinLength: &minLength, + MaxLength: 8, + Name: "query", + Description: "Search query", + Required: true, + }, + }, +} + +func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) { + query := Query{ + Keywords: &[]string{"Computer Science"}, + } + courses := Search(query, 0, 5, "", false) + fetch_time := time.Now() + fields := []*discordgo.MessageEmbedField{} + + for _, course := range courses.Data { + fields = append(fields, &discordgo.MessageEmbedField{ + Name: "Name", + Value: course.CourseTitle, + Inline: true, + }, &discordgo.MessageEmbedField{ + Name: "CRN", + Value: "12345", + Inline: true, + }, &discordgo.MessageEmbedField{ + Name: "Credits", + Value: "3", + Inline: true, + }) + } + + session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Footer: &discordgo.MessageEmbedFooter{ + Text: fmt.Sprintf("Fetched at %s", fetch_time.Format("Monday, January 2, 2006 at 3:04:05PM")), + }, + Description: "", + Fields: fields, + }, + }, + AllowedMentions: &discordgo.MessageAllowedMentions{}, + }, + }) +} + var TermCommandDefinition = &discordgo.ApplicationCommand{ Name: "term", Description: "Guess the current term, or search for a specific term", @@ -52,12 +109,6 @@ var TimeCommandDefinition = &discordgo.ApplicationCommand{ Description: "Course Reference Number", Required: true, }, - { - Type: discordgo.ApplicationCommandOptionString, - Name: "term", - Description: "Term", - Required: false, - }, }, } @@ -65,7 +116,6 @@ func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { fetch_time := time.Now() crn := i.ApplicationCommandData().Options[0].IntValue() courseMeetingTime, err := GetCourseMeetingTime(202420, int(crn)) - if err != nil { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource,