Work on SearchCommandHandler & definition

This commit is contained in:
2024-01-17 23:22:11 -06:00
parent 6d758ed396
commit 1baca364b0

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@@ -14,7 +15,6 @@ var (
TermCommandDefinition.Name: TermCommandHandler, TermCommandDefinition.Name: TermCommandHandler,
SearchCommandDefinition.Name: SearchCommandHandler, SearchCommandDefinition.Name: SearchCommandHandler,
} }
minLength = 0
) )
var SearchCommandDefinition = &discordgo.ApplicationCommand{ var SearchCommandDefinition = &discordgo.ApplicationCommand{
@@ -23,35 +23,63 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
MinLength: &minLength, MinLength: GetPointer(0),
MaxLength: 8, MaxLength: 16,
Name: "query", Name: "name",
Description: "Search query", Description: "Course Name",
Required: true, Required: false,
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "code",
MinLength: GetPointer(2),
Description: "Course Code (e.g. 3743, 3000-3999, 3xxx, 3000-)",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "max",
Description: "Maximum number of results",
Required: false,
}, },
}, },
} }
func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) { func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
query := Query{ data := interaction.ApplicationCommandData()
keywords: &[]string{"Computer Science"}, query := NewQuery().Credits(3, 6)
countOption := data.Options[2]
if countOption.Value != nil {
query.MaxResults(int(countOption.IntValue()))
} }
courses := Search(query, 0, 5, "", false)
courses, err := Search(query, "", false)
if err != nil {
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error searching for courses",
},
})
return
}
fetch_time := time.Now() fetch_time := time.Now()
fields := []*discordgo.MessageEmbedField{} fields := []*discordgo.MessageEmbedField{}
for _, course := range courses.Data { for i, course := range courses.Data {
fields = append(fields, &discordgo.MessageEmbedField{ fields = append(fields, &discordgo.MessageEmbedField{
Name: "Name", Name: "Name",
Value: course.CourseTitle, Value: fmt.Sprintf("%s %d", course.CourseTitle, i),
Inline: true, Inline: true,
}, &discordgo.MessageEmbedField{ }, &discordgo.MessageEmbedField{
Name: "CRN", Name: "CRN",
Value: "12345", Value: course.CourseReferenceNumber,
Inline: true, Inline: true,
}, &discordgo.MessageEmbedField{ }, &discordgo.MessageEmbedField{
Name: "Credits", Name: "Credits",
Value: "3", Value: strconv.Itoa(course.CreditHours),
Inline: true, Inline: true,
}) })
} }
@@ -79,7 +107,7 @@ var TermCommandDefinition = &discordgo.ApplicationCommand{
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
MinLength: &minLength, MinLength: GetPointer(0),
MaxLength: 8, MaxLength: 8,
Name: "term", Name: "term",
Description: "Term to search for", Description: "Term to search for",