discorb 0.13.1 → 0.14.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/.gitattributes +2 -0
- data/.github/workflows/build_version.yml +3 -3
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/lint-push.yml +18 -0
- data/.github/workflows/lint.yml +16 -0
- data/.rubocop.yml +70 -0
- data/CODE_OF_CONDUCT.md +128 -0
- data/Changelog.md +33 -0
- data/Gemfile +7 -3
- data/README.md +1 -1
- data/Rakefile +35 -35
- data/discorb.gemspec +13 -1
- data/examples/commands/bookmarker.rb +2 -1
- data/examples/commands/hello.rb +1 -0
- data/examples/commands/inspect.rb +3 -2
- data/examples/components/authorization_button.rb +2 -1
- data/examples/components/select_menu.rb +2 -1
- data/examples/extension/main.rb +1 -0
- data/examples/extension/message_expander.rb +1 -0
- data/examples/simple/eval.rb +3 -2
- data/examples/simple/ping_pong.rb +1 -0
- data/examples/simple/rolepanel.rb +1 -0
- data/examples/simple/wait_for_message.rb +4 -3
- data/exe/discorb +8 -7
- data/lib/discorb/allowed_mentions.rb +64 -0
- data/lib/discorb/app_command/command.rb +274 -0
- data/lib/discorb/app_command/handler.rb +168 -0
- data/lib/discorb/app_command.rb +2 -404
- data/lib/discorb/asset.rb +3 -1
- data/lib/discorb/audit_logs.rb +3 -3
- data/lib/discorb/channel.rb +89 -53
- data/lib/discorb/client.rb +36 -33
- data/lib/discorb/common.rb +28 -21
- data/lib/discorb/components/button.rb +106 -0
- data/lib/discorb/components/select_menu.rb +157 -0
- data/lib/discorb/components/text_input.rb +96 -0
- data/lib/discorb/components.rb +11 -276
- data/lib/discorb/dictionary.rb +13 -2
- data/lib/discorb/embed.rb +2 -2
- data/lib/discorb/emoji.rb +21 -5
- data/lib/discorb/emoji_table.rb +1 -1
- data/lib/discorb/error.rb +4 -6
- data/lib/discorb/event.rb +13 -11
- data/lib/discorb/exe/about.rb +1 -0
- data/lib/discorb/exe/irb.rb +4 -3
- data/lib/discorb/exe/new.rb +6 -7
- data/lib/discorb/exe/run.rb +2 -1
- data/lib/discorb/exe/setup.rb +8 -5
- data/lib/discorb/exe/show.rb +1 -0
- data/lib/discorb/extend.rb +19 -14
- data/lib/discorb/extension.rb +5 -1
- data/lib/discorb/gateway.rb +75 -27
- data/lib/discorb/guild.rb +58 -80
- data/lib/discorb/guild_template.rb +5 -5
- data/lib/discorb/http.rb +34 -169
- data/lib/discorb/integration.rb +32 -3
- data/lib/discorb/intents.rb +1 -1
- data/lib/discorb/interaction/autocomplete.rb +5 -4
- data/lib/discorb/interaction/command.rb +34 -9
- data/lib/discorb/interaction/components.rb +5 -2
- data/lib/discorb/interaction/modal.rb +33 -0
- data/lib/discorb/interaction/response.rb +41 -12
- data/lib/discorb/interaction/root.rb +1 -0
- data/lib/discorb/interaction.rb +2 -1
- data/lib/discorb/invite.rb +1 -1
- data/lib/discorb/log.rb +4 -3
- data/lib/discorb/member.rb +4 -6
- data/lib/discorb/message.rb +31 -282
- data/lib/discorb/message_meta.rb +205 -0
- data/lib/discorb/modules.rb +11 -11
- data/lib/discorb/permission.rb +2 -2
- data/lib/discorb/presence.rb +6 -3
- data/lib/discorb/rate_limit.rb +15 -21
- data/lib/discorb/role.rb +3 -3
- data/lib/discorb/sticker.rb +2 -2
- data/lib/discorb/user.rb +3 -3
- data/lib/discorb/utils/colored_puts.rb +1 -0
- data/lib/discorb/voice_state.rb +7 -2
- data/lib/discorb/webhook.rb +8 -5
- data/lib/discorb.rb +1 -0
- data/template-replace/scripts/arrow.rb +1 -0
- data/template-replace/scripts/favicon.rb +1 -0
- data/template-replace/scripts/index.rb +2 -1
- data/template-replace/scripts/locale_ja.rb +5 -4
- data/template-replace/scripts/sidebar.rb +1 -0
- data/template-replace/scripts/version.rb +7 -10
- data/template-replace/scripts/yard_replace.rb +5 -4
- metadata +29 -4
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Discorb
|
4
|
+
#
|
5
|
+
# Represents a button component.
|
6
|
+
#
|
7
|
+
class Button < Component
|
8
|
+
# @return [String] The label of the button.
|
9
|
+
attr_accessor :label
|
10
|
+
# @return [:primary, :secondary, :success, :danger, :link] The style of the button.
|
11
|
+
attr_accessor :style
|
12
|
+
# @return [Discorb::Emoji] The emoji of the button.
|
13
|
+
attr_accessor :emoji
|
14
|
+
# @return [String] The custom ID of the button.
|
15
|
+
# Won't be used if the style is `:link`.
|
16
|
+
attr_accessor :custom_id
|
17
|
+
# @return [String] The URL of the button.
|
18
|
+
# Only used when the style is `:link`.
|
19
|
+
attr_accessor :url
|
20
|
+
# @return [Boolean] Whether the button is disabled.
|
21
|
+
attr_accessor :disabled
|
22
|
+
alias disabled? disabled
|
23
|
+
|
24
|
+
@styles = {
|
25
|
+
primary: 1,
|
26
|
+
secondary: 2,
|
27
|
+
success: 3,
|
28
|
+
danger: 4,
|
29
|
+
link: 5,
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
#
|
33
|
+
# Initialize a new button.
|
34
|
+
#
|
35
|
+
# @param [String] label The label of the button.
|
36
|
+
# @param [:primary, :secondary, :success, :danger, :link] style The style of the button.
|
37
|
+
# @param [Discorb::Emoji] emoji The emoji of the button.
|
38
|
+
# @param [String] custom_id The custom ID of the button.
|
39
|
+
# @param [String] url The URL of the button.
|
40
|
+
# @param [Boolean] disabled Whether the button is disabled.
|
41
|
+
#
|
42
|
+
def initialize(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false)
|
43
|
+
@label = label
|
44
|
+
@style = style
|
45
|
+
@emoji = emoji
|
46
|
+
@custom_id = custom_id
|
47
|
+
@url = url
|
48
|
+
@disabled = disabled
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Converts the button to a hash.
|
53
|
+
#
|
54
|
+
# @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure Official Discord API docs
|
55
|
+
# @return [Hash] A hash representation of the button.
|
56
|
+
#
|
57
|
+
def to_hash
|
58
|
+
if @style == :link
|
59
|
+
{
|
60
|
+
type: 2,
|
61
|
+
label: @label,
|
62
|
+
style: self.class.styles[@style],
|
63
|
+
url: @url,
|
64
|
+
emoji: @emoji&.to_hash,
|
65
|
+
disabled: @disabled,
|
66
|
+
}
|
67
|
+
else
|
68
|
+
{
|
69
|
+
type: 2,
|
70
|
+
label: @label,
|
71
|
+
style: self.class.styles[@style],
|
72
|
+
custom_id: @custom_id,
|
73
|
+
emoji: @emoji&.to_hash,
|
74
|
+
disabled: @disabled,
|
75
|
+
}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def inspect
|
80
|
+
"#<#{self.class}: #{@custom_id || @url}>"
|
81
|
+
end
|
82
|
+
|
83
|
+
class << self
|
84
|
+
# @private
|
85
|
+
attr_reader :styles
|
86
|
+
|
87
|
+
#
|
88
|
+
# Creates a new button from a hash.
|
89
|
+
#
|
90
|
+
# @param [Hash] data The hash to create the button from.
|
91
|
+
#
|
92
|
+
# @return [Discorb::Button] The created button.
|
93
|
+
#
|
94
|
+
def from_hash(data)
|
95
|
+
new(
|
96
|
+
data[:label],
|
97
|
+
data[:style],
|
98
|
+
emoji: data[:emoji],
|
99
|
+
custom_id: data[:custom_id],
|
100
|
+
url: data[:url],
|
101
|
+
disabled: data[:disabled],
|
102
|
+
)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +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(custom_id, options, placeholder: nil, min_values: 1, max_values: 1)
|
30
|
+
@custom_id = custom_id
|
31
|
+
@options = options
|
32
|
+
@placeholder = placeholder
|
33
|
+
@min_values = min_values
|
34
|
+
@max_values = max_values
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Converts the select menu to a hash.
|
39
|
+
#
|
40
|
+
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure Official Discord API docs
|
41
|
+
# @return [Hash] A hash representation of the select menu.
|
42
|
+
#
|
43
|
+
def to_hash
|
44
|
+
{
|
45
|
+
type: 3,
|
46
|
+
custom_id: @custom_id,
|
47
|
+
options: @options.map(&:to_hash),
|
48
|
+
placeholder: @placeholder,
|
49
|
+
min_values: @min_values,
|
50
|
+
max_values: @max_values,
|
51
|
+
disabled: @disabled,
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
def inspect
|
56
|
+
"#<#{self.class}: #{@custom_id}>"
|
57
|
+
end
|
58
|
+
|
59
|
+
class << self
|
60
|
+
#
|
61
|
+
# Creates a new select menu from a hash.
|
62
|
+
#
|
63
|
+
# @param [Hash] data The hash to create the select menu from.
|
64
|
+
#
|
65
|
+
# @return [Discorb::SelectMenu] The created select menu.
|
66
|
+
#
|
67
|
+
def from_hash(data)
|
68
|
+
new(
|
69
|
+
data[:custom_id],
|
70
|
+
data[:options].map { |o| SelectMenu::Option.from_hash(o) },
|
71
|
+
placeholder: data[:placeholder],
|
72
|
+
min_values: data[:min_values],
|
73
|
+
max_values: data[:max_values],
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Represents an option of a select menu.
|
80
|
+
#
|
81
|
+
class Option
|
82
|
+
# @return [String] The label of the option.
|
83
|
+
attr_accessor :label
|
84
|
+
# @return [String] The value of the option.
|
85
|
+
attr_accessor :value
|
86
|
+
# @return [String] The description of the option.
|
87
|
+
attr_accessor :description
|
88
|
+
# @return [Discorb::Emoji] The emoji of the option.
|
89
|
+
attr_accessor :emoji
|
90
|
+
# @return [Boolean] Whether the option is default.
|
91
|
+
attr_accessor :default
|
92
|
+
|
93
|
+
#
|
94
|
+
# Initialize a new option.
|
95
|
+
#
|
96
|
+
# @param [String] label The label of the option.
|
97
|
+
# @param [String] value The value of the option.
|
98
|
+
# @param [String] description The description of the option.
|
99
|
+
# @param [Discorb::Emoji] emoji The emoji of the option.
|
100
|
+
# @param [Boolean] default Whether the option is default.
|
101
|
+
def initialize(label, value, description: nil, emoji: nil, default: false)
|
102
|
+
@label = label
|
103
|
+
@value = value
|
104
|
+
@description = description
|
105
|
+
@emoji = emoji
|
106
|
+
@default = default
|
107
|
+
end
|
108
|
+
|
109
|
+
#
|
110
|
+
# Converts the option to a hash.
|
111
|
+
#
|
112
|
+
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure Official Discord API docs
|
113
|
+
# @return [Hash] Hash representation of the option.
|
114
|
+
#
|
115
|
+
def to_hash
|
116
|
+
{
|
117
|
+
label: @label,
|
118
|
+
value: @value,
|
119
|
+
description: @description,
|
120
|
+
emoji: @emoji&.to_hash,
|
121
|
+
default: @default,
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
# @private
|
126
|
+
def hash_emoji(emoji)
|
127
|
+
case emoji
|
128
|
+
when UnicodeEmoji
|
129
|
+
{
|
130
|
+
id: nil,
|
131
|
+
name: emoji.to_s,
|
132
|
+
animated: false,
|
133
|
+
}
|
134
|
+
when CustomEmoji
|
135
|
+
{
|
136
|
+
id: emoji.id,
|
137
|
+
name: emoji.name,
|
138
|
+
animated: emoji.animated?,
|
139
|
+
}
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class << self
|
144
|
+
#
|
145
|
+
# Creates a new option from a hash.
|
146
|
+
#
|
147
|
+
# @param [Hash] data A hash representing the option.
|
148
|
+
#
|
149
|
+
# @return [Discorb::SelectMenu::Option] A new option.
|
150
|
+
#
|
151
|
+
def from_hash(data)
|
152
|
+
new(data[:label], data[:value], description: data[:description], emoji: data[:emoji], default: data[:default])
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,96 @@
|
|
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
|
+
short: 1,
|
11
|
+
paragraph: 2,
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
# @return [String] The label of the text input.
|
15
|
+
attr_accessor :label
|
16
|
+
# @return [String] The custom id of the text input.
|
17
|
+
attr_accessor :custom_id
|
18
|
+
# @return [:short, :paragraph] The style of the text input.
|
19
|
+
attr_accessor :style
|
20
|
+
# @return [Integer, nil] The minimum length of the text input.
|
21
|
+
attr_accessor :min_length
|
22
|
+
# @return [Integer, nil] The maximum length of the text input.
|
23
|
+
attr_accessor :max_length
|
24
|
+
# @return [Boolean] Whether the text input is required.
|
25
|
+
attr_accessor :required
|
26
|
+
# @return [String, nil] The prefilled value of the text input.
|
27
|
+
attr_accessor :value
|
28
|
+
# @return [String, nil] The placeholder of the text input.
|
29
|
+
attr_accessor :placeholder
|
30
|
+
|
31
|
+
#
|
32
|
+
# Initialize a new text input component.
|
33
|
+
#
|
34
|
+
# @param [String] label The label of the text input.
|
35
|
+
# @param [String] custom_id The custom id of the text input.
|
36
|
+
# @param [:short, :paragraph] style The style of the text input.
|
37
|
+
# @param [Integer, nil] min_length The minimum length of the text input.
|
38
|
+
# @param [Integer, nil] max_length The maximum length of the text input.
|
39
|
+
# @param [Boolean] required Whether the text input is required.
|
40
|
+
# @param [String, nil] value The prefilled value of the text input.
|
41
|
+
# @param [String, nil] placeholder The placeholder of the text input.
|
42
|
+
#
|
43
|
+
def initialize(label, custom_id, style, min_length: nil, max_length: nil, required: false, value: nil, placeholder: nil)
|
44
|
+
@label = label
|
45
|
+
@custom_id = custom_id
|
46
|
+
@style = style
|
47
|
+
@min_length = min_length
|
48
|
+
@max_length = max_length
|
49
|
+
@required = required
|
50
|
+
@value = value
|
51
|
+
@placeholder = placeholder
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Converts the select menu to a hash.
|
56
|
+
#
|
57
|
+
# @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure Official Discord API docs
|
58
|
+
# @return [Hash] A hash representation of the text input.
|
59
|
+
#
|
60
|
+
def to_hash
|
61
|
+
{
|
62
|
+
type: 4,
|
63
|
+
label: @label,
|
64
|
+
style: STYLES[@style],
|
65
|
+
custom_id: @custom_id,
|
66
|
+
min_length: @min_length,
|
67
|
+
max_length: @max_length,
|
68
|
+
required: @required,
|
69
|
+
value: @value,
|
70
|
+
placeholder: @placeholder,
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
class << self
|
75
|
+
#
|
76
|
+
# Creates a new text input from a hash.
|
77
|
+
#
|
78
|
+
# @param [Hash] data The hash to create the text input from.
|
79
|
+
#
|
80
|
+
# @return [Discorb::TextInput] The created text input.
|
81
|
+
#
|
82
|
+
def from_hash(data)
|
83
|
+
new(
|
84
|
+
data[:label],
|
85
|
+
data[:custom_id],
|
86
|
+
STYLES.key(data[:style]),
|
87
|
+
min_length: data[:min_length],
|
88
|
+
max_length: data[:max_length],
|
89
|
+
required: data[:required],
|
90
|
+
value: data[:value],
|
91
|
+
placeholder: data[:placeholder],
|
92
|
+
)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/discorb/components.rb
CHANGED
@@ -22,23 +22,12 @@ module Discorb
|
|
22
22
|
def from_hash(data)
|
23
23
|
case data[:type]
|
24
24
|
when 2
|
25
|
-
Button
|
26
|
-
data[:label],
|
27
|
-
data[:style],
|
28
|
-
emoji: data[:emoji],
|
29
|
-
custom_id: data[:custom_id],
|
30
|
-
url: data[:url],
|
31
|
-
disabled: data[:disabled],
|
32
|
-
)
|
25
|
+
Button
|
33
26
|
when 3
|
34
|
-
SelectMenu
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
min_values: data[:min_values],
|
39
|
-
max_values: data[:max_values],
|
40
|
-
)
|
41
|
-
end
|
27
|
+
SelectMenu
|
28
|
+
when 4
|
29
|
+
TextInput
|
30
|
+
end.from_hash(data)
|
42
31
|
end
|
43
32
|
|
44
33
|
#
|
@@ -57,7 +46,7 @@ module Discorb
|
|
57
46
|
tmp_components << tmp_row
|
58
47
|
tmp_row = []
|
59
48
|
tmp_components << c
|
60
|
-
when SelectMenu
|
49
|
+
when SelectMenu, TextInput
|
61
50
|
tmp_components << tmp_row
|
62
51
|
tmp_row = []
|
63
52
|
tmp_components << [c]
|
@@ -66,266 +55,12 @@ module Discorb
|
|
66
55
|
end
|
67
56
|
end
|
68
57
|
tmp_components << tmp_row
|
69
|
-
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
#
|
75
|
-
# Represents a button component.
|
76
|
-
#
|
77
|
-
class Button < Component
|
78
|
-
# @return [String] The label of the button.
|
79
|
-
attr_accessor :label
|
80
|
-
# @return [:primary, :secondary, :success, :danger, :link] The style of the button.
|
81
|
-
attr_accessor :style
|
82
|
-
# @return [Discorb::Emoji] The emoji of the button.
|
83
|
-
attr_accessor :emoji
|
84
|
-
# @return [String] The custom ID of the button.
|
85
|
-
# Won't be used if the style is `:link`.
|
86
|
-
attr_accessor :custom_id
|
87
|
-
# @return [String] The URL of the button.
|
88
|
-
# Only used when the style is `:link`.
|
89
|
-
attr_accessor :url
|
90
|
-
# @return [Boolean] Whether the button is disabled.
|
91
|
-
attr_accessor :disabled
|
92
|
-
alias disabled? disabled
|
93
|
-
|
94
|
-
@styles = {
|
95
|
-
primary: 1,
|
96
|
-
secondary: 2,
|
97
|
-
success: 3,
|
98
|
-
danger: 4,
|
99
|
-
link: 5,
|
100
|
-
}.freeze
|
101
|
-
|
102
|
-
#
|
103
|
-
# Initialize a new button.
|
104
|
-
#
|
105
|
-
# @param [String] label The label of the button.
|
106
|
-
# @param [:primary, :secondary, :success, :danger, :link] style The style of the button.
|
107
|
-
# @param [Discorb::Emoji] emoji The emoji of the button.
|
108
|
-
# @param [String] custom_id The custom ID of the button.
|
109
|
-
# @param [String] url The URL of the button.
|
110
|
-
# @param [Boolean] disabled Whether the button is disabled.
|
111
|
-
#
|
112
|
-
def initialize(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false)
|
113
|
-
@label = label
|
114
|
-
@style = style
|
115
|
-
@emoji = emoji
|
116
|
-
@custom_id = custom_id
|
117
|
-
@url = url
|
118
|
-
@disabled = disabled
|
119
|
-
end
|
120
|
-
|
121
|
-
#
|
122
|
-
# Converts the button to a hash.
|
123
|
-
#
|
124
|
-
# @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure Official Discord API docs
|
125
|
-
# @return [Hash] A hash representation of the button.
|
126
|
-
#
|
127
|
-
def to_hash
|
128
|
-
if @style == :link
|
129
|
-
{
|
130
|
-
type: 2,
|
131
|
-
label: @label,
|
132
|
-
style: self.class.styles[@style],
|
133
|
-
url: @url,
|
134
|
-
emoji: hash_emoji(@emoji),
|
135
|
-
disabled: @disabled,
|
136
|
-
}
|
137
|
-
else
|
138
|
-
{
|
139
|
-
type: 2,
|
140
|
-
label: @label,
|
141
|
-
style: self.class.styles[@style],
|
142
|
-
custom_id: @custom_id,
|
143
|
-
emoji: hash_emoji(@emoji),
|
144
|
-
disabled: @disabled,
|
145
|
-
}
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def inspect
|
150
|
-
"#<#{self.class}: #{@custom_id || @url}>"
|
151
|
-
end
|
152
|
-
|
153
|
-
class << self
|
154
|
-
# @private
|
155
|
-
attr_reader :styles
|
156
|
-
end
|
157
|
-
|
158
|
-
private
|
159
|
-
|
160
|
-
def hash_emoji(emoji)
|
161
|
-
case emoji
|
162
|
-
when UnicodeEmoji
|
163
|
-
{
|
164
|
-
id: nil,
|
165
|
-
name: emoji.to_s,
|
166
|
-
animated: false,
|
167
|
-
}
|
168
|
-
when CustomEmoji
|
169
|
-
{
|
170
|
-
id: emoji.id,
|
171
|
-
name: emoji.name,
|
172
|
-
animated: emoji.animated?,
|
173
|
-
}
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
#
|
179
|
-
# Represents a select menu component.
|
180
|
-
#
|
181
|
-
class SelectMenu < Component
|
182
|
-
# @return [String] The custom ID of the select menu.
|
183
|
-
attr_accessor :custom_id
|
184
|
-
# @return [Array<SelectMenu::Option>] The options of the select menu.
|
185
|
-
attr_accessor :options
|
186
|
-
# @return [Integer] The minimum number of values.
|
187
|
-
attr_accessor :min_values
|
188
|
-
# @return [Integer] The maximum number of values.
|
189
|
-
attr_accessor :max_values
|
190
|
-
# @return [Boolean] Whether the select menu is disabled.
|
191
|
-
attr_accessor :disabled
|
192
|
-
alias disabled? disabled
|
193
|
-
|
194
|
-
#
|
195
|
-
# Initialize a new select menu.
|
196
|
-
#
|
197
|
-
# @param [String, Symbol] custom_id Custom ID of the select menu.
|
198
|
-
# @param [Array<Discorb::SelectMenu::Option>] options The options of the select menu.
|
199
|
-
# @param [String] placeholder The placeholder of the select menu.
|
200
|
-
# @param [Integer] min_values The minimum number of values.
|
201
|
-
# @param [Integer] max_values The maximum number of values.
|
202
|
-
#
|
203
|
-
def initialize(custom_id, options, placeholder: nil, min_values: 1, max_values: 1)
|
204
|
-
@custom_id = custom_id
|
205
|
-
@options = options
|
206
|
-
@placeholder = placeholder
|
207
|
-
@min_values = min_values
|
208
|
-
@max_values = max_values
|
209
|
-
end
|
210
|
-
|
211
|
-
#
|
212
|
-
# Converts the select menu to a hash.
|
213
|
-
#
|
214
|
-
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure Official Discord API docs
|
215
|
-
# @return [Hash] A hash representation of the select menu.
|
216
|
-
#
|
217
|
-
def to_hash
|
218
|
-
{
|
219
|
-
type: 3,
|
220
|
-
custom_id: @custom_id,
|
221
|
-
options: @options.map(&:to_hash),
|
222
|
-
placeholder: @placeholder,
|
223
|
-
min_values: @min_values,
|
224
|
-
max_values: @max_values,
|
225
|
-
disabled: @disabled,
|
226
|
-
}
|
227
|
-
end
|
228
|
-
|
229
|
-
def inspect
|
230
|
-
"#<#{self.class}: #{@custom_id}>"
|
231
|
-
end
|
232
|
-
|
233
|
-
#
|
234
|
-
# Represents an option of a select menu.
|
235
|
-
#
|
236
|
-
class Option
|
237
|
-
# @return [String] The label of the option.
|
238
|
-
attr_accessor :label
|
239
|
-
# @return [String] The value of the option.
|
240
|
-
attr_accessor :value
|
241
|
-
# @return [String] The description of the option.
|
242
|
-
attr_accessor :description
|
243
|
-
# @return [Discorb::Emoji] The emoji of the option.
|
244
|
-
attr_accessor :emoji
|
245
|
-
# @return [Boolean] Whether the option is default.
|
246
|
-
attr_accessor :default
|
247
|
-
|
248
|
-
#
|
249
|
-
# Initialize a new option.
|
250
|
-
#
|
251
|
-
# @param [String] label The label of the option.
|
252
|
-
# @param [String] value The value of the option.
|
253
|
-
# @param [String] description The description of the option.
|
254
|
-
# @param [Discorb::Emoji] emoji The emoji of the option.
|
255
|
-
# @param [Boolean] default Whether the option is default.
|
256
|
-
def initialize(label, value, description: nil, emoji: nil, default: false)
|
257
|
-
@label = label
|
258
|
-
@value = value
|
259
|
-
@description = description
|
260
|
-
@emoji = emoji
|
261
|
-
@default = default
|
262
|
-
end
|
263
|
-
|
264
|
-
#
|
265
|
-
# Converts the option to a hash.
|
266
|
-
#
|
267
|
-
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure Official Discord API docs
|
268
|
-
# @return [Hash] Hash representation of the option.
|
269
|
-
#
|
270
|
-
def to_hash
|
271
|
-
{
|
272
|
-
label: @label,
|
273
|
-
value: @value,
|
274
|
-
description: @description,
|
275
|
-
emoji: hash_emoji(@emoji),
|
276
|
-
default: @default,
|
277
|
-
}
|
278
|
-
end
|
279
|
-
|
280
|
-
# @private
|
281
|
-
def hash_emoji(emoji)
|
282
|
-
case emoji
|
283
|
-
when UnicodeEmoji
|
284
|
-
{
|
285
|
-
id: nil,
|
286
|
-
name: emoji.to_s,
|
287
|
-
animated: false,
|
288
|
-
}
|
289
|
-
when CustomEmoji
|
290
|
-
{
|
291
|
-
id: emoji.id,
|
292
|
-
name: emoji.name,
|
293
|
-
animated: emoji.animated?,
|
294
|
-
}
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
class << self
|
299
|
-
#
|
300
|
-
# Creates a new option from a hash.
|
301
|
-
#
|
302
|
-
# @param [Hash] data A hash representing the option.
|
303
|
-
#
|
304
|
-
# @return [Discorb::SelectMenu::Option] A new option.
|
305
|
-
#
|
306
|
-
def from_hash(data)
|
307
|
-
new(data[:label], data[:value], description: data[:description], emoji: data[:emoji], default: data[:default])
|
308
|
-
end
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
|
-
private
|
313
|
-
|
314
|
-
def hash_emoji(emoji)
|
315
|
-
case emoji
|
316
|
-
when UnicodeEmoji
|
317
|
-
{
|
318
|
-
id: nil,
|
319
|
-
name: emoji.to_s,
|
320
|
-
animated: false,
|
321
|
-
}
|
322
|
-
when CustomEmoji
|
323
|
-
{
|
324
|
-
id: emoji.id,
|
325
|
-
name: emoji.name,
|
326
|
-
animated: emoji.animated?,
|
327
|
-
}
|
58
|
+
tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
|
328
59
|
end
|
329
60
|
end
|
330
61
|
end
|
331
62
|
end
|
63
|
+
|
64
|
+
%w[button text_input select_menu].each do |name|
|
65
|
+
require_relative "components/#{name}"
|
66
|
+
end
|
data/lib/discorb/dictionary.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Discorb
|
4
|
+
#
|
5
|
+
# Extended hash class. This is used for storing pair of ID and object.
|
6
|
+
#
|
4
7
|
class Dictionary
|
5
8
|
# @return [Integer] The maximum number of items in the dictionary.
|
6
9
|
attr_accessor :limit
|
@@ -16,6 +19,7 @@ module Discorb
|
|
16
19
|
@cache = hash.transform_keys(&:to_s)
|
17
20
|
@limit = limit
|
18
21
|
@sort = sort
|
22
|
+
@cache = @cache.sort_by(&@sort).to_h if @sort
|
19
23
|
end
|
20
24
|
|
21
25
|
#
|
@@ -39,7 +43,7 @@ module Discorb
|
|
39
43
|
# @param [Discorb::Dictionary] other The dictionary to merge.
|
40
44
|
#
|
41
45
|
def merge(other)
|
42
|
-
@cache.merge!(other)
|
46
|
+
@cache.merge!(other.to_h)
|
43
47
|
end
|
44
48
|
|
45
49
|
#
|
@@ -73,6 +77,13 @@ module Discorb
|
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
80
|
+
#
|
81
|
+
# Convert the dictionary to a hash.
|
82
|
+
#
|
83
|
+
def to_h
|
84
|
+
@cache
|
85
|
+
end
|
86
|
+
|
76
87
|
#
|
77
88
|
# Returns the values of the dictionary.
|
78
89
|
#
|
@@ -104,7 +115,7 @@ module Discorb
|
|
104
115
|
end
|
105
116
|
end
|
106
117
|
|
107
|
-
def respond_to_missing?(name,
|
118
|
+
def respond_to_missing?(name, ...)
|
108
119
|
if values.respond_to?(name)
|
109
120
|
true
|
110
121
|
else
|