discorb 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/.github/workflows/build_version.yml +2 -2
 - data/.rubocop.yml +12 -75
 - data/Changelog.md +10 -0
 - data/Rakefile +482 -454
 - data/lib/discorb/allowed_mentions.rb +68 -72
 - data/lib/discorb/app_command/command.rb +466 -398
 - data/lib/discorb/app_command/common.rb +65 -25
 - data/lib/discorb/app_command/handler.rb +304 -266
 - data/lib/discorb/app_command.rb +5 -5
 - data/lib/discorb/application.rb +198 -197
 - data/lib/discorb/asset.rb +101 -101
 - data/lib/discorb/attachment.rb +134 -119
 - data/lib/discorb/audit_logs.rb +412 -385
 - data/lib/discorb/automod.rb +279 -269
 - data/lib/discorb/channel/base.rb +107 -108
 - data/lib/discorb/channel/category.rb +32 -32
 - data/lib/discorb/channel/container.rb +44 -44
 - data/lib/discorb/channel/dm.rb +26 -28
 - data/lib/discorb/channel/guild.rb +311 -246
 - data/lib/discorb/channel/stage.rb +156 -140
 - data/lib/discorb/channel/text.rb +430 -336
 - data/lib/discorb/channel/thread.rb +374 -325
 - data/lib/discorb/channel/voice.rb +85 -79
 - data/lib/discorb/channel.rb +5 -5
 - data/lib/discorb/client.rb +635 -621
 - data/lib/discorb/color.rb +178 -182
 - data/lib/discorb/common.rb +168 -164
 - data/lib/discorb/components/button.rb +107 -106
 - data/lib/discorb/components/select_menu.rb +157 -145
 - data/lib/discorb/components/text_input.rb +103 -106
 - data/lib/discorb/components.rb +68 -66
 - data/lib/discorb/dictionary.rb +135 -135
 - data/lib/discorb/embed.rb +404 -398
 - data/lib/discorb/emoji.rb +309 -302
 - data/lib/discorb/emoji_table.rb +16099 -8857
 - data/lib/discorb/error.rb +131 -131
 - data/lib/discorb/event.rb +360 -314
 - data/lib/discorb/event_handler.rb +39 -39
 - data/lib/discorb/exe/about.rb +17 -17
 - data/lib/discorb/exe/irb.rb +72 -67
 - data/lib/discorb/exe/new.rb +323 -315
 - data/lib/discorb/exe/run.rb +69 -68
 - data/lib/discorb/exe/setup.rb +57 -55
 - data/lib/discorb/exe/show.rb +12 -12
 - data/lib/discorb/extend.rb +25 -45
 - data/lib/discorb/extension.rb +89 -83
 - data/lib/discorb/flag.rb +126 -128
 - data/lib/discorb/gateway.rb +984 -804
 - data/lib/discorb/gateway_events.rb +670 -638
 - data/lib/discorb/gateway_requests.rb +45 -48
 - data/lib/discorb/guild.rb +2115 -1626
 - data/lib/discorb/guild_template.rb +280 -241
 - data/lib/discorb/http.rb +247 -232
 - data/lib/discorb/image.rb +42 -42
 - data/lib/discorb/integration.rb +169 -161
 - data/lib/discorb/intents.rb +161 -163
 - data/lib/discorb/interaction/autocomplete.rb +76 -62
 - data/lib/discorb/interaction/command.rb +279 -224
 - data/lib/discorb/interaction/components.rb +114 -104
 - data/lib/discorb/interaction/modal.rb +36 -32
 - data/lib/discorb/interaction/response.rb +379 -336
 - data/lib/discorb/interaction/root.rb +271 -257
 - data/lib/discorb/interaction.rb +5 -5
 - data/lib/discorb/invite.rb +154 -153
 - data/lib/discorb/member.rb +344 -311
 - data/lib/discorb/message.rb +615 -544
 - data/lib/discorb/message_meta.rb +197 -186
 - data/lib/discorb/modules.rb +371 -290
 - data/lib/discorb/permission.rb +305 -291
 - data/lib/discorb/presence.rb +352 -346
 - data/lib/discorb/rate_limit.rb +81 -76
 - data/lib/discorb/reaction.rb +55 -54
 - data/lib/discorb/role.rb +272 -240
 - data/lib/discorb/shard.rb +76 -74
 - data/lib/discorb/sticker.rb +193 -171
 - data/lib/discorb/user.rb +205 -188
 - data/lib/discorb/utils/colored_puts.rb +16 -16
 - data/lib/discorb/utils.rb +12 -16
 - data/lib/discorb/voice_state.rb +305 -281
 - data/lib/discorb/webhook.rb +537 -507
 - data/lib/discorb.rb +62 -56
 - data/sig/discorb/application.rbs +2 -0
 - data/sig/discorb/automod.rbs +10 -1
 - data/sig/discorb/guild.rbs +2 -0
 - data/sig/discorb/message.rbs +2 -0
 - data/sig/discorb/user.rbs +22 -20
 - metadata +2 -2
 
| 
         @@ -1,145 +1,157 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            module Discorb
         
     | 
| 
       4 
     | 
    
         
            -
              #
         
     | 
| 
       5 
     | 
    
         
            -
              # Represents a select menu component.
         
     | 
| 
       6 
     | 
    
         
            -
              #
         
     | 
| 
       7 
     | 
    
         
            -
              class SelectMenu < Component
         
     | 
| 
       8 
     | 
    
         
            -
                # @return [String] The custom ID of the select menu.
         
     | 
| 
       9 
     | 
    
         
            -
                attr_accessor :custom_id
         
     | 
| 
       10 
     | 
    
         
            -
                # @return [Array<SelectMenu::Option>] The options of the select menu.
         
     | 
| 
       11 
     | 
    
         
            -
                attr_accessor :options
         
     | 
| 
       12 
     | 
    
         
            -
                # @return [Integer] The minimum number of values.
         
     | 
| 
       13 
     | 
    
         
            -
                attr_accessor :min_values
         
     | 
| 
       14 
     | 
    
         
            -
                # @return [Integer] The maximum number of values.
         
     | 
| 
       15 
     | 
    
         
            -
                attr_accessor :max_values
         
     | 
| 
       16 
     | 
    
         
            -
                # @return [Boolean] Whether the select menu is disabled.
         
     | 
| 
       17 
     | 
    
         
            -
                attr_accessor :disabled
         
     | 
| 
       18 
     | 
    
         
            -
                alias disabled? disabled
         
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                #
         
     | 
| 
       21 
     | 
    
         
            -
                # Initialize a new select menu.
         
     | 
| 
       22 
     | 
    
         
            -
                #
         
     | 
| 
       23 
     | 
    
         
            -
                # @param [String, Symbol] custom_id Custom ID of the select menu.
         
     | 
| 
       24 
     | 
    
         
            -
                # @param [Array<Discorb::SelectMenu::Option>] options The options of the select menu.
         
     | 
| 
       25 
     | 
    
         
            -
                # @param [String] placeholder The placeholder of the select menu.
         
     | 
| 
       26 
     | 
    
         
            -
                # @param [Integer] min_values The minimum number of values.
         
     | 
| 
       27 
     | 
    
         
            -
                # @param [Integer] max_values The maximum number of values.
         
     | 
| 
       28 
     | 
    
         
            -
                #
         
     | 
| 
       29 
     | 
    
         
            -
                def initialize( 
     | 
| 
       30 
     | 
    
         
            -
                   
     | 
| 
       31 
     | 
    
         
            -
                   
     | 
| 
       32 
     | 
    
         
            -
                   
     | 
| 
       33 
     | 
    
         
            -
                   
     | 
| 
       34 
     | 
    
         
            -
                   
     | 
| 
       35 
     | 
    
         
            -
                 
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
                 
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
                #
         
     | 
| 
       44 
     | 
    
         
            -
                 
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
                     
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
                 
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                  #
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
       67 
     | 
    
         
            -
                  #
         
     | 
| 
       68 
     | 
    
         
            -
                   
     | 
| 
       69 
     | 
    
         
            -
             
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
                     
     | 
| 
       76 
     | 
    
         
            -
             
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
             
     | 
| 
       84 
     | 
    
         
            -
             
     | 
| 
       85 
     | 
    
         
            -
             
     | 
| 
       86 
     | 
    
         
            -
             
     | 
| 
       87 
     | 
    
         
            -
             
     | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
       89 
     | 
    
         
            -
                  # @return [ 
     | 
| 
       90 
     | 
    
         
            -
                  attr_accessor : 
     | 
| 
       91 
     | 
    
         
            -
                  # @return [ 
     | 
| 
       92 
     | 
    
         
            -
                  attr_accessor : 
     | 
| 
       93 
     | 
    
         
            -
             
     | 
| 
       94 
     | 
    
         
            -
                   
     | 
| 
       95 
     | 
    
         
            -
                  #  
     | 
| 
       96 
     | 
    
         
            -
                   
     | 
| 
       97 
     | 
    
         
            -
                  # @ 
     | 
| 
       98 
     | 
    
         
            -
                   
     | 
| 
       99 
     | 
    
         
            -
             
     | 
| 
       100 
     | 
    
         
            -
                  # 
     | 
| 
       101 
     | 
    
         
            -
                  #  
     | 
| 
       102 
     | 
    
         
            -
                   
     | 
| 
       103 
     | 
    
         
            -
             
     | 
| 
       104 
     | 
    
         
            -
             
     | 
| 
       105 
     | 
    
         
            -
             
     | 
| 
       106 
     | 
    
         
            -
             
     | 
| 
       107 
     | 
    
         
            -
             
     | 
| 
       108 
     | 
    
         
            -
                   
     | 
| 
       109 
     | 
    
         
            -
             
     | 
| 
       110 
     | 
    
         
            -
             
     | 
| 
       111 
     | 
    
         
            -
             
     | 
| 
       112 
     | 
    
         
            -
             
     | 
| 
       113 
     | 
    
         
            -
             
     | 
| 
       114 
     | 
    
         
            -
                   
     | 
| 
       115 
     | 
    
         
            -
             
     | 
| 
       116 
     | 
    
         
            -
                  #
         
     | 
| 
       117 
     | 
    
         
            -
                   
     | 
| 
       118 
     | 
    
         
            -
             
     | 
| 
       119 
     | 
    
         
            -
             
     | 
| 
       120 
     | 
    
         
            -
             
     | 
| 
       121 
     | 
    
         
            -
             
     | 
| 
       122 
     | 
    
         
            -
             
     | 
| 
       123 
     | 
    
         
            -
             
     | 
| 
       124 
     | 
    
         
            -
                     
     | 
| 
       125 
     | 
    
         
            -
             
     | 
| 
       126 
     | 
    
         
            -
             
     | 
| 
       127 
     | 
    
         
            -
             
     | 
| 
       128 
     | 
    
         
            -
             
     | 
| 
       129 
     | 
    
         
            -
             
     | 
| 
       130 
     | 
    
         
            -
             
     | 
| 
       131 
     | 
    
         
            -
                   
     | 
| 
       132 
     | 
    
         
            -
             
     | 
| 
       133 
     | 
    
         
            -
             
     | 
| 
       134 
     | 
    
         
            -
                    #
         
     | 
| 
       135 
     | 
    
         
            -
             
     | 
| 
       136 
     | 
    
         
            -
             
     | 
| 
       137 
     | 
    
         
            -
             
     | 
| 
       138 
     | 
    
         
            -
                    #
         
     | 
| 
       139 
     | 
    
         
            -
                     
     | 
| 
       140 
     | 
    
         
            -
             
     | 
| 
       141 
     | 
    
         
            -
                     
     | 
| 
       142 
     | 
    
         
            -
             
     | 
| 
       143 
     | 
    
         
            -
             
     | 
| 
       144 
     | 
    
         
            -
             
     | 
| 
       145 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module Discorb
         
     | 
| 
      
 4 
     | 
    
         
            +
              #
         
     | 
| 
      
 5 
     | 
    
         
            +
              # Represents a select menu component.
         
     | 
| 
      
 6 
     | 
    
         
            +
              #
         
     | 
| 
      
 7 
     | 
    
         
            +
              class SelectMenu < Component
         
     | 
| 
      
 8 
     | 
    
         
            +
                # @return [String] The custom ID of the select menu.
         
     | 
| 
      
 9 
     | 
    
         
            +
                attr_accessor :custom_id
         
     | 
| 
      
 10 
     | 
    
         
            +
                # @return [Array<SelectMenu::Option>] The options of the select menu.
         
     | 
| 
      
 11 
     | 
    
         
            +
                attr_accessor :options
         
     | 
| 
      
 12 
     | 
    
         
            +
                # @return [Integer] The minimum number of values.
         
     | 
| 
      
 13 
     | 
    
         
            +
                attr_accessor :min_values
         
     | 
| 
      
 14 
     | 
    
         
            +
                # @return [Integer] The maximum number of values.
         
     | 
| 
      
 15 
     | 
    
         
            +
                attr_accessor :max_values
         
     | 
| 
      
 16 
     | 
    
         
            +
                # @return [Boolean] Whether the select menu is disabled.
         
     | 
| 
      
 17 
     | 
    
         
            +
                attr_accessor :disabled
         
     | 
| 
      
 18 
     | 
    
         
            +
                alias disabled? disabled
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                #
         
     | 
| 
      
 21 
     | 
    
         
            +
                # Initialize a new select menu.
         
     | 
| 
      
 22 
     | 
    
         
            +
                #
         
     | 
| 
      
 23 
     | 
    
         
            +
                # @param [String, Symbol] custom_id Custom ID of the select menu.
         
     | 
| 
      
 24 
     | 
    
         
            +
                # @param [Array<Discorb::SelectMenu::Option>] options The options of the select menu.
         
     | 
| 
      
 25 
     | 
    
         
            +
                # @param [String] placeholder The placeholder of the select menu.
         
     | 
| 
      
 26 
     | 
    
         
            +
                # @param [Integer] min_values The minimum number of values.
         
     | 
| 
      
 27 
     | 
    
         
            +
                # @param [Integer] max_values The maximum number of values.
         
     | 
| 
      
 28 
     | 
    
         
            +
                #
         
     | 
| 
      
 29 
     | 
    
         
            +
                def initialize(
         
     | 
| 
      
 30 
     | 
    
         
            +
                  custom_id,
         
     | 
| 
      
 31 
     | 
    
         
            +
                  options,
         
     | 
| 
      
 32 
     | 
    
         
            +
                  placeholder: nil,
         
     | 
| 
      
 33 
     | 
    
         
            +
                  min_values: 1,
         
     | 
| 
      
 34 
     | 
    
         
            +
                  max_values: 1
         
     | 
| 
      
 35 
     | 
    
         
            +
                )
         
     | 
| 
      
 36 
     | 
    
         
            +
                  @custom_id = custom_id
         
     | 
| 
      
 37 
     | 
    
         
            +
                  @options = options
         
     | 
| 
      
 38 
     | 
    
         
            +
                  @placeholder = placeholder
         
     | 
| 
      
 39 
     | 
    
         
            +
                  @min_values = min_values
         
     | 
| 
      
 40 
     | 
    
         
            +
                  @max_values = max_values
         
     | 
| 
      
 41 
     | 
    
         
            +
                end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                #
         
     | 
| 
      
 44 
     | 
    
         
            +
                # Converts the select menu to a hash.
         
     | 
| 
      
 45 
     | 
    
         
            +
                #
         
     | 
| 
      
 46 
     | 
    
         
            +
                # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure
         
     | 
| 
      
 47 
     | 
    
         
            +
                #   Official Discord API docs
         
     | 
| 
      
 48 
     | 
    
         
            +
                # @return [Hash] A hash representation of the select menu.
         
     | 
| 
      
 49 
     | 
    
         
            +
                #
         
     | 
| 
      
 50 
     | 
    
         
            +
                def to_hash
         
     | 
| 
      
 51 
     | 
    
         
            +
                  {
         
     | 
| 
      
 52 
     | 
    
         
            +
                    type: 3,
         
     | 
| 
      
 53 
     | 
    
         
            +
                    custom_id: @custom_id,
         
     | 
| 
      
 54 
     | 
    
         
            +
                    options: @options.map(&:to_hash),
         
     | 
| 
      
 55 
     | 
    
         
            +
                    placeholder: @placeholder,
         
     | 
| 
      
 56 
     | 
    
         
            +
                    min_values: @min_values,
         
     | 
| 
      
 57 
     | 
    
         
            +
                    max_values: @max_values,
         
     | 
| 
      
 58 
     | 
    
         
            +
                    disabled: @disabled
         
     | 
| 
      
 59 
     | 
    
         
            +
                  }
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                def inspect
         
     | 
| 
      
 63 
     | 
    
         
            +
                  "#<#{self.class}: #{@custom_id}>"
         
     | 
| 
      
 64 
     | 
    
         
            +
                end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 67 
     | 
    
         
            +
                  #
         
     | 
| 
      
 68 
     | 
    
         
            +
                  # Creates a new select menu from a hash.
         
     | 
| 
      
 69 
     | 
    
         
            +
                  #
         
     | 
| 
      
 70 
     | 
    
         
            +
                  # @param [Hash] data The hash to create the select menu from.
         
     | 
| 
      
 71 
     | 
    
         
            +
                  #
         
     | 
| 
      
 72 
     | 
    
         
            +
                  # @return [Discorb::SelectMenu] The created select menu.
         
     | 
| 
      
 73 
     | 
    
         
            +
                  #
         
     | 
| 
      
 74 
     | 
    
         
            +
                  def from_hash(data)
         
     | 
| 
      
 75 
     | 
    
         
            +
                    new(
         
     | 
| 
      
 76 
     | 
    
         
            +
                      data[:custom_id],
         
     | 
| 
      
 77 
     | 
    
         
            +
                      data[:options].map { |o| SelectMenu::Option.from_hash(o) },
         
     | 
| 
      
 78 
     | 
    
         
            +
                      placeholder: data[:placeholder],
         
     | 
| 
      
 79 
     | 
    
         
            +
                      min_values: data[:min_values],
         
     | 
| 
      
 80 
     | 
    
         
            +
                      max_values: data[:max_values]
         
     | 
| 
      
 81 
     | 
    
         
            +
                    )
         
     | 
| 
      
 82 
     | 
    
         
            +
                  end
         
     | 
| 
      
 83 
     | 
    
         
            +
                end
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                #
         
     | 
| 
      
 86 
     | 
    
         
            +
                # Represents an option of a select menu.
         
     | 
| 
      
 87 
     | 
    
         
            +
                #
         
     | 
| 
      
 88 
     | 
    
         
            +
                class Option
         
     | 
| 
      
 89 
     | 
    
         
            +
                  # @return [String] The label of the option.
         
     | 
| 
      
 90 
     | 
    
         
            +
                  attr_accessor :label
         
     | 
| 
      
 91 
     | 
    
         
            +
                  # @return [String] The value of the option.
         
     | 
| 
      
 92 
     | 
    
         
            +
                  attr_accessor :value
         
     | 
| 
      
 93 
     | 
    
         
            +
                  # @return [String] The description of the option.
         
     | 
| 
      
 94 
     | 
    
         
            +
                  attr_accessor :description
         
     | 
| 
      
 95 
     | 
    
         
            +
                  # @return [Discorb::Emoji] The emoji of the option.
         
     | 
| 
      
 96 
     | 
    
         
            +
                  attr_accessor :emoji
         
     | 
| 
      
 97 
     | 
    
         
            +
                  # @return [Boolean] Whether the option is default.
         
     | 
| 
      
 98 
     | 
    
         
            +
                  attr_accessor :default
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                  #
         
     | 
| 
      
 101 
     | 
    
         
            +
                  # Initialize a new option.
         
     | 
| 
      
 102 
     | 
    
         
            +
                  #
         
     | 
| 
      
 103 
     | 
    
         
            +
                  # @param [String] label The label of the option.
         
     | 
| 
      
 104 
     | 
    
         
            +
                  # @param [String] value The value of the option.
         
     | 
| 
      
 105 
     | 
    
         
            +
                  # @param [String] description The description of the option.
         
     | 
| 
      
 106 
     | 
    
         
            +
                  # @param [Discorb::Emoji] emoji The emoji of the option.
         
     | 
| 
      
 107 
     | 
    
         
            +
                  # @param [Boolean] default Whether the option is default.
         
     | 
| 
      
 108 
     | 
    
         
            +
                  def initialize(label, value, description: nil, emoji: nil, default: false)
         
     | 
| 
      
 109 
     | 
    
         
            +
                    @label = label
         
     | 
| 
      
 110 
     | 
    
         
            +
                    @value = value
         
     | 
| 
      
 111 
     | 
    
         
            +
                    @description = description
         
     | 
| 
      
 112 
     | 
    
         
            +
                    @emoji = emoji
         
     | 
| 
      
 113 
     | 
    
         
            +
                    @default = default
         
     | 
| 
      
 114 
     | 
    
         
            +
                  end
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                  #
         
     | 
| 
      
 117 
     | 
    
         
            +
                  # Converts the option to a hash.
         
     | 
| 
      
 118 
     | 
    
         
            +
                  #
         
     | 
| 
      
 119 
     | 
    
         
            +
                  # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
         
     | 
| 
      
 120 
     | 
    
         
            +
                  #   Official Discord API docs
         
     | 
| 
      
 121 
     | 
    
         
            +
                  # @return [Hash] Hash representation of the option.
         
     | 
| 
      
 122 
     | 
    
         
            +
                  #
         
     | 
| 
      
 123 
     | 
    
         
            +
                  def to_hash
         
     | 
| 
      
 124 
     | 
    
         
            +
                    {
         
     | 
| 
      
 125 
     | 
    
         
            +
                      label: @label,
         
     | 
| 
      
 126 
     | 
    
         
            +
                      value: @value,
         
     | 
| 
      
 127 
     | 
    
         
            +
                      description: @description,
         
     | 
| 
      
 128 
     | 
    
         
            +
                      emoji: @emoji&.to_hash,
         
     | 
| 
      
 129 
     | 
    
         
            +
                      default: @default
         
     | 
| 
      
 130 
     | 
    
         
            +
                    }
         
     | 
| 
      
 131 
     | 
    
         
            +
                  end
         
     | 
| 
      
 132 
     | 
    
         
            +
             
     | 
| 
      
 133 
     | 
    
         
            +
                  def inspect
         
     | 
| 
      
 134 
     | 
    
         
            +
                    "#<#{self.class} #{@label}: #{@value}>"
         
     | 
| 
      
 135 
     | 
    
         
            +
                  end
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
| 
      
 137 
     | 
    
         
            +
                  class << self
         
     | 
| 
      
 138 
     | 
    
         
            +
                    #
         
     | 
| 
      
 139 
     | 
    
         
            +
                    # Creates a new option from a hash.
         
     | 
| 
      
 140 
     | 
    
         
            +
                    #
         
     | 
| 
      
 141 
     | 
    
         
            +
                    # @param [Hash] data A hash representing the option.
         
     | 
| 
      
 142 
     | 
    
         
            +
                    #
         
     | 
| 
      
 143 
     | 
    
         
            +
                    # @return [Discorb::SelectMenu::Option] A new option.
         
     | 
| 
      
 144 
     | 
    
         
            +
                    #
         
     | 
| 
      
 145 
     | 
    
         
            +
                    def from_hash(data)
         
     | 
| 
      
 146 
     | 
    
         
            +
                      new(
         
     | 
| 
      
 147 
     | 
    
         
            +
                        data[:label],
         
     | 
| 
      
 148 
     | 
    
         
            +
                        data[:value],
         
     | 
| 
      
 149 
     | 
    
         
            +
                        description: data[:description],
         
     | 
| 
      
 150 
     | 
    
         
            +
                        emoji: data[:emoji],
         
     | 
| 
      
 151 
     | 
    
         
            +
                        default: data[:default]
         
     | 
| 
      
 152 
     | 
    
         
            +
                      )
         
     | 
| 
      
 153 
     | 
    
         
            +
                    end
         
     | 
| 
      
 154 
     | 
    
         
            +
                  end
         
     | 
| 
      
 155 
     | 
    
         
            +
                end
         
     | 
| 
      
 156 
     | 
    
         
            +
              end
         
     | 
| 
      
 157 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,106 +1,103 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            module Discorb
         
     | 
| 
       4 
     | 
    
         
            -
              #
         
     | 
| 
       5 
     | 
    
         
            -
              # Represents a text input component.
         
     | 
| 
       6 
     | 
    
         
            -
              #
         
     | 
| 
       7 
     | 
    
         
            -
              class TextInput < Component
         
     | 
| 
       8 
     | 
    
         
            -
                # @private
         
     | 
| 
       9 
     | 
    
         
            -
                STYLES = {
         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
                 
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                 
     | 
| 
       16 
     | 
    
         
            -
                 
     | 
| 
       17 
     | 
    
         
            -
                 
     | 
| 
       18 
     | 
    
         
            -
                 
     | 
| 
       19 
     | 
    
         
            -
                 
     | 
| 
       20 
     | 
    
         
            -
                 
     | 
| 
       21 
     | 
    
         
            -
                 
     | 
| 
       22 
     | 
    
         
            -
                 
     | 
| 
       23 
     | 
    
         
            -
                 
     | 
| 
       24 
     | 
    
         
            -
                 
     | 
| 
       25 
     | 
    
         
            -
                 
     | 
| 
       26 
     | 
    
         
            -
                 
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
                # 
     | 
| 
       29 
     | 
    
         
            -
                 
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
                #
         
     | 
| 
       32 
     | 
    
         
            -
                #  
     | 
| 
       33 
     | 
    
         
            -
                #
         
     | 
| 
       34 
     | 
    
         
            -
                # @param [ 
     | 
| 
       35 
     | 
    
         
            -
                # @param [ 
     | 
| 
       36 
     | 
    
         
            -
                # @param [ 
     | 
| 
       37 
     | 
    
         
            -
                # @param [ 
     | 
| 
       38 
     | 
    
         
            -
                # @param [ 
     | 
| 
       39 
     | 
    
         
            -
                # 
     | 
| 
       40 
     | 
    
         
            -
                 
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
                   
     | 
| 
       45 
     | 
    
         
            -
                   
     | 
| 
       46 
     | 
    
         
            -
                   
     | 
| 
       47 
     | 
    
         
            -
                   
     | 
| 
       48 
     | 
    
         
            -
                   
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
                   
     | 
| 
       51 
     | 
    
         
            -
                   
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
                  @ 
     | 
| 
       54 
     | 
    
         
            -
                  @ 
     | 
| 
       55 
     | 
    
         
            -
                  @ 
     | 
| 
       56 
     | 
    
         
            -
                  @ 
     | 
| 
       57 
     | 
    
         
            -
                  @ 
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
                 
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                #
         
     | 
| 
       64 
     | 
    
         
            -
                #  
     | 
| 
       65 
     | 
    
         
            -
                #
         
     | 
| 
       66 
     | 
    
         
            -
                # 
     | 
| 
       67 
     | 
    
         
            -
                 
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
       69 
     | 
    
         
            -
             
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
                     
     | 
| 
       73 
     | 
    
         
            -
                     
     | 
| 
       74 
     | 
    
         
            -
                     
     | 
| 
       75 
     | 
    
         
            -
                     
     | 
| 
       76 
     | 
    
         
            -
                     
     | 
| 
       77 
     | 
    
         
            -
                     
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
             
     | 
| 
       84 
     | 
    
         
            -
             
     | 
| 
       85 
     | 
    
         
            -
                  #
         
     | 
| 
       86 
     | 
    
         
            -
                  # 
     | 
| 
       87 
     | 
    
         
            -
                  #
         
     | 
| 
       88 
     | 
    
         
            -
                  # 
     | 
| 
       89 
     | 
    
         
            -
                   
     | 
| 
       90 
     | 
    
         
            -
             
     | 
| 
       91 
     | 
    
         
            -
             
     | 
| 
       92 
     | 
    
         
            -
             
     | 
| 
       93 
     | 
    
         
            -
             
     | 
| 
       94 
     | 
    
         
            -
                      data[: 
     | 
| 
       95 
     | 
    
         
            -
                      data[: 
     | 
| 
       96 
     | 
    
         
            -
                       
     | 
| 
       97 
     | 
    
         
            -
                       
     | 
| 
       98 
     | 
    
         
            -
                       
     | 
| 
       99 
     | 
    
         
            -
             
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
       101 
     | 
    
         
            -
             
     | 
| 
       102 
     | 
    
         
            -
             
     | 
| 
       103 
     | 
    
         
            -
             
     | 
| 
       104 
     | 
    
         
            -
                end
         
     | 
| 
       105 
     | 
    
         
            -
              end
         
     | 
| 
       106 
     | 
    
         
            -
            end
         
     | 
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module Discorb
         
     | 
| 
      
 4 
     | 
    
         
            +
              #
         
     | 
| 
      
 5 
     | 
    
         
            +
              # Represents a text input component.
         
     | 
| 
      
 6 
     | 
    
         
            +
              #
         
     | 
| 
      
 7 
     | 
    
         
            +
              class TextInput < Component
         
     | 
| 
      
 8 
     | 
    
         
            +
                # @private
         
     | 
| 
      
 9 
     | 
    
         
            +
                STYLES = { short: 1, paragraph: 2 }.freeze
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                # @return [String] The label of the text input.
         
     | 
| 
      
 12 
     | 
    
         
            +
                attr_accessor :label
         
     | 
| 
      
 13 
     | 
    
         
            +
                # @return [String] The custom id of the text input.
         
     | 
| 
      
 14 
     | 
    
         
            +
                attr_accessor :custom_id
         
     | 
| 
      
 15 
     | 
    
         
            +
                # @return [:short, :paragraph] The style of the text input.
         
     | 
| 
      
 16 
     | 
    
         
            +
                attr_accessor :style
         
     | 
| 
      
 17 
     | 
    
         
            +
                # @return [Integer, nil] The minimum length of the text input.
         
     | 
| 
      
 18 
     | 
    
         
            +
                attr_accessor :min_length
         
     | 
| 
      
 19 
     | 
    
         
            +
                # @return [Integer, nil] The maximum length of the text input.
         
     | 
| 
      
 20 
     | 
    
         
            +
                attr_accessor :max_length
         
     | 
| 
      
 21 
     | 
    
         
            +
                # @return [Boolean] Whether the text input is required.
         
     | 
| 
      
 22 
     | 
    
         
            +
                attr_accessor :required
         
     | 
| 
      
 23 
     | 
    
         
            +
                # @return [String, nil] The prefilled value of the text input.
         
     | 
| 
      
 24 
     | 
    
         
            +
                attr_accessor :value
         
     | 
| 
      
 25 
     | 
    
         
            +
                # @return [String, nil] The placeholder of the text input.
         
     | 
| 
      
 26 
     | 
    
         
            +
                attr_accessor :placeholder
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                #
         
     | 
| 
      
 29 
     | 
    
         
            +
                # Initialize a new text input component.
         
     | 
| 
      
 30 
     | 
    
         
            +
                #
         
     | 
| 
      
 31 
     | 
    
         
            +
                # @param [String] label The label of the text input.
         
     | 
| 
      
 32 
     | 
    
         
            +
                # @param [String] custom_id The custom id of the text input.
         
     | 
| 
      
 33 
     | 
    
         
            +
                # @param [:short, :paragraph] style The style of the text input.
         
     | 
| 
      
 34 
     | 
    
         
            +
                # @param [Integer, nil] min_length The minimum length of the text input.
         
     | 
| 
      
 35 
     | 
    
         
            +
                # @param [Integer, nil] max_length The maximum length of the text input.
         
     | 
| 
      
 36 
     | 
    
         
            +
                # @param [Boolean] required Whether the text input is required.
         
     | 
| 
      
 37 
     | 
    
         
            +
                # @param [String, nil] value The prefilled value of the text input.
         
     | 
| 
      
 38 
     | 
    
         
            +
                # @param [String, nil] placeholder The placeholder of the text input.
         
     | 
| 
      
 39 
     | 
    
         
            +
                #
         
     | 
| 
      
 40 
     | 
    
         
            +
                def initialize(
         
     | 
| 
      
 41 
     | 
    
         
            +
                  label,
         
     | 
| 
      
 42 
     | 
    
         
            +
                  custom_id,
         
     | 
| 
      
 43 
     | 
    
         
            +
                  style,
         
     | 
| 
      
 44 
     | 
    
         
            +
                  min_length: nil,
         
     | 
| 
      
 45 
     | 
    
         
            +
                  max_length: nil,
         
     | 
| 
      
 46 
     | 
    
         
            +
                  required: false,
         
     | 
| 
      
 47 
     | 
    
         
            +
                  value: nil,
         
     | 
| 
      
 48 
     | 
    
         
            +
                  placeholder: nil
         
     | 
| 
      
 49 
     | 
    
         
            +
                )
         
     | 
| 
      
 50 
     | 
    
         
            +
                  @label = label
         
     | 
| 
      
 51 
     | 
    
         
            +
                  @custom_id = custom_id
         
     | 
| 
      
 52 
     | 
    
         
            +
                  @style = style
         
     | 
| 
      
 53 
     | 
    
         
            +
                  @min_length = min_length
         
     | 
| 
      
 54 
     | 
    
         
            +
                  @max_length = max_length
         
     | 
| 
      
 55 
     | 
    
         
            +
                  @required = required
         
     | 
| 
      
 56 
     | 
    
         
            +
                  @value = value
         
     | 
| 
      
 57 
     | 
    
         
            +
                  @placeholder = placeholder
         
     | 
| 
      
 58 
     | 
    
         
            +
                end
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                #
         
     | 
| 
      
 61 
     | 
    
         
            +
                # Converts the select menu to a hash.
         
     | 
| 
      
 62 
     | 
    
         
            +
                #
         
     | 
| 
      
 63 
     | 
    
         
            +
                # @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
         
     | 
| 
      
 64 
     | 
    
         
            +
                #   Official Discord API docs
         
     | 
| 
      
 65 
     | 
    
         
            +
                # @return [Hash] A hash representation of the text input.
         
     | 
| 
      
 66 
     | 
    
         
            +
                #
         
     | 
| 
      
 67 
     | 
    
         
            +
                def to_hash
         
     | 
| 
      
 68 
     | 
    
         
            +
                  {
         
     | 
| 
      
 69 
     | 
    
         
            +
                    type: 4,
         
     | 
| 
      
 70 
     | 
    
         
            +
                    label: @label,
         
     | 
| 
      
 71 
     | 
    
         
            +
                    style: STYLES[@style],
         
     | 
| 
      
 72 
     | 
    
         
            +
                    custom_id: @custom_id,
         
     | 
| 
      
 73 
     | 
    
         
            +
                    min_length: @min_length,
         
     | 
| 
      
 74 
     | 
    
         
            +
                    max_length: @max_length,
         
     | 
| 
      
 75 
     | 
    
         
            +
                    required: @required,
         
     | 
| 
      
 76 
     | 
    
         
            +
                    value: @value,
         
     | 
| 
      
 77 
     | 
    
         
            +
                    placeholder: @placeholder
         
     | 
| 
      
 78 
     | 
    
         
            +
                  }
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 82 
     | 
    
         
            +
                  #
         
     | 
| 
      
 83 
     | 
    
         
            +
                  # Creates a new text input from a hash.
         
     | 
| 
      
 84 
     | 
    
         
            +
                  #
         
     | 
| 
      
 85 
     | 
    
         
            +
                  # @param [Hash] data The hash to create the text input from.
         
     | 
| 
      
 86 
     | 
    
         
            +
                  #
         
     | 
| 
      
 87 
     | 
    
         
            +
                  # @return [Discorb::TextInput] The created text input.
         
     | 
| 
      
 88 
     | 
    
         
            +
                  #
         
     | 
| 
      
 89 
     | 
    
         
            +
                  def from_hash(data)
         
     | 
| 
      
 90 
     | 
    
         
            +
                    new(
         
     | 
| 
      
 91 
     | 
    
         
            +
                      data[:label],
         
     | 
| 
      
 92 
     | 
    
         
            +
                      data[:custom_id],
         
     | 
| 
      
 93 
     | 
    
         
            +
                      STYLES.key(data[:style]),
         
     | 
| 
      
 94 
     | 
    
         
            +
                      min_length: data[:min_length],
         
     | 
| 
      
 95 
     | 
    
         
            +
                      max_length: data[:max_length],
         
     | 
| 
      
 96 
     | 
    
         
            +
                      required: data[:required],
         
     | 
| 
      
 97 
     | 
    
         
            +
                      value: data[:value],
         
     | 
| 
      
 98 
     | 
    
         
            +
                      placeholder: data[:placeholder]
         
     | 
| 
      
 99 
     | 
    
         
            +
                    )
         
     | 
| 
      
 100 
     | 
    
         
            +
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
                end
         
     | 
| 
      
 102 
     | 
    
         
            +
              end
         
     | 
| 
      
 103 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/discorb/components.rb
    CHANGED
    
    | 
         @@ -1,66 +1,68 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            module Discorb
         
     | 
| 
       4 
     | 
    
         
            -
              #
         
     | 
| 
       5 
     | 
    
         
            -
              # @abstract
         
     | 
| 
       6 
     | 
    
         
            -
              # Represents a Discord component.
         
     | 
| 
       7 
     | 
    
         
            -
              #
         
     | 
| 
       8 
     | 
    
         
            -
              class Component
         
     | 
| 
       9 
     | 
    
         
            -
                def inspect
         
     | 
| 
       10 
     | 
    
         
            -
                  "#<#{self.class}>"
         
     | 
| 
       11 
     | 
    
         
            -
                end
         
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
                class << self
         
     | 
| 
       14 
     | 
    
         
            -
                  #
         
     | 
| 
       15 
     | 
    
         
            -
                  # Create a new component from hash data.
         
     | 
| 
       16 
     | 
    
         
            -
                  #
         
     | 
| 
       17 
     | 
    
         
            -
                  # @see https://discord.com/developers/docs/interactions/message-components Official Discord API documentation
         
     | 
| 
       18 
     | 
    
         
            -
                  # @param [Hash] data Hash data.
         
     | 
| 
       19 
     | 
    
         
            -
                  #
         
     | 
| 
       20 
     | 
    
         
            -
                  # @return [Component] A new component.
         
     | 
| 
       21 
     | 
    
         
            -
                  #
         
     | 
| 
       22 
     | 
    
         
            -
                  def from_hash(data)
         
     | 
| 
       23 
     | 
    
         
            -
                    case data[:type]
         
     | 
| 
       24 
     | 
    
         
            -
                    when 2
         
     | 
| 
       25 
     | 
    
         
            -
                      Button
         
     | 
| 
       26 
     | 
    
         
            -
                    when 3
         
     | 
| 
       27 
     | 
    
         
            -
                      SelectMenu
         
     | 
| 
       28 
     | 
    
         
            -
                    when 4
         
     | 
| 
       29 
     | 
    
         
            -
                      TextInput
         
     | 
| 
       30 
     | 
    
         
            -
                    end.from_hash(data)
         
     | 
| 
       31 
     | 
    
         
            -
                  end
         
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
                  #
         
     | 
| 
       34 
     | 
    
         
            -
                  # Convert components to a hash.
         
     | 
| 
       35 
     | 
    
         
            -
                  #
         
     | 
| 
       36 
     | 
    
         
            -
                  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components Components.
         
     | 
| 
       37 
     | 
    
         
            -
                  #
         
     | 
| 
       38 
     | 
    
         
            -
                  # @return [Array<Hash>] Hash data.
         
     | 
| 
       39 
     | 
    
         
            -
                  #
         
     | 
| 
       40 
     | 
    
         
            -
                  def to_payload(components)
         
     | 
| 
       41 
     | 
    
         
            -
                    tmp_components = []
         
     | 
| 
       42 
     | 
    
         
            -
                    tmp_row = []
         
     | 
| 
       43 
     | 
    
         
            -
                    components.each do |c|
         
     | 
| 
       44 
     | 
    
         
            -
                      case c
         
     | 
| 
       45 
     | 
    
         
            -
                      when Array
         
     | 
| 
       46 
     | 
    
         
            -
                        tmp_components << tmp_row
         
     | 
| 
       47 
     | 
    
         
            -
                        tmp_row = []
         
     | 
| 
       48 
     | 
    
         
            -
                        tmp_components << c
         
     | 
| 
       49 
     | 
    
         
            -
                      when SelectMenu, TextInput
         
     | 
| 
       50 
     | 
    
         
            -
                        tmp_components << tmp_row
         
     | 
| 
       51 
     | 
    
         
            -
                        tmp_row = []
         
     | 
| 
       52 
     | 
    
         
            -
                        tmp_components << [c]
         
     | 
| 
       53 
     | 
    
         
            -
                      else
         
     | 
| 
       54 
     | 
    
         
            -
                        tmp_row << c
         
     | 
| 
       55 
     | 
    
         
            -
                      end
         
     | 
| 
       56 
     | 
    
         
            -
                    end
         
     | 
| 
       57 
     | 
    
         
            -
                    tmp_components << tmp_row
         
     | 
| 
       58 
     | 
    
         
            -
                    tmp_components 
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
            end
         
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module Discorb
         
     | 
| 
      
 4 
     | 
    
         
            +
              #
         
     | 
| 
      
 5 
     | 
    
         
            +
              # @abstract
         
     | 
| 
      
 6 
     | 
    
         
            +
              # Represents a Discord component.
         
     | 
| 
      
 7 
     | 
    
         
            +
              #
         
     | 
| 
      
 8 
     | 
    
         
            +
              class Component
         
     | 
| 
      
 9 
     | 
    
         
            +
                def inspect
         
     | 
| 
      
 10 
     | 
    
         
            +
                  "#<#{self.class}>"
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 14 
     | 
    
         
            +
                  #
         
     | 
| 
      
 15 
     | 
    
         
            +
                  # Create a new component from hash data.
         
     | 
| 
      
 16 
     | 
    
         
            +
                  #
         
     | 
| 
      
 17 
     | 
    
         
            +
                  # @see https://discord.com/developers/docs/interactions/message-components Official Discord API documentation
         
     | 
| 
      
 18 
     | 
    
         
            +
                  # @param [Hash] data Hash data.
         
     | 
| 
      
 19 
     | 
    
         
            +
                  #
         
     | 
| 
      
 20 
     | 
    
         
            +
                  # @return [Component] A new component.
         
     | 
| 
      
 21 
     | 
    
         
            +
                  #
         
     | 
| 
      
 22 
     | 
    
         
            +
                  def from_hash(data)
         
     | 
| 
      
 23 
     | 
    
         
            +
                    case data[:type]
         
     | 
| 
      
 24 
     | 
    
         
            +
                    when 2
         
     | 
| 
      
 25 
     | 
    
         
            +
                      Button
         
     | 
| 
      
 26 
     | 
    
         
            +
                    when 3
         
     | 
| 
      
 27 
     | 
    
         
            +
                      SelectMenu
         
     | 
| 
      
 28 
     | 
    
         
            +
                    when 4
         
     | 
| 
      
 29 
     | 
    
         
            +
                      TextInput
         
     | 
| 
      
 30 
     | 
    
         
            +
                    end.from_hash(data)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  #
         
     | 
| 
      
 34 
     | 
    
         
            +
                  # Convert components to a hash.
         
     | 
| 
      
 35 
     | 
    
         
            +
                  #
         
     | 
| 
      
 36 
     | 
    
         
            +
                  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components Components.
         
     | 
| 
      
 37 
     | 
    
         
            +
                  #
         
     | 
| 
      
 38 
     | 
    
         
            +
                  # @return [Array<Hash>] Hash data.
         
     | 
| 
      
 39 
     | 
    
         
            +
                  #
         
     | 
| 
      
 40 
     | 
    
         
            +
                  def to_payload(components)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    tmp_components = []
         
     | 
| 
      
 42 
     | 
    
         
            +
                    tmp_row = []
         
     | 
| 
      
 43 
     | 
    
         
            +
                    components.each do |c|
         
     | 
| 
      
 44 
     | 
    
         
            +
                      case c
         
     | 
| 
      
 45 
     | 
    
         
            +
                      when Array
         
     | 
| 
      
 46 
     | 
    
         
            +
                        tmp_components << tmp_row
         
     | 
| 
      
 47 
     | 
    
         
            +
                        tmp_row = []
         
     | 
| 
      
 48 
     | 
    
         
            +
                        tmp_components << c
         
     | 
| 
      
 49 
     | 
    
         
            +
                      when SelectMenu, TextInput
         
     | 
| 
      
 50 
     | 
    
         
            +
                        tmp_components << tmp_row
         
     | 
| 
      
 51 
     | 
    
         
            +
                        tmp_row = []
         
     | 
| 
      
 52 
     | 
    
         
            +
                        tmp_components << [c]
         
     | 
| 
      
 53 
     | 
    
         
            +
                      else
         
     | 
| 
      
 54 
     | 
    
         
            +
                        tmp_row << c
         
     | 
| 
      
 55 
     | 
    
         
            +
                      end
         
     | 
| 
      
 56 
     | 
    
         
            +
                    end
         
     | 
| 
      
 57 
     | 
    
         
            +
                    tmp_components << tmp_row
         
     | 
| 
      
 58 
     | 
    
         
            +
                    tmp_components
         
     | 
| 
      
 59 
     | 
    
         
            +
                      .filter { |c| c.length.positive? }
         
     | 
| 
      
 60 
     | 
    
         
            +
                      .map { |c| { type: 1, components: c.map(&:to_hash) } }
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
            end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
            %w[button text_input select_menu].each do |name|
         
     | 
| 
      
 67 
     | 
    
         
            +
              require_relative "components/#{name}"
         
     | 
| 
      
 68 
     | 
    
         
            +
            end
         
     |