mirror of
https://github.com/Xevion/banner.git
synced 2025-12-08 10:06:28 -06:00
Return commands at handler level, work on TermCommand implementation/definition
This commit is contained in:
54
commands.go
54
commands.go
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
commandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition, SearchCommandDefinition}
|
commandDefinitions = []*discordgo.ApplicationCommand{TermCommandDefinition, TimeCommandDefinition, SearchCommandDefinition}
|
||||||
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) error{
|
||||||
TimeCommandDefinition.Name: TimeCommandHandler,
|
TimeCommandDefinition.Name: TimeCommandHandler,
|
||||||
TermCommandDefinition.Name: TermCommandHandler,
|
TermCommandDefinition.Name: TermCommandHandler,
|
||||||
SearchCommandDefinition.Name: SearchCommandHandler,
|
SearchCommandDefinition.Name: SearchCommandHandler,
|
||||||
@@ -45,7 +45,7 @@ var SearchCommandDefinition = &discordgo.ApplicationCommand{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) error {
|
||||||
data := interaction.ApplicationCommandData()
|
data := interaction.ApplicationCommandData()
|
||||||
query := NewQuery().Credits(3, 6)
|
query := NewQuery().Credits(3, 6)
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.Int
|
|||||||
Content: "Error searching for courses",
|
Content: "Error searching for courses",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch_time := time.Now()
|
fetch_time := time.Now()
|
||||||
@@ -103,32 +103,55 @@ func SearchCommandHandler(session *discordgo.Session, interaction *discordgo.Int
|
|||||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var TermCommandDefinition = &discordgo.ApplicationCommand{
|
var TermCommandDefinition = &discordgo.ApplicationCommand{
|
||||||
Name: "term",
|
Name: "terms",
|
||||||
Description: "Guess the current term, or search for a specific term",
|
Description: "Guess the current term, or search for a specific term",
|
||||||
Options: []*discordgo.ApplicationCommandOption{
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
{
|
{
|
||||||
Type: discordgo.ApplicationCommandOptionString,
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
MinLength: GetPointer(0),
|
MinLength: GetPointer(0),
|
||||||
MaxLength: 8,
|
MaxLength: 8,
|
||||||
Name: "term",
|
Name: "search",
|
||||||
Description: "Term to search for",
|
Description: "Term to search for",
|
||||||
Required: true,
|
Required: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TermCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
func TermCommandHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) error {
|
||||||
GetTerms("", 1, 25)
|
data := interaction.ApplicationCommandData()
|
||||||
|
|
||||||
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
|
var searchTerm string
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
if len(data.Options) == 1 {
|
||||||
Data: &discordgo.InteractionResponseData{
|
searchTerm = data.Options[0].StringValue()
|
||||||
Content: fmt.Sprintf("```json\n%s```", "{\n \"name\": \"Term\",\n \"value\": \"202420\"\n}"),
|
}
|
||||||
},
|
|
||||||
|
terms, err := GetTerms(searchTerm, 1, 25)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
RespondError(session, interaction.Interaction, "Error while fetching terms", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := []*discordgo.MessageEmbedField{}
|
||||||
|
|
||||||
|
for _, t := range terms {
|
||||||
|
fields = append(fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: "ID",
|
||||||
|
Value: t.Code,
|
||||||
|
Inline: true,
|
||||||
|
}, &discordgo.MessageEmbedField{
|
||||||
|
Name: "Description",
|
||||||
|
Value: t.Description,
|
||||||
|
Inline: true,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var TimeCommandDefinition = &discordgo.ApplicationCommand{
|
var TimeCommandDefinition = &discordgo.ApplicationCommand{
|
||||||
@@ -144,7 +167,7 @@ var TimeCommandDefinition = &discordgo.ApplicationCommand{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) error {
|
||||||
fetch_time := time.Now()
|
fetch_time := time.Now()
|
||||||
crn := i.ApplicationCommandData().Options[0].IntValue()
|
crn := i.ApplicationCommandData().Options[0].IntValue()
|
||||||
courseMeetingTime, err := GetCourseMeetingTime(202420, int(crn))
|
courseMeetingTime, err := GetCourseMeetingTime(202420, int(crn))
|
||||||
@@ -155,7 +178,7 @@ func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
Content: "Error getting meeting time",
|
Content: "Error getting meeting time",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := courseMeetingTime.timeEnd.Sub(courseMeetingTime.timeStart)
|
duration := courseMeetingTime.timeEnd.Sub(courseMeetingTime.timeStart)
|
||||||
@@ -192,4 +215,5 @@ func TimeCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
17
helpers.go
17
helpers.go
@@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
log "github.com/rs/zerolog/log"
|
log "github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
@@ -301,3 +302,19 @@ func DumpResponse(res *http.Response) {
|
|||||||
|
|
||||||
log.Info().Str("filename", filename).Str("content-type", contentType).Msg("Dumped response body")
|
log.Info().Str("filename", filename).Str("content-type", contentType).Msg("Dumped response body")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResponseError responds to an interaction with an error message
|
||||||
|
// TODO: Improve with a proper embed and colors
|
||||||
|
func RespondError(session *discordgo.Session, interaction *discordgo.Interaction, message string, err error) {
|
||||||
|
// Optional: log the error
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
session.InteractionRespond(interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: message,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
10
main.go
10
main.go
@@ -156,12 +156,18 @@ func main() {
|
|||||||
log.Info().Str("name", name).Str("user", interaction.Member.User.Username).Dict("options", options).Msg("Command Invoked")
|
log.Info().Str("name", name).Str("user", interaction.Member.User.Username).Dict("options", options).Msg("Command Invoked")
|
||||||
|
|
||||||
// Call handler
|
// Call handler
|
||||||
handler(internalSession, interaction)
|
err := handler(internalSession, interaction)
|
||||||
|
|
||||||
|
// Log error
|
||||||
|
if err != nil {
|
||||||
|
// TODO: Find a way to merge the response with the handler's error
|
||||||
|
log.Error().Str("commandName", name).Err(err).Msg("Command Handler Error")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Error().Str("commandName", name).Msg("Command Interaction Has No Handler")
|
log.Error().Str("commandName", name).Msg("Command Interaction Has No Handler")
|
||||||
|
|
||||||
// Respond with error
|
// Respond with error
|
||||||
RespondError(internalSession, interaction.Interaction, "Command not found", nil)
|
RespondError(internalSession, interaction.Interaction, "Unexpected Error: interaction has no handler", nil)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user