Improve embed, return interaction err properly, improve definition, add keywords to search

This commit is contained in:
2024-01-18 00:19:32 -06:00
parent dde00824e0
commit f5da706933
+27 -13
View File
@@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv" "strings"
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@@ -25,8 +25,8 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
MinLength: GetPointer(0), MinLength: GetPointer(0),
MaxLength: 16, MaxLength: 16,
Name: "name", Name: "title",
Description: "Course Name", Description: "Course Title",
Required: false, Required: false,
}, },
{ {
@@ -42,6 +42,12 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
Description: "Maximum number of results", Description: "Maximum number of results",
Required: false, Required: false,
}, },
{
Type: discordgo.ApplicationCommandOptionString,
Name: "keywords",
Description: "Keywords in Title or Description (space separated)",
Required: false,
},
}, },
} }
@@ -51,10 +57,18 @@ func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.Int
for _, option := range data.Options { for _, option := range data.Options {
switch option.Name { switch option.Name {
case "name": case "title":
query.Title(option.StringValue())
case "code": case "code":
// TODO: Handle & parse course codes properly
case "keywords":
query.Keywords(
strings.Split(option.StringValue(), " "),
)
case "max": case "max":
query.MaxResults(int(option.IntValue())) query.MaxResults(
min(8, int(option.IntValue())),
)
} }
} }
@@ -74,21 +88,21 @@ func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.Int
for _, course := range courses.Data { for _, course := range courses.Data {
fields = append(fields, &discordgo.MessageEmbedField{ fields = append(fields, &discordgo.MessageEmbedField{
Name: "Identifier",
Value: fmt.Sprintf("%s %s-%s [%s]", course.Subject, course.CourseNumber, course.SequenceNumber, course.CourseReferenceNumber),
Inline: true,
}, &discordgo.MessageEmbedField{
Name: "Name", Name: "Name",
Value: course.CourseTitle, Value: course.CourseTitle,
Inline: true, Inline: true,
}, &discordgo.MessageEmbedField{ }, &discordgo.MessageEmbedField{
Name: "CRN", Name: "Meeting Time",
Value: course.CourseReferenceNumber, Value: "MWF 11AM-12:15PM",
Inline: true,
}, &discordgo.MessageEmbedField{
Name: "Credits",
Value: strconv.Itoa(course.CreditHours),
Inline: true, Inline: true,
}) })
} }
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ err = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{ Embeds: []*discordgo.MessageEmbed{
@@ -104,7 +118,7 @@ func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.Int
}, },
}) })
return nil return err
} }
var TermCommandDefinition = &discordgo.ApplicationCommand{ var TermCommandDefinition = &discordgo.ApplicationCommand{