Module: OnyxCord::MessageComponents
- Defined in:
- lib/onyxcord/utils/message_components.rb
Overview
Helpers for Discord message component payloads.
Constant Summary collapse
- IS_COMPONENTS_V2 =
Discord message flag for the Components V2 layout system.
1 << 15
- V2_COMPONENT_TYPES =
Component types that only exist in the Components V2 message system.
[9, 10, 11, 12, 13, 14, 17].freeze
Class Method Summary collapse
- .apply_v2_flag(flags, components, force: false) ⇒ Object
- .component_v2?(component) ⇒ Boolean
- .components_v2?(components) ⇒ Boolean
- .flag_value(flags) ⇒ Object
- .payload(components) ⇒ Object
Class Method Details
.apply_v2_flag(flags, components, force: false) ⇒ Object
37 38 39 40 41 |
# File 'lib/onyxcord/utils/message_components.rb', line 37 def apply_v2_flag(flags, components, force: false) return flags unless force || components_v2?(components) flag_value(flags) | IS_COMPONENTS_V2 end |
.component_v2?(component) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/onyxcord/utils/message_components.rb', line 43 def component_v2?(component) return false if component.nil? component = component.to_h if component.respond_to?(:to_h) return false unless component.is_a?(Hash) type = component[:type] || component['type'] return true if V2_COMPONENT_TYPES.include?(type) children = component[:components] || component['components'] return true if children && components_v2?(children) accessory = component[:accessory] || component['accessory'] component_v2?(accessory) end |
.components_v2?(components) ⇒ Boolean
33 34 35 |
# File 'lib/onyxcord/utils/message_components.rb', line 33 def components_v2?(components) payload(components).any? { |component| component_v2?(component) } end |
.flag_value(flags) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/onyxcord/utils/message_components.rb', line 59 def flag_value(flags) case flags when nil, :undef 0 when Array flags.map { |flag| flag.respond_to?(:to_i) ? flag.to_i : 0 }.reduce(0, &:|) else flags.respond_to?(:to_i) ? flags.to_i : 0 end end |
.payload(components) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/onyxcord/utils/message_components.rb', line 14 def payload(components) case components when nil [] when Array components.map { |component| component.respond_to?(:to_h) ? component.to_h : component } when Hash [components] else if components.respond_to?(:to_a) components.to_a elsif components.respond_to?(:to_h) [components.to_h] else Array(components) end end end |