Add page number option to terms command, add comma printing to terms

This commit is contained in:
2024-02-16 17:04:18 -06:00
parent 7ce28b9f4a
commit 470415cc70
4 changed files with 41 additions and 23 deletions

View File

@@ -29,7 +29,7 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
MinLength: GetPointer(0),
MinLength: GetIntPointer(0),
MaxLength: 48,
Name: "title",
Description: "Course Title (exact, use autocomplete)",
@@ -39,7 +39,7 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "code",
MinLength: GetPointer(4),
MinLength: GetIntPointer(4),
Description: "Course Code (e.g. 3743, 3000-3999, 3xxx, 3000-)",
Required: false,
},
@@ -243,24 +243,40 @@ var TermCommandDefinition = &discordgo.ApplicationCommand{
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
MinLength: GetPointer(0),
MinLength: GetIntPointer(0),
MaxLength: 8,
Name: "search",
Description: "Term to search for",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "page",
Description: "Page Number",
Required: false,
MinValue: GetFloatPointer(1),
},
},
}
func TermCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) error {
data := interaction.ApplicationCommandData()
var searchTerm string
if len(data.Options) == 1 {
searchTerm = data.Options[0].StringValue()
searchTerm := ""
pageNumber := 1
for _, option := range data.Options {
switch option.Name {
case "search":
searchTerm = option.StringValue()
case "page":
pageNumber = int(option.IntValue())
default:
log.Warn().Str("option", option.Name).Msg("Unexpected option in term command")
}
}
terms, err := GetTerms(searchTerm, 1, 25)
termResult, err := GetTerms(searchTerm, pageNumber, 25)
if err != nil {
RespondError(session, interaction.Interaction, "Error while fetching terms", err)
@@ -269,15 +285,11 @@ func TermCommandHandler(session *discordgo.Session, interaction *discordgo.Inter
fields := []*discordgo.MessageEmbedField{}
for _, t := range terms {
for _, t := range termResult {
fields = append(fields, &discordgo.MessageEmbedField{
Name: "ID",
Name: t.Description,
Value: t.Code,
Inline: true,
}, &discordgo.MessageEmbedField{
Name: "Description",
Value: t.Description,
Inline: true,
})
}
@@ -289,7 +301,7 @@ func TermCommandHandler(session *discordgo.Session, interaction *discordgo.Inter
Embeds: []*discordgo.MessageEmbed{
{
Footer: GetFetchedFooter(fetch_time),
Description: fmt.Sprintf("%d Terms", len(terms)),
Description: p.Sprintf("%d of %d terms (page %d)", len(termResult), len(terms), pageNumber),
Fields: fields[:min(25, len(fields))],
},
},

13
go.mod
View File

@@ -4,7 +4,14 @@ go 1.21
require github.com/bwmarrin/discordgo v0.27.1
require github.com/joho/godotenv v1.5.1
require (
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.3.1
github.com/rs/zerolog v1.31.0
github.com/samber/lo v1.39.0
golang.org/x/text v0.14.0
)
require (
github.com/arran4/golang-ical v0.2.3 // indirect
@@ -13,10 +20,6 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/redis/go-redis/v9 v9.3.1 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
)

2
go.sum
View File

@@ -76,6 +76,8 @@ golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@@ -22,6 +22,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"github.com/samber/lo"
"golang.org/x/text/message"
)
var (
@@ -33,6 +34,7 @@ var (
isDevelopment bool
baseURL string // Base URL for all requests to the banner system
environment string
p *message.Printer = message.NewPrinter(message.MatchLanguage("en"))
CentralTimeLocation *time.Location
isClosing bool = false
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to `file`")
@@ -288,9 +290,9 @@ func main() {
}
// Fetch terms on startup
_, err = GetTerms("", 1, 10)
err = TryReloadTerms()
if err != nil {
log.Fatal().Stack().Err(err).Msg("Cannot get terms")
log.Fatal().Stack().Err(err).Msg("Cannot fetch terms on startup")
}
// Term Select Pre-Search POST
@@ -321,8 +323,7 @@ func main() {
// Wait for signal (indefinite)
closingSignal := <-stop
isClosing = true
isClosing = true // TODO: Switch to atomic lock with forced close after 10 seconds
// Write memory profile if requested
if *memoryProfile != "" {