discorb 0.16.0 → 0.18.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +1 -0
- data/.github/workflows/build_main.yml +2 -2
- data/.github/workflows/build_version.yml +1 -1
- data/.github/workflows/codeql-analysis.yml +1 -1
- data/.github/workflows/lint-push.yml +3 -5
- data/.github/workflows/lint.yml +2 -4
- data/.github/workflows/spec.yml +33 -0
- data/.github/workflows/validate.yml +21 -0
- data/.gitignore +2 -0
- data/.lefthook/commit-msg/validator.rb +5 -0
- data/.rspec +2 -0
- data/.rspec_parallel +2 -0
- data/.rubocop.yml +43 -6
- data/Changelog.md +30 -1
- data/Gemfile +20 -8
- data/Rakefile +226 -98
- data/Steepfile +28 -0
- data/bin/console +3 -3
- data/docs/Examples.md +1 -1
- data/docs/application_command.md +156 -47
- data/docs/cli/irb.md +2 -2
- data/docs/cli/new.md +14 -9
- data/docs/cli/run.md +7 -11
- data/docs/cli.md +17 -10
- data/docs/events.md +247 -213
- data/docs/extension.md +1 -2
- data/docs/faq.md +0 -1
- data/docs/tutorial.md +18 -18
- data/docs/voice_events.md +106 -106
- data/examples/commands/message.rb +68 -0
- data/examples/commands/permission.rb +19 -0
- data/examples/commands/slash.rb +48 -0
- data/examples/commands/user.rb +54 -0
- data/examples/components/authorization_button.rb +4 -3
- data/examples/components/select_menu.rb +6 -3
- data/examples/extension/main.rb +2 -1
- data/examples/extension/message_expander.rb +6 -2
- data/examples/sig/commands/message.rbs +5 -0
- data/examples/simple/eval.rb +3 -2
- data/examples/simple/ping_pong.rb +2 -1
- data/examples/simple/rolepanel.rb +17 -6
- data/examples/simple/shard.rb +3 -2
- data/examples/simple/wait_for_message.rb +4 -1
- data/exe/discorb +33 -18
- data/lefthook.yml +45 -0
- data/lib/discorb/allowed_mentions.rb +2 -1
- data/lib/discorb/app_command/command.rb +130 -72
- data/lib/discorb/app_command/common.rb +25 -0
- data/lib/discorb/app_command/handler.rb +130 -33
- data/lib/discorb/app_command.rb +2 -1
- data/lib/discorb/application.rb +1 -0
- data/lib/discorb/asset.rb +1 -2
- data/lib/discorb/attachment.rb +1 -1
- data/lib/discorb/audit_logs.rb +19 -10
- data/lib/discorb/automod.rb +269 -0
- data/lib/discorb/channel/base.rb +108 -0
- data/lib/discorb/channel/category.rb +32 -0
- data/lib/discorb/channel/container.rb +44 -0
- data/lib/discorb/channel/dm.rb +28 -0
- data/lib/discorb/channel/guild.rb +246 -0
- data/lib/discorb/channel/stage.rb +140 -0
- data/lib/discorb/channel/text.rb +336 -0
- data/lib/discorb/channel/thread.rb +325 -0
- data/lib/discorb/channel/voice.rb +79 -0
- data/lib/discorb/channel.rb +2 -1165
- data/lib/discorb/client.rb +50 -36
- data/lib/discorb/color.rb +37 -60
- data/lib/discorb/common.rb +2 -1
- data/lib/discorb/components/button.rb +2 -1
- data/lib/discorb/components/select_menu.rb +4 -2
- data/lib/discorb/components/text_input.rb +12 -2
- data/lib/discorb/components.rb +1 -1
- data/lib/discorb/dictionary.rb +1 -1
- data/lib/discorb/embed.rb +26 -10
- data/lib/discorb/emoji.rb +31 -4
- data/lib/discorb/emoji_table.rb +4969 -3
- data/lib/discorb/event.rb +29 -4
- data/lib/discorb/exe/about.rb +2 -1
- data/lib/discorb/exe/irb.rb +2 -4
- data/lib/discorb/exe/new.rb +89 -26
- data/lib/discorb/exe/run.rb +8 -22
- data/lib/discorb/exe/setup.rb +25 -12
- data/lib/discorb/exe/show.rb +4 -3
- data/lib/discorb/extend.rb +1 -0
- data/lib/discorb/extension.rb +6 -7
- data/lib/discorb/flag.rb +13 -2
- data/lib/discorb/gateway.rb +79 -589
- data/lib/discorb/gateway_events.rb +638 -0
- data/lib/discorb/guild.rb +318 -67
- data/lib/discorb/guild_template.rb +11 -5
- data/lib/discorb/http.rb +53 -24
- data/lib/discorb/integration.rb +4 -1
- data/lib/discorb/intents.rb +28 -19
- data/lib/discorb/interaction/autocomplete.rb +28 -16
- data/lib/discorb/interaction/command.rb +42 -14
- data/lib/discorb/interaction/components.rb +5 -2
- data/lib/discorb/interaction/modal.rb +0 -1
- data/lib/discorb/interaction/response.rb +125 -26
- data/lib/discorb/interaction/root.rb +13 -13
- data/lib/discorb/interaction.rb +1 -0
- data/lib/discorb/invite.rb +5 -2
- data/lib/discorb/member.rb +28 -8
- data/lib/discorb/message.rb +60 -25
- data/lib/discorb/message_meta.rb +3 -3
- data/lib/discorb/modules.rb +59 -16
- data/lib/discorb/presence.rb +2 -0
- data/lib/discorb/rate_limit.rb +7 -2
- data/lib/discorb/reaction.rb +2 -2
- data/lib/discorb/role.rb +20 -5
- data/lib/discorb/shard.rb +1 -1
- data/lib/discorb/sticker.rb +9 -8
- data/lib/discorb/user.rb +4 -3
- data/lib/discorb/utils/colored_puts.rb +1 -0
- data/lib/discorb/voice_state.rb +6 -2
- data/lib/discorb/webhook.rb +64 -31
- data/lib/discorb.rb +7 -5
- data/po/yard.pot +20 -20
- data/rbs_collection.lock.yaml +88 -0
- data/rbs_collection.yaml +21 -0
- data/sig/async.rbs +11 -0
- data/sig/discorb/activity.rbs +23 -0
- data/sig/discorb/allowed_mentions.rbs +44 -0
- data/sig/discorb/app_command/base.rbs +282 -0
- data/sig/discorb/app_command/handler.rbs +171 -0
- data/sig/discorb/application.rbs +142 -0
- data/sig/discorb/asset.rbs +32 -0
- data/sig/discorb/attachment.rbs +91 -0
- data/sig/discorb/audit_log.rbs +231 -0
- data/sig/discorb/automod.rbs +128 -0
- data/sig/discorb/avatar.rbs +26 -0
- data/sig/discorb/channel/base.rbs +179 -0
- data/sig/discorb/channel/category.rbs +56 -0
- data/sig/discorb/channel/container.rbs +29 -0
- data/sig/discorb/channel/dm.rbs +14 -0
- data/sig/discorb/channel/news.rbs +20 -0
- data/sig/discorb/channel/stage.rbs +77 -0
- data/sig/discorb/channel/text.rbs +158 -0
- data/sig/discorb/channel/thread.rbs +185 -0
- data/sig/discorb/channel/voice.rbs +41 -0
- data/sig/discorb/client.rbs +2495 -0
- data/sig/discorb/color.rbs +142 -0
- data/sig/discorb/component/base.rbs +28 -0
- data/sig/discorb/component/button.rbs +65 -0
- data/sig/discorb/component/select_menu.rbs +107 -0
- data/sig/discorb/component/text_input.rbs +69 -0
- data/sig/discorb/connectable.rbs +8 -0
- data/sig/discorb/custom_emoji.rbs +90 -0
- data/sig/discorb/dictionary.rbs +85 -0
- data/sig/discorb/discord_model.rbs +15 -0
- data/sig/discorb/embed.rbs +279 -0
- data/sig/discorb/emoji.rbs +13 -0
- data/sig/discorb/error.rbs +73 -0
- data/sig/discorb/event_handler.rbs +27 -0
- data/sig/discorb/extension.rbs +1734 -0
- data/sig/discorb/flag.rbs +72 -0
- data/sig/discorb/gateway.rbs +481 -0
- data/sig/discorb/guild.rbs +870 -0
- data/sig/discorb/guild_template.rbs +174 -0
- data/sig/discorb/http.rbs +147 -0
- data/sig/discorb/image.rbs +20 -0
- data/sig/discorb/integration.rbs +118 -0
- data/sig/discorb/intents.rbs +97 -0
- data/sig/discorb/interaction/autocomplete.rbs +9 -0
- data/sig/discorb/interaction/base.rbs +66 -0
- data/sig/discorb/interaction/command.rbs +66 -0
- data/sig/discorb/interaction/message_component.rbs +140 -0
- data/sig/discorb/interaction/modal.rbs +50 -0
- data/sig/discorb/interaction/responder.rbs +157 -0
- data/sig/discorb/invite.rbs +86 -0
- data/sig/discorb/member.rbs +187 -0
- data/sig/discorb/message.rbs +469 -0
- data/sig/discorb/messageable.rbs +153 -0
- data/sig/discorb/partial_emoji.rbs +35 -0
- data/sig/discorb/permissions.rbs +149 -0
- data/sig/discorb/presence.rbs +237 -0
- data/sig/discorb/reaction.rbs +33 -0
- data/sig/discorb/role.rbs +145 -0
- data/sig/discorb/scheduled_event.rbs +148 -0
- data/sig/discorb/shard.rbs +62 -0
- data/sig/discorb/snowflake.rbs +56 -0
- data/sig/discorb/stage_instance.rbs +63 -0
- data/sig/discorb/sticker.rbs +116 -0
- data/sig/discorb/system_channel_flag.rbs +17 -0
- data/sig/discorb/unicode_emoji.rbs +49 -0
- data/sig/discorb/user.rbs +93 -0
- data/sig/discorb/utils.rbs +8 -0
- data/sig/discorb/voice_region.rbs +30 -0
- data/sig/discorb/voice_state.rbs +71 -0
- data/sig/discorb/webhook.rbs +327 -0
- data/sig/discorb/welcome_screen.rbs +78 -0
- data/sig/discorb.rbs +6 -7230
- data/sig/manifest.yaml +3 -0
- data/sig/override.rbs +19 -0
- data/template-replace/files/css/common.css +4 -0
- metadata +102 -6
- data/examples/commands/bookmarker.rb +0 -42
- data/examples/commands/hello.rb +0 -10
- data/examples/commands/inspect.rb +0 -25
@@ -19,7 +19,7 @@ module Discorb
|
|
19
19
|
attr_reader :created_at
|
20
20
|
# @return [Time] The time this template was last updated.
|
21
21
|
attr_reader :updated_at
|
22
|
-
# @return [Discorb::
|
22
|
+
# @return [Discorb::Snowflake] The ID of guild where the template was created.
|
23
23
|
attr_reader :source_guild_id
|
24
24
|
# @return [Discorb::GuildTemplate::TemplateGuild] The guild where the template was created.
|
25
25
|
attr_reader :serialized_source_guild
|
@@ -64,7 +64,10 @@ module Discorb
|
|
64
64
|
payload = {}
|
65
65
|
payload[:name] = name if name
|
66
66
|
payload[:description] = description if description != Discorb::Unset
|
67
|
-
@client.http.request(
|
67
|
+
@client.http.request(
|
68
|
+
Route.new("/guilds/#{@source_guild_id}/templates/#{@code}", "//guilds/:guild_id/templates/:code",
|
69
|
+
:patch), payload
|
70
|
+
).wait
|
68
71
|
end
|
69
72
|
end
|
70
73
|
|
@@ -78,7 +81,8 @@ module Discorb
|
|
78
81
|
#
|
79
82
|
def update
|
80
83
|
Async do
|
81
|
-
_resp, data = @client.http.request(Route.new("/guilds/#{@source_guild_id}/templates/#{@code}",
|
84
|
+
_resp, data = @client.http.request(Route.new("/guilds/#{@source_guild_id}/templates/#{@code}",
|
85
|
+
"//guilds/:guild_id/templates/:code", :put)).wait
|
82
86
|
_set_data(data)
|
83
87
|
end
|
84
88
|
end
|
@@ -91,7 +95,8 @@ module Discorb
|
|
91
95
|
#
|
92
96
|
def delete!
|
93
97
|
Async do
|
94
|
-
@client.http.request(Route.new("/guilds/#{@source_guild_id}/templates/#{@code}",
|
98
|
+
@client.http.request(Route.new("/guilds/#{@source_guild_id}/templates/#{@code}",
|
99
|
+
"//guilds/:guild_id/templates/:code", :delete)).wait
|
95
100
|
end
|
96
101
|
end
|
97
102
|
|
@@ -109,7 +114,8 @@ module Discorb
|
|
109
114
|
attr_reader :roles
|
110
115
|
# @return [Discorb::SystemChannelFlag] The flag for the system channel.
|
111
116
|
attr_reader :system_channel_flags
|
112
|
-
# @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::GuildChannel}]
|
117
|
+
# @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::GuildChannel}]
|
118
|
+
# A dictionary of channels in the guild.
|
113
119
|
attr_reader :channels
|
114
120
|
# @return [String] The description of the guild.
|
115
121
|
attr_reader :description
|
data/lib/discorb/http.rb
CHANGED
@@ -27,7 +27,6 @@ module Discorb
|
|
27
27
|
#
|
28
28
|
# @param [Discorb::Route] path The path to the resource.
|
29
29
|
# @param [String, Hash] body The body of the request. Defaults to an empty string.
|
30
|
-
# @param [Hash] headers The headers to send with the request.
|
31
30
|
# @param [String] audit_log_reason The audit log reason to send with the request.
|
32
31
|
# @param [Hash] kwargs The keyword arguments.
|
33
32
|
#
|
@@ -36,17 +35,28 @@ module Discorb
|
|
36
35
|
#
|
37
36
|
# @raise [Discorb::HTTPError] The request was failed.
|
38
37
|
#
|
39
|
-
def request(path, body = "",
|
38
|
+
def request(path, body = "", audit_log_reason: nil, **kwargs)
|
40
39
|
Async do |_task|
|
41
40
|
@ratelimit_handler.wait(path)
|
42
41
|
resp = if %i[post patch put].include? path.method
|
43
|
-
http.send(
|
42
|
+
http.send(
|
43
|
+
path.method,
|
44
|
+
get_path(path),
|
45
|
+
get_body(body),
|
46
|
+
get_headers(body, audit_log_reason),
|
47
|
+
**kwargs,
|
48
|
+
)
|
44
49
|
else
|
45
|
-
http.send(
|
50
|
+
http.send(
|
51
|
+
path.method,
|
52
|
+
get_path(path),
|
53
|
+
get_headers(body, audit_log_reason),
|
54
|
+
**kwargs,
|
55
|
+
)
|
46
56
|
end
|
47
57
|
data = get_response_data(resp)
|
48
58
|
@ratelimit_handler.save(path, resp)
|
49
|
-
handle_response(resp, data, path, body,
|
59
|
+
handle_response(resp, data, path, body, nil, audit_log_reason, kwargs)
|
50
60
|
end
|
51
61
|
end
|
52
62
|
|
@@ -56,8 +66,7 @@ module Discorb
|
|
56
66
|
#
|
57
67
|
# @param [Discorb::Route] path The path to the resource.
|
58
68
|
# @param [String, Hash] body The body of the request.
|
59
|
-
# @param [Array<Discorb::
|
60
|
-
# @param [Hash] headers The headers to send with the request.
|
69
|
+
# @param [Array<Discorb::Attachment>] files The files to upload.
|
61
70
|
# @param [String] audit_log_reason The audit log reason to send with the request.
|
62
71
|
# @param [Hash] kwargs The keyword arguments.
|
63
72
|
#
|
@@ -66,21 +75,30 @@ module Discorb
|
|
66
75
|
#
|
67
76
|
# @raise [Discorb::HTTPError] The request was failed.
|
68
77
|
#
|
69
|
-
def multipart_request(path, body, files,
|
78
|
+
def multipart_request(path, body, files, audit_log_reason: nil, **kwargs)
|
70
79
|
Async do |_task|
|
71
80
|
@ratelimit_handler.wait(path)
|
72
|
-
req = Net::HTTP.const_get(path.method.to_s.capitalize).new(
|
81
|
+
req = Net::HTTP.const_get(path.method.to_s.capitalize).new(
|
82
|
+
get_path(path),
|
83
|
+
get_headers(body, audit_log_reason),
|
84
|
+
**kwargs,
|
85
|
+
)
|
86
|
+
# @type var data: Array[untyped]
|
73
87
|
data = [
|
74
88
|
["payload_json", get_body(body)],
|
75
89
|
]
|
76
90
|
files&.each_with_index do |file, i|
|
77
91
|
next if file.nil?
|
92
|
+
|
78
93
|
if file.created_by == :discord
|
79
94
|
request_io = StringIO.new(
|
80
|
-
cdn_http.get(
|
81
|
-
"
|
82
|
-
|
83
|
-
|
95
|
+
cdn_http.get(
|
96
|
+
(URI.parse(file.url).path || raise("Could not parse url")),
|
97
|
+
{
|
98
|
+
"Content-Type" => nil,
|
99
|
+
"User-Agent" => Discorb::USER_AGENT,
|
100
|
+
}
|
101
|
+
).body
|
84
102
|
)
|
85
103
|
data << ["files[#{i}]", request_io, { filename: file.filename, content_type: file.content_type }]
|
86
104
|
else
|
@@ -91,10 +109,11 @@ module Discorb
|
|
91
109
|
session = Net::HTTP.new("discord.com", 443)
|
92
110
|
session.use_ssl = true
|
93
111
|
resp = session.request(req)
|
94
|
-
|
95
|
-
data = get_response_data(resp)
|
112
|
+
resp_data = get_response_data(resp)
|
96
113
|
@ratelimit_handler.save(path, resp)
|
97
|
-
handle_response(resp,
|
114
|
+
response = handle_response(resp, resp_data, path, body, files, audit_log_reason, kwargs)
|
115
|
+
files&.then { _1.filter(&:will_close).each { |f| f.io.close } }
|
116
|
+
response
|
98
117
|
end
|
99
118
|
end
|
100
119
|
|
@@ -104,12 +123,16 @@ module Discorb
|
|
104
123
|
|
105
124
|
private
|
106
125
|
|
107
|
-
def handle_response(resp, data, path, body,
|
126
|
+
def handle_response(resp, data, path, body, files, audit_log_reason, kwargs)
|
108
127
|
case resp.code
|
109
128
|
when "429"
|
110
129
|
@client.logger.info("Rate limit exceeded for #{path.method} #{path.url}, waiting #{data[:retry_after]} seconds")
|
111
130
|
sleep(data[:retry_after])
|
112
|
-
|
131
|
+
if files
|
132
|
+
multipart_request(path, body, files, audit_log_reason: audit_log_reason, **kwargs).wait
|
133
|
+
else
|
134
|
+
request(path, body, audit_log_reason: audit_log_reason, **kwargs).wait
|
135
|
+
end
|
113
136
|
when "400"
|
114
137
|
raise BadRequestError.new(resp, data)
|
115
138
|
when "401"
|
@@ -123,14 +146,19 @@ module Discorb
|
|
123
146
|
end
|
124
147
|
end
|
125
148
|
|
126
|
-
def get_headers(
|
149
|
+
def get_headers(body = "", audit_log_reason = nil)
|
127
150
|
ret = if body.nil? || body == ""
|
128
|
-
{
|
151
|
+
{
|
152
|
+
"User-Agent" => USER_AGENT,
|
153
|
+
"authorization" => "Bot #{@client.token}",
|
154
|
+
}
|
129
155
|
else
|
130
|
-
{
|
131
|
-
"
|
156
|
+
{
|
157
|
+
"User-Agent" => USER_AGENT,
|
158
|
+
"authorization" => "Bot #{@client.token}",
|
159
|
+
"content-type" => "application/json",
|
160
|
+
}
|
132
161
|
end
|
133
|
-
ret.merge!(headers) if !headers.nil? && headers.length.positive?
|
134
162
|
ret["X-Audit-Log-Reason"] = audit_log_reason unless audit_log_reason.nil?
|
135
163
|
ret
|
136
164
|
end
|
@@ -160,12 +188,13 @@ module Discorb
|
|
160
188
|
data = JSON.parse(resp.body, symbolize_names: true)
|
161
189
|
rescue JSON::ParserError, TypeError
|
162
190
|
data = if resp.body.nil? || resp.body.empty?
|
163
|
-
|
191
|
+
{}
|
164
192
|
else
|
165
193
|
resp.body
|
166
194
|
end
|
167
195
|
end
|
168
196
|
raise CloudFlareBanError.new(resp, @client) if resp["Via"].nil? && resp.code == "429" && data.is_a?(String)
|
197
|
+
|
169
198
|
data
|
170
199
|
end
|
171
200
|
|
data/lib/discorb/integration.rb
CHANGED
@@ -74,7 +74,10 @@ module Discorb
|
|
74
74
|
#
|
75
75
|
def delete!(reason: nil)
|
76
76
|
Async do
|
77
|
-
@client.http.request(
|
77
|
+
@client.http.request(
|
78
|
+
Route.new("/guilds/#{@guild}/integrations/#{@id}", "//guilds/:guild_id/integrations/:integration_id",
|
79
|
+
:delete), {}, audit_log_reason: reason
|
80
|
+
).wait
|
78
81
|
end
|
79
82
|
end
|
80
83
|
|
data/lib/discorb/intents.rb
CHANGED
@@ -23,6 +23,8 @@ module Discorb
|
|
23
23
|
dm_typing: 1 << 14,
|
24
24
|
message_content: 1 << 15,
|
25
25
|
scheduled_events: 1 << 16,
|
26
|
+
automod_configuration: 1 << 20,
|
27
|
+
automod_execution: 1 << 21,
|
26
28
|
}.freeze
|
27
29
|
|
28
30
|
#
|
@@ -44,29 +46,34 @@ module Discorb
|
|
44
46
|
# @param dm_typing [Boolean] Whether dm typing related events are enabled.
|
45
47
|
# @param message_content [Boolean] Whether message content will be sent with events.
|
46
48
|
# @param scheduled_events [Boolean] Whether events related scheduled events are enabled.
|
47
|
-
#
|
49
|
+
# @param automod_configuration [Boolean] Whether automod configuration related events are enabled.
|
50
|
+
# @param automod_execution [Boolean] Whether automod execution related events are enabled.
|
48
51
|
# @note You must enable privileged intents to use `members` and/or `presences` intents.
|
49
52
|
# @note Message Content Intent is not required to use `message_content` intents for now,
|
50
|
-
# this will be required in
|
53
|
+
# this will be required in September 1, 2022. [Learn More](https://support-dev.discord.com/hc/en-us/articles/4404772028055).
|
51
54
|
# You should specify `message_content` intent for preventing unexpected changes in the future.
|
52
55
|
#
|
53
|
-
def initialize(
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
56
|
+
def initialize(
|
57
|
+
guilds: true,
|
58
|
+
members: false,
|
59
|
+
bans: true,
|
60
|
+
emojis: true,
|
61
|
+
integrations: true,
|
62
|
+
webhooks: true,
|
63
|
+
invites: true,
|
64
|
+
voice_states: true,
|
65
|
+
presences: false,
|
66
|
+
messages: true,
|
67
|
+
reactions: true,
|
68
|
+
typing: true,
|
69
|
+
dm_messages: true,
|
70
|
+
dm_reactions: true,
|
71
|
+
dm_typing: true,
|
72
|
+
message_content: nil,
|
73
|
+
scheduled_events: true,
|
74
|
+
automod_configuration: true,
|
75
|
+
automod_execution: true
|
76
|
+
)
|
70
77
|
@raw_value = {
|
71
78
|
guilds: guilds,
|
72
79
|
members: members,
|
@@ -85,6 +92,8 @@ module Discorb
|
|
85
92
|
dm_typing: dm_typing,
|
86
93
|
message_content: message_content,
|
87
94
|
scheduled_events: scheduled_events,
|
95
|
+
automod_configuration: automod_configuration,
|
96
|
+
automod_execution: automod_execution,
|
88
97
|
}
|
89
98
|
end
|
90
99
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Discorb
|
3
4
|
#
|
4
5
|
# Represents auto complete interaction.
|
@@ -12,35 +13,46 @@ module Discorb
|
|
12
13
|
def _set_data(data)
|
13
14
|
super
|
14
15
|
Sync do
|
15
|
-
name, options = Discorb::CommandInteraction::
|
16
|
+
name, options = Discorb::CommandInteraction::ChatInputCommand.get_command_data(data)
|
16
17
|
|
17
|
-
unless (command = @client.
|
18
|
+
unless (command = @client.callable_commands.find { |c| c.to_s == name && c.type_raw == 1 })
|
18
19
|
@client.logger.warn "Unknown command name #{name}, ignoring"
|
19
20
|
next
|
20
21
|
end
|
21
22
|
|
22
23
|
option_map = command.options.to_h { |k, v| [k.to_s, v[:default]] }
|
23
|
-
Discorb::CommandInteraction::
|
24
|
+
Discorb::CommandInteraction::ChatInputCommand.modify_option_map(option_map, options, guild, {}, {})
|
24
25
|
focused_index = options.find_index { |o| o[:focused] }
|
25
|
-
val = command.options.values.filter
|
26
|
+
val = command.options.values.filter do |option|
|
27
|
+
option[:type] != :attachment
|
28
|
+
end[focused_index][:autocomplete]&.call(self, *command.options.map do |k, _v|
|
29
|
+
option_map[k.to_s]
|
30
|
+
end)
|
26
31
|
send_complete_result(val)
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
30
35
|
def send_complete_result(val)
|
31
|
-
@client.http.request(
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
@client.http.request(
|
37
|
+
Route.new(
|
38
|
+
"/interactions/#{@id}/#{@token}/callback",
|
39
|
+
"//interactions/:interaction_id/:token/callback",
|
40
|
+
:post
|
41
|
+
), {
|
42
|
+
type: 8,
|
43
|
+
data: {
|
44
|
+
choices: val.map do |vk, vv|
|
45
|
+
{
|
46
|
+
name: vk,
|
47
|
+
value: vv,
|
48
|
+
}
|
49
|
+
end,
|
50
|
+
},
|
51
|
+
}
|
52
|
+
).wait
|
42
53
|
rescue Discorb::NotFoundError
|
43
|
-
@client.logger.warn "Failed to send auto complete result,
|
54
|
+
@client.logger.warn "Failed to send auto complete result, " \
|
55
|
+
"This may be caused by the suggestion is taking too long (over 3 seconds) to respond"
|
44
56
|
end
|
45
57
|
|
46
58
|
class << self
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Discorb
|
3
4
|
#
|
4
5
|
# Represents a command interaction.
|
@@ -12,7 +13,7 @@ module Discorb
|
|
12
13
|
#
|
13
14
|
# Represents a slash command interaction.
|
14
15
|
#
|
15
|
-
class
|
16
|
+
class ChatInputCommand < CommandInteraction
|
16
17
|
@command_type = 1
|
17
18
|
@event_name = :slash_command
|
18
19
|
|
@@ -21,15 +22,15 @@ module Discorb
|
|
21
22
|
def _set_data(data)
|
22
23
|
super
|
23
24
|
|
24
|
-
name, options =
|
25
|
+
name, options = ChatInputCommand.get_command_data(data)
|
25
26
|
|
26
|
-
unless (command = @client.
|
27
|
+
unless (command = @client.callable_commands.find { |c| c.to_s == name && c.type_raw == 1 })
|
27
28
|
@client.logger.warn "Unknown command name #{name}, ignoring"
|
28
29
|
return
|
29
30
|
end
|
30
31
|
|
31
32
|
option_map = command.options.to_h { |k, v| [k.to_s, v[:default]] }
|
32
|
-
|
33
|
+
ChatInputCommand.modify_option_map(option_map, options, guild, @members, @attachments)
|
33
34
|
|
34
35
|
command.block.call(self, *command.options.map { |k, _v| option_map[k.to_s] })
|
35
36
|
end
|
@@ -82,13 +83,25 @@ module Discorb
|
|
82
83
|
when 3, 4, 5, 10
|
83
84
|
option[:value]
|
84
85
|
when 6
|
85
|
-
members[option[:value]] || guild.members[option[:value]] ||
|
86
|
+
members[option[:value]] || guild && (guild.members[option[:value]] ||
|
87
|
+
guild.fetch_member(option[:value]).wait)
|
86
88
|
when 7
|
87
|
-
|
89
|
+
if guild
|
90
|
+
guild.channels[option[:value]] || guild.fetch_channels.wait.find do |channel|
|
91
|
+
channel.id == option[:value]
|
92
|
+
end
|
93
|
+
end
|
88
94
|
when 8
|
89
|
-
guild.roles[option[:value]] ||
|
95
|
+
guild && (guild.roles[option[:value]] ||
|
96
|
+
guild.fetch_roles.wait.find { |role| role.id == option[:value] })
|
90
97
|
when 9
|
91
|
-
members[option[:value]] ||
|
98
|
+
members[option[:value]] ||
|
99
|
+
guild && (guild.members[option[:value]] ||
|
100
|
+
guild.roles[option[:value]] ||
|
101
|
+
guild.fetch_member(option[:value]).wait ||
|
102
|
+
guild.fetch_roles.wait.find do |role|
|
103
|
+
role.id == option[:value]
|
104
|
+
end)
|
92
105
|
when 11
|
93
106
|
attachments[option[:value]]
|
94
107
|
end
|
@@ -112,8 +125,17 @@ module Discorb
|
|
112
125
|
|
113
126
|
def _set_data(data)
|
114
127
|
super
|
115
|
-
@target = guild.members[data[:target_id]] || Discorb::Member.new(
|
116
|
-
|
128
|
+
@target = guild.members[data[:target_id]] || Discorb::Member.new(
|
129
|
+
@client, @guild_id,
|
130
|
+
data[:resolved][:users][data[:target_id].to_sym],
|
131
|
+
data[:resolved][:members][data[:target_id].to_sym]
|
132
|
+
)
|
133
|
+
command = @client.commands.find { |c| c.name["default"] == data[:name] && c.type_raw == 2 }
|
134
|
+
if command
|
135
|
+
command.block.call(self, @target)
|
136
|
+
else
|
137
|
+
@client.logger.warn "Unknown command name #{data[:name]}, ignoring"
|
138
|
+
end
|
117
139
|
end
|
118
140
|
end
|
119
141
|
|
@@ -132,7 +154,12 @@ module Discorb
|
|
132
154
|
def _set_data(data)
|
133
155
|
super
|
134
156
|
@target = @messages[data[:target_id]]
|
135
|
-
@client.commands.find { |c| c.name == data[:name] && c.type_raw == 3 }
|
157
|
+
command = @client.commands.find { |c| c.name["default"] == data[:name] && c.type_raw == 3 }
|
158
|
+
if command
|
159
|
+
command.block.call(self, @target)
|
160
|
+
else
|
161
|
+
@client.logger.warn "Unknown command name #{data[:name]}, ignoring"
|
162
|
+
end
|
136
163
|
end
|
137
164
|
end
|
138
165
|
|
@@ -152,10 +179,11 @@ module Discorb
|
|
152
179
|
@client, @guild_id, data[:resolved][:users][id], member
|
153
180
|
)
|
154
181
|
end
|
155
|
-
|
156
|
-
|
182
|
+
|
183
|
+
data[:resolved][:messages]&.each do |id, message|
|
184
|
+
@messages[id.to_s] = Message.new(@client, message.merge(guild_id: @guild_id.to_s))
|
157
185
|
end
|
158
|
-
data[:resolved][:attachments]&.
|
186
|
+
data[:resolved][:attachments]&.each do |id, attachment|
|
159
187
|
@attachments[id.to_s] = Attachment.new(attachment)
|
160
188
|
end
|
161
189
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
module Discorb
|
3
2
|
|
3
|
+
module Discorb
|
4
4
|
#
|
5
5
|
# Represents a message component interaction.
|
6
6
|
# @abstract
|
@@ -41,7 +41,10 @@ module Discorb
|
|
41
41
|
#
|
42
42
|
def make_interaction(client, data)
|
43
43
|
nested_classes.each do |klass|
|
44
|
-
|
44
|
+
if !klass.component_type.nil? && klass.component_type == data[:data][:component_type]
|
45
|
+
return klass.new(client,
|
46
|
+
data)
|
47
|
+
end
|
45
48
|
end
|
46
49
|
client.logger.warn("Unknown component type #{data[:component_type]}, initialized Interaction")
|
47
50
|
MessageComponentInteraction.new(client, data)
|