Skip to content

Commit

Permalink
fix(logger): component handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnyu5 committed Aug 14, 2024
1 parent 466b578 commit e2113fa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
50 changes: 24 additions & 26 deletions commands/rmp/rmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package rmp

import (
"fmt"
"log"
"time"

"github.com/bwmarrin/discordgo"
"github.com/charmbracelet/log"
"github.com/shawnyu5/debate_dragon_2.0/command"
"github.com/shawnyu5/debate_dragon_2.0/utils"
)
Expand All @@ -16,7 +16,7 @@ var rmp = command.Command{
ApplicationCommand: func() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: "rmp",
Version: "2.0.0",
Version: "2.0.1",
Description: "Get reviews from rate my prof",
Options: []*discordgo.ApplicationCommandOption{
{
Expand Down Expand Up @@ -167,8 +167,8 @@ func createSelectMenu(profs []ProfNode, disable bool) discordgo.SelectMenu {
Label: prof.fullName(),
Value: prof.Node.ID,
Description: fmt.Sprintf("Department: %s", prof.Node.Department),
Emoji: &discordgo.ComponentEmoji{},
Default: false,
// Emoji: &discordgo.ComponentEmoji{},
Default: false,
}
menu.Options = append(menu.Options, option)
}
Expand All @@ -181,33 +181,31 @@ func createSelectMenu(profs []ProfNode, disable bool) discordgo.SelectMenu {
// prof : professor information to send.
// return: error if any.
func SendProfInformation(sess *discordgo.Session, i *discordgo.InteractionCreate, prof ProfNode) error {
return sess.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
URL: prof.rmpURL(),
Type: "",
Title: fmt.Sprintf("%s %s", prof.Node.FirstName, prof.Node.LastName),
Description: prof.profDescription(),
Timestamp: "",
Color: 0,
Footer: &discordgo.MessageEmbedFooter{
Text: "Information retrieved from ratemyprof.com",
IconURL: "https://pbs.twimg.com/profile_images/1146077191043788800/hG1lAGm9_400x400.png",
ProxyIconURL: "",
},
Author: &discordgo.MessageEmbedAuthor{
URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley",
Name: fmt.Sprintf("brought to you by @%s's mom TM", i.Member.User.Username),
IconURL: "",
ProxyIconURL: "",
},
_, err := sess.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Embeds: &[]*discordgo.MessageEmbed{
{
URL: prof.rmpURL(),
Type: "",
Title: fmt.Sprintf("%s %s", prof.Node.FirstName, prof.Node.LastName),
Description: prof.profDescription(),
Timestamp: "",
Color: 0,
Footer: &discordgo.MessageEmbedFooter{
Text: "Information retrieved from ratemyprof.com",
IconURL: "https://pbs.twimg.com/profile_images/1146077191043788800/hG1lAGm9_400x400.png",
ProxyIconURL: "",
},
Author: &discordgo.MessageEmbedAuthor{
URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley",
Name: fmt.Sprintf("brought to you by @%s's mom TM", i.Member.User.Username),
IconURL: "",
ProxyIconURL: "",
},
},
},
})

return err
}

// autoCompleteHandler handles filling the auto complete results
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ func init() {
case discordgo.InteractionMessageComponent:
if handlerFunc, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
command := command.Command{
// This field is needed for `HandleInteractionApplicationCommand()`'s logging
ApplicationCommand: func() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: i.MessageComponentData().CustomID,
}
},
EditInteractionResponse: handlerFunc,
}

logger := middware.NewLogger(command)
logger.EditIteractionResponse(sess, i)
logger.HandleInteractionApplicationCommand(sess, i)
} else {
utils.SendErrorMessage(sess, i, "")
}
Expand Down
2 changes: 2 additions & 0 deletions middware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func (l Logger) HandleInteractionApplicationCommand(sess *discordgo.Session, i *
var output string
var err error
if l.Next.EditInteractionResponse != nil {
log.Debugf("Editing interaction response")
utils.DeferReply(sess, i.Interaction)
output, err = l.Next.EditInteractionResponse(sess, i)
} else if l.Next.InteractionRespond != nil {
log.Debugf("Sending interaction response")
output, err = l.Next.InteractionRespond(sess, i)
} else {
panic("No handler defined for slash command " + l.Next.ApplicationCommand().Name)
Expand Down

0 comments on commit e2113fa

Please sign in to comment.