Browse Source

feat(interactions): number application command option

nitroflap 2 years ago
parent
commit
11283ba0ab
2 changed files with 22 additions and 12 deletions
  1. 16 7
      examples/slash_commands/main.go
  2. 6 5
      interactions.go

+ 16 - 7
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{

+ 6 - 5
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