diff --git a/helpers.go b/helpers.go index db2b850..af03f50 100644 --- a/helpers.go +++ b/helpers.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "runtime" + "sort" "strconv" "strings" "time" @@ -112,7 +113,13 @@ func Nonce() string { // DoRequest performs & logs the request, logging and returning the response func DoRequest(req *http.Request) (*http.Response, error) { - log.Debug().Str("method", strings.TrimRight(req.Method, " ")).Str("url", req.URL.String()).Str("query", req.URL.RawQuery).Str("content-type", req.Header.Get("Content-Type")).Msg("Request") + log.Debug(). + Str("method", strings.TrimRight(req.Method, " ")). + Str("url", req.URL.String()). + Str("query", req.URL.RawQuery). + Str("content-type", req.Header.Get("Content-Type")). + Msg("Request") + res, err := client.Do(req) if err != nil { @@ -324,10 +331,51 @@ func NewCalendar() *ics.Calendar { return c } -func GetUsername(interaction *discordgo.InteractionCreate) string { +// GetUser returns the user from the interaction. +// This helper method is useful as depending on where the message was sent (guild or DM), the user is in a different field. +func GetUser(interaction *discordgo.InteractionCreate) *discordgo.User { + // If the interaction is in a guild, the user is kept in the Member field if interaction.Member != nil { - return interaction.Member.User.Username + return interaction.Member.User } - return interaction.User.Username + // If the interaction is in a DM, the user is kept in the User field + return interaction.User +} + +// Encode encodes the values into “URL encoded” form +// ("bar=baz&foo=quux") sorted by key. +func EncodeParams(params map[string][]string) string { + // Escape hatch for nil + if params == nil { + return "" + } + + // Sort the keys + keys := make([]string, 0, len(params)) + for k := range params { + keys = append(keys, k) + } + sort.Strings(keys) + + var buf strings.Builder + for _, k := range keys { + // Multiple values are allowed, so extract the slice & prepare the key + values := params[k] + keyEscaped := url.QueryEscape(k) + + for _, v := range values { + // If any parameters have been written, add the ampersand + if buf.Len() > 0 { + buf.WriteByte('&') + } + + // Write the key and value + buf.WriteString(keyEscaped) + buf.WriteByte('=') + buf.WriteString(url.QueryEscape(v)) + } + } + + return buf.String() } diff --git a/main.go b/main.go index 767dbf4..f213bed 100644 --- a/main.go +++ b/main.go @@ -173,7 +173,7 @@ func main() { options.Str(option.Name, fmt.Sprintf("%v", option.Value)) } - event := log.Info().Str("name", name).Str("user", GetUsername(interaction)).Dict("options", options) + event := log.Info().Str("name", name).Str("user", GetUser(interaction).Username).Dict("options", options) // If the command was invoked in a guild, add guild & channel info to the log if interaction.Member != nil {