Add user command for profile
This commit is contained in:
parent
85a7a37162
commit
1df691bc13
|
@ -5,16 +5,18 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.handmade.network/hmn/hmn/src/config"
|
|
||||||
"git.handmade.network/hmn/hmn/src/db"
|
"git.handmade.network/hmn/hmn/src/db"
|
||||||
"git.handmade.network/hmn/hmn/src/hmnurl"
|
"git.handmade.network/hmn/hmn/src/hmnurl"
|
||||||
"git.handmade.network/hmn/hmn/src/logging"
|
"git.handmade.network/hmn/hmn/src/logging"
|
||||||
"git.handmade.network/hmn/hmn/src/models"
|
"git.handmade.network/hmn/hmn/src/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CommandNameProfile = "profile"
|
// Slash command names and options
|
||||||
|
const SlashCommandProfile = "profile"
|
||||||
|
const ProfileOptionUser = "user"
|
||||||
|
|
||||||
const ProfileCommandOptionUser = "user"
|
// User command names
|
||||||
|
const UserCommandProfile = "HMN Profile"
|
||||||
|
|
||||||
func (bot *botInstance) createApplicationCommands(ctx context.Context) {
|
func (bot *botInstance) createApplicationCommands(ctx context.Context) {
|
||||||
doOrWarn := func(err error) {
|
doOrWarn := func(err error) {
|
||||||
|
@ -26,17 +28,21 @@ func (bot *botInstance) createApplicationCommands(ctx context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
doOrWarn(CreateGuildApplicationCommand(ctx, CreateGuildApplicationCommandRequest{
|
doOrWarn(CreateGuildApplicationCommand(ctx, CreateGuildApplicationCommandRequest{
|
||||||
Name: CommandNameProfile,
|
Type: ApplicationCommandTypeChatInput,
|
||||||
|
Name: SlashCommandProfile,
|
||||||
Description: "Get a link to a user's Handmade Network profile",
|
Description: "Get a link to a user's Handmade Network profile",
|
||||||
Options: []ApplicationCommandOption{
|
Options: []ApplicationCommandOption{
|
||||||
{
|
{
|
||||||
Type: ApplicationCommandOptionTypeUser,
|
Type: ApplicationCommandOptionTypeUser,
|
||||||
Name: ProfileCommandOptionUser,
|
Name: ProfileOptionUser,
|
||||||
Description: "The Discord user to look up on Handmade Network",
|
Description: "The Discord user to look up on Handmade Network",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: ApplicationCommandTypeChatInput,
|
}))
|
||||||
|
doOrWarn(CreateGuildApplicationCommand(ctx, CreateGuildApplicationCommandRequest{
|
||||||
|
Type: ApplicationCommandTypeUser,
|
||||||
|
Name: UserCommandProfile,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,23 +60,25 @@ func (bot *botInstance) doInteraction(ctx context.Context, i *Interaction) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
switch i.Data.Name {
|
switch i.Data.Name {
|
||||||
case CommandNameProfile:
|
case SlashCommandProfile:
|
||||||
bot.handleProfileCommand(ctx, i)
|
userOpt := mustGetInteractionOption(i.Data.Options, ProfileOptionUser)
|
||||||
|
userID := userOpt.Value.(string)
|
||||||
|
bot.handleProfileCommand(ctx, i, userID)
|
||||||
|
case UserCommandProfile:
|
||||||
|
bot.handleProfileCommand(ctx, i, i.Data.TargetID)
|
||||||
default:
|
default:
|
||||||
logging.ExtractLogger(ctx).Warn().Str("name", i.Data.Name).Msg("didn't recognize Discord interaction name")
|
logging.ExtractLogger(ctx).Warn().Str("name", i.Data.Name).Msg("didn't recognize Discord interaction name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *botInstance) handleProfileCommand(ctx context.Context, i *Interaction) {
|
func (bot *botInstance) handleProfileCommand(ctx context.Context, i *Interaction, userID string) {
|
||||||
userOpt := mustGetInteractionOption(i.Data.Options, ProfileCommandOptionUser)
|
|
||||||
userID := userOpt.Value.(string)
|
|
||||||
member := i.Data.Resolved.Members[userID]
|
member := i.Data.Resolved.Members[userID]
|
||||||
|
|
||||||
if userID == config.Config.Discord.BotUserID {
|
if member.User.IsBot {
|
||||||
err := CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
err := CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
||||||
Type: InteractionCallbackTypeChannelMessageWithSource,
|
Type: InteractionCallbackTypeChannelMessageWithSource,
|
||||||
Data: &InteractionCallbackData{
|
Data: &InteractionCallbackData{
|
||||||
Content: "<a:confusedparrot:891814826484064336>",
|
Content: "<a:confusedparrot:865957487026765864>",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -98,7 +106,7 @@ func (bot *botInstance) handleProfileCommand(ctx context.Context, i *Interaction
|
||||||
err = CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
err = CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
||||||
Type: InteractionCallbackTypeChannelMessageWithSource,
|
Type: InteractionCallbackTypeChannelMessageWithSource,
|
||||||
Data: &InteractionCallbackData{
|
Data: &InteractionCallbackData{
|
||||||
Content: fmt.Sprintf("%s hasn't linked a Handmade Network profile.", member.DisplayName()),
|
Content: fmt.Sprintf("<@%s> hasn't linked a Handmade Network profile.", member.User.ID),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -115,7 +123,7 @@ func (bot *botInstance) handleProfileCommand(ctx context.Context, i *Interaction
|
||||||
err = CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
err = CreateInteractionResponse(ctx, i.ID, i.Token, InteractionResponse{
|
||||||
Type: InteractionCallbackTypeChannelMessageWithSource,
|
Type: InteractionCallbackTypeChannelMessageWithSource,
|
||||||
Data: &InteractionCallbackData{
|
Data: &InteractionCallbackData{
|
||||||
Content: fmt.Sprintf("%s's profile can be viewed at %s.", member.DisplayName(), url),
|
Content: fmt.Sprintf("<@%s>'s profile can be viewed at %s.", member.User.ID, url),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -712,7 +712,7 @@ type InteractionData struct {
|
||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
// Fields for User Command and Message Command
|
// Fields for User Command and Message Command
|
||||||
// TODO
|
TargetID string `json:"target_id"` // id the of user or message targetted by a user or message command
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-resolved-data-structure
|
// See https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-resolved-data-structure
|
||||||
|
@ -842,6 +842,7 @@ func InteractionDataFromMap(m interface{}, k string) *InteractionData {
|
||||||
Name: mmap["name"].(string),
|
Name: mmap["name"].(string),
|
||||||
Type: ApplicationCommandType(mmap["type"].(float64)),
|
Type: ApplicationCommandType(mmap["type"].(float64)),
|
||||||
Resolved: ResolvedDataFromMap(mmap, "resolved"),
|
Resolved: ResolvedDataFromMap(mmap, "resolved"),
|
||||||
|
TargetID: maybeString(mmap, "target_id"),
|
||||||
}
|
}
|
||||||
|
|
||||||
if ioptions, ok := mmap["options"]; ok {
|
if ioptions, ok := mmap["options"]; ok {
|
||||||
|
|
Loading…
Reference in New Issue