From 11283ba0abd4b8f79f0cf06c0124ff2d4735c82d Mon Sep 17 00:00:00 2001 From: nitroflap Date: Wed, 2 Mar 2022 22:05:22 +0300 Subject: [PATCH] feat(interactions): number application command option --- examples/slash_commands/main.go | 23 ++++++++++++++++------- interactions.go | 11 ++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/examples/slash_commands/main.go b/examples/slash_commands/main.go index 3d49477..5ac8926 100644 --- a/examples/slash_commands/main.go +++ b/examples/slash_commands/main.go @@ -65,6 +65,13 @@ var ( MaxValue: 10, Required: true, }, + { + Type: discordgo.ApplicationCommandOptionNumber, + Name: "number-option", + Description: "Float option", + MaxValue: 10.1, + Required: true, + }, { Type: discordgo.ApplicationCommandOptionBoolean, Name: "bool-option", @@ -197,24 +204,26 @@ var ( // but this is much simpler i.ApplicationCommandData().Options[0].StringValue(), i.ApplicationCommandData().Options[1].IntValue(), - i.ApplicationCommandData().Options[2].BoolValue(), + i.ApplicationCommandData().Options[2].FloatValue(), + i.ApplicationCommandData().Options[3].BoolValue(), } msgformat := ` Now you just learned how to use command options. Take a look to the value of which you've just entered: > string_option: %s > integer_option: %d + > number_option: %f > bool_option: %v ` - if len(i.ApplicationCommandData().Options) >= 4 { - margs = append(margs, i.ApplicationCommandData().Options[3].ChannelValue(nil).ID) + if len(i.ApplicationCommandData().Options) >= 5 { + margs = append(margs, i.ApplicationCommandData().Options[4].ChannelValue(nil).ID) msgformat += "> channel-option: <#%s>\n" } - if len(i.ApplicationCommandData().Options) >= 5 { - margs = append(margs, i.ApplicationCommandData().Options[4].UserValue(nil).ID) + if len(i.ApplicationCommandData().Options) >= 6 { + margs = append(margs, i.ApplicationCommandData().Options[5].UserValue(nil).ID) msgformat += "> user-option: <@%s>\n" } - if len(i.ApplicationCommandData().Options) >= 6 { - margs = append(margs, i.ApplicationCommandData().Options[5].RoleValue(nil, "").ID) + if len(i.ApplicationCommandData().Options) >= 7 { + margs = append(margs, i.ApplicationCommandData().Options[6].RoleValue(nil, "").ID) msgformat += "> role-option: <@&%s>\n" } s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ diff --git a/interactions.go b/interactions.go index b263291..0e5ae3c 100644 --- a/interactions.go +++ b/interactions.go @@ -57,6 +57,7 @@ const ( ApplicationCommandOptionChannel ApplicationCommandOptionType = 7 ApplicationCommandOptionRole ApplicationCommandOptionType = 8 ApplicationCommandOptionMentionable ApplicationCommandOptionType = 9 + ApplicationCommandOptionNumber ApplicationCommandOptionType = 10 ApplicationCommandOptionAttachment ApplicationCommandOptionType = 11 ) @@ -80,6 +81,8 @@ func (t ApplicationCommandOptionType) String() string { return "Role" case ApplicationCommandOptionMentionable: return "Mentionable" + case ApplicationCommandOptionNumber: + return "Number" case ApplicationCommandOptionAttachment: return "Attachment" } @@ -381,12 +384,10 @@ func (o ApplicationCommandInteractionDataOption) UintValue() uint64 { // FloatValue is a utility function for casting option value to float func (o ApplicationCommandInteractionDataOption) FloatValue() float64 { - // TODO: limit calls to Number type once it is released - if v, ok := o.Value.(float64); ok { - return v + if o.Type != ApplicationCommandOptionNumber { + panic("FloatValue called on data option of type " + o.Type.String()) } - - return 0.0 + return o.Value.(float64) } // StringValue is a utility function for casting option value to string