discorb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +56 -0
- data/.yardopts +6 -0
- data/Changelog.md +5 -0
- data/Gemfile +23 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +46 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/discorb.gemspec +37 -0
- data/docs/Examples.md +26 -0
- data/docs/events.md +480 -0
- data/docs/voice_events.md +283 -0
- data/examples/components/authorization_button.rb +43 -0
- data/examples/components/select_menu.rb +61 -0
- data/examples/extension/main.rb +12 -0
- data/examples/extension/message_expander.rb +41 -0
- data/examples/simple/eval.rb +32 -0
- data/examples/simple/ping_pong.rb +16 -0
- data/examples/simple/rolepanel.rb +65 -0
- data/examples/simple/wait_for_message.rb +30 -0
- data/lib/discorb/application.rb +157 -0
- data/lib/discorb/asset.rb +57 -0
- data/lib/discorb/audit_logs.rb +323 -0
- data/lib/discorb/channel.rb +1101 -0
- data/lib/discorb/client.rb +363 -0
- data/lib/discorb/color.rb +173 -0
- data/lib/discorb/common.rb +123 -0
- data/lib/discorb/components.rb +290 -0
- data/lib/discorb/dictionary.rb +119 -0
- data/lib/discorb/embed.rb +345 -0
- data/lib/discorb/emoji.rb +218 -0
- data/lib/discorb/emoji_table.rb +3799 -0
- data/lib/discorb/error.rb +98 -0
- data/lib/discorb/event.rb +35 -0
- data/lib/discorb/extend.rb +18 -0
- data/lib/discorb/extension.rb +54 -0
- data/lib/discorb/file.rb +69 -0
- data/lib/discorb/flag.rb +109 -0
- data/lib/discorb/gateway.rb +967 -0
- data/lib/discorb/gateway_requests.rb +47 -0
- data/lib/discorb/guild.rb +1244 -0
- data/lib/discorb/guild_template.rb +211 -0
- data/lib/discorb/image.rb +43 -0
- data/lib/discorb/integration.rb +111 -0
- data/lib/discorb/intents.rb +137 -0
- data/lib/discorb/interaction.rb +333 -0
- data/lib/discorb/internet.rb +285 -0
- data/lib/discorb/invite.rb +145 -0
- data/lib/discorb/log.rb +70 -0
- data/lib/discorb/member.rb +232 -0
- data/lib/discorb/message.rb +583 -0
- data/lib/discorb/modules.rb +138 -0
- data/lib/discorb/permission.rb +270 -0
- data/lib/discorb/presence.rb +308 -0
- data/lib/discorb/reaction.rb +48 -0
- data/lib/discorb/role.rb +189 -0
- data/lib/discorb/sticker.rb +157 -0
- data/lib/discorb/user.rb +163 -0
- data/lib/discorb/utils.rb +16 -0
- data/lib/discorb/voice_state.rb +251 -0
- data/lib/discorb/webhook.rb +420 -0
- data/lib/discorb.rb +51 -0
- metadata +120 -0
@@ -0,0 +1,211 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Discorb
|
4
|
+
#
|
5
|
+
# Represents a guild template.
|
6
|
+
#
|
7
|
+
class GuildTemplate < DiscordModel
|
8
|
+
# @return [String] The code of the template.
|
9
|
+
attr_reader :code
|
10
|
+
# @return [String] The name of the template.
|
11
|
+
attr_reader :name
|
12
|
+
# @return [String] The description of the template.
|
13
|
+
attr_reader :description
|
14
|
+
# @return [Integer] The number of times this template has been used.
|
15
|
+
attr_reader :usage_count
|
16
|
+
# @return [Discorb::User] The user who created this template.
|
17
|
+
attr_reader :creator
|
18
|
+
# @return [Time] The time this template was created.
|
19
|
+
attr_reader :created_at
|
20
|
+
# @return [Time] The time this template was last updated.
|
21
|
+
attr_reader :updated_at
|
22
|
+
# @return [Discorb::Guild] The guild where the template was created.
|
23
|
+
attr_reader :source_guild_id
|
24
|
+
# @return [Discorb::GuildTemplate::TemplateGuild] The guild where the template was created.
|
25
|
+
attr_reader :serialized_source_guild
|
26
|
+
alias content serialized_source_guild
|
27
|
+
# @return [Boolean] Whether this template is dirty.
|
28
|
+
attr_reader :is_dirty
|
29
|
+
alias dirty? is_dirty
|
30
|
+
|
31
|
+
# @!attribute [r] source_guild
|
32
|
+
# @macro client_cache
|
33
|
+
# @return [Discorb::Guild] The guild this template is based on.
|
34
|
+
# @return [nil] Client wasn't able to find the guild this template is based on.
|
35
|
+
|
36
|
+
# @!visibility private
|
37
|
+
def initialize(client, data)
|
38
|
+
@client = client
|
39
|
+
_set_data(data)
|
40
|
+
end
|
41
|
+
|
42
|
+
def source_guild
|
43
|
+
@client.guilds[@source_guild_id]
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Edit the template.
|
48
|
+
# @macro async
|
49
|
+
# @macro http
|
50
|
+
# @macro edit
|
51
|
+
#
|
52
|
+
# @param [String] name The new name of the template.
|
53
|
+
# @param [String] description The new description of the template.
|
54
|
+
#
|
55
|
+
def edit(name = nil, description = :unset)
|
56
|
+
Async do
|
57
|
+
payload = {}
|
58
|
+
payload[:name] = name if name
|
59
|
+
payload[:description] = description if description != :unset
|
60
|
+
@client.internet.patch("/guilds/#{@source_guild_id}/templates/#{@code}", payload).wait
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
alias modify edit
|
65
|
+
|
66
|
+
#
|
67
|
+
# Update the template.
|
68
|
+
# @macro async
|
69
|
+
# @macro http
|
70
|
+
#
|
71
|
+
def update
|
72
|
+
Async do
|
73
|
+
_resp, data = @client.internet.put("/guilds/#{@source_guild_id}/templates/#{@code}").wait
|
74
|
+
_set_data(data)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Delete the template.
|
80
|
+
# @macro async
|
81
|
+
# @macro http
|
82
|
+
#
|
83
|
+
def delete!
|
84
|
+
Async do
|
85
|
+
@client.internet.delete("/guilds/#{@source_guild_id}/templates/#{@code}").wait
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
alias destroy! delete!
|
90
|
+
|
91
|
+
#
|
92
|
+
# Represents a guild in guild template.
|
93
|
+
#
|
94
|
+
class TemplateGuild < DiscordModel
|
95
|
+
# @return [String] The name of the guild.
|
96
|
+
attr_reader :name
|
97
|
+
# @return [Integer] The AFK timeout of the guild.
|
98
|
+
attr_reader :afk_timeout
|
99
|
+
# @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::Role}] A dictionary of roles in the guild.
|
100
|
+
attr_reader :roles
|
101
|
+
# @return [Discorb::Guild::SystemChannelFlag] The flag for the system channel.
|
102
|
+
attr_reader :system_channel_flags
|
103
|
+
# @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::GuildChannel}] A dictionary of channels in the guild.
|
104
|
+
attr_reader :channels
|
105
|
+
# @return [String] The description of the guild.
|
106
|
+
attr_reader :description
|
107
|
+
# @return [Symbol] The preffered language of the guild.
|
108
|
+
# @note This modifies the language code, `-` will be replaced with `_`.
|
109
|
+
attr_reader :preferred_locale
|
110
|
+
# @return [:none, :low, :medium, :high, :very_high] The verification level of the guild.
|
111
|
+
attr_reader :verification_level
|
112
|
+
# @return [:all_messages, :only_mentions] The default message notification level of the guild.
|
113
|
+
attr_reader :default_message_notifications
|
114
|
+
# @return [:disabled_in_text, :members_without_roles, :all_members] The explict content filter level of the guild.
|
115
|
+
attr_reader :explicit_content_filter
|
116
|
+
# @return [Boolean] Whether the guild enabled the widget.
|
117
|
+
attr_reader :widget_enabled
|
118
|
+
alias widget_enabled? widget_enabled
|
119
|
+
|
120
|
+
# @!visibility private
|
121
|
+
def initialize(data)
|
122
|
+
@name = data[:name]
|
123
|
+
@description = data[:description]
|
124
|
+
@region = data[:region]
|
125
|
+
@verification_level = Discorb::Guild.mfa_levels[data[:verification_level]]
|
126
|
+
@default_message_notifications = Discorb::Guild.notification_levels[data[:default_message_notifications]]
|
127
|
+
@explicit_content_filter = Discorb::Guild.explicit_content_filter[data[:explicit_content_filter]]
|
128
|
+
@preferred_locale = data[:preferred_locale]
|
129
|
+
@afk_timeout = data[:afk_timeout]
|
130
|
+
@roles = data[:roles].map { |r| Role.new(r) }
|
131
|
+
@channels = data[:channels].map { |c| Channel.new(c) }
|
132
|
+
@system_channel_flags = Discorb::SystemChannelFlag.new(data[:system_channel_flags])
|
133
|
+
end
|
134
|
+
|
135
|
+
#
|
136
|
+
# Represents a role in guild template.
|
137
|
+
#
|
138
|
+
class Role < DiscordModel
|
139
|
+
# @return [String] The name of the role.
|
140
|
+
attr_reader :name
|
141
|
+
# @return [Discorb::Permission] The permissions of the role.
|
142
|
+
attr_reader :permissions
|
143
|
+
# @return [Discorb::Color] The color of the role.
|
144
|
+
attr_reader :color
|
145
|
+
|
146
|
+
# @!visibility private
|
147
|
+
def initialize(data)
|
148
|
+
@name = data[:name]
|
149
|
+
@permissions = Permission.new(data[:permissions])
|
150
|
+
@color = Color.new(data[:color])
|
151
|
+
@hoist = data[:hoist]
|
152
|
+
@mentionable = data[:mentionable]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# Represents a channel in guild template.
|
158
|
+
#
|
159
|
+
class Channel < DiscordModel
|
160
|
+
# @return [String] The name of the channel.
|
161
|
+
attr_reader :name
|
162
|
+
# @return [Integer] The position of the channel.
|
163
|
+
attr_reader :position
|
164
|
+
# @return [String] The type of the channel.
|
165
|
+
attr_reader :topic
|
166
|
+
# @return [Integer] The bitrate of the channel.
|
167
|
+
attr_reader :bitrate
|
168
|
+
# @return [Integer] The user limit of the channel.
|
169
|
+
attr_reader :user_limit
|
170
|
+
# @return [Boolean] Whether the channel is nsfw.
|
171
|
+
attr_reader :nsfw
|
172
|
+
# @return [Integer] The rate limit of the channel.
|
173
|
+
attr_reader :rate_limit_per_user
|
174
|
+
# @return [Class] The class of the channel.
|
175
|
+
attr_reader :type
|
176
|
+
|
177
|
+
# @!visibility private
|
178
|
+
def initialize(data)
|
179
|
+
@name = data[:name]
|
180
|
+
@position = data[:position]
|
181
|
+
@topic = data[:topic]
|
182
|
+
@bitrate = data[:bitrate]
|
183
|
+
@user_limit = data[:user_limit]
|
184
|
+
@nsfw = data[:nsfw]
|
185
|
+
@rate_limit_per_user = data[:rate_limit_per_user]
|
186
|
+
@parent_id = data[:parent_id]
|
187
|
+
@permission_overwrites = data[:permission_overwrites].map do |ow|
|
188
|
+
[Snowflake.new(ow[:id]), PermissionOverwrite.new(ow[:allow], ow[:deny])]
|
189
|
+
end.to_h
|
190
|
+
@type = Discorb::Channel.descendants.find { |c| c.channel_type == data[:type] }
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def _set_data(data)
|
198
|
+
@code = data[:code]
|
199
|
+
@name = data[:name]
|
200
|
+
@description = data[:description]
|
201
|
+
@usage_count = data[:usage_count]
|
202
|
+
@creator_id = Snowflake.new(data[:creator_id])
|
203
|
+
@creator = @client.users[@creator_id] || User.new(@client, data[:creator])
|
204
|
+
@created_at = Time.iso8601(data[:created_at])
|
205
|
+
@updated_at = Time.iso8601(data[:updated_at])
|
206
|
+
@source_guild_id = Snowflake.new(data[:source_guild_id])
|
207
|
+
@serialized_source_guild = data[:serialized_source_guild]
|
208
|
+
@is_dirty = data[:is_dirty]
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
require "mime/types"
|
5
|
+
|
6
|
+
module Discorb
|
7
|
+
#
|
8
|
+
# Represents an image.
|
9
|
+
#
|
10
|
+
class Image
|
11
|
+
#
|
12
|
+
# Initializes a new Image.
|
13
|
+
#
|
14
|
+
# @param [#read] source The source of the image.
|
15
|
+
# @param [String] type The MIME type of the image.
|
16
|
+
# @overload
|
17
|
+
# @param [String] source The file path of the source.
|
18
|
+
# @param [String] type The MIME type of the image.
|
19
|
+
#
|
20
|
+
def initialize(source, type = nil)
|
21
|
+
if source.respond_to?(:read)
|
22
|
+
@bytes = source.read
|
23
|
+
@type = type || MIME::Types.type_for(source.path).first.content_type
|
24
|
+
elsif ::File.exist?(source)
|
25
|
+
::File.open(source, "rb") do |file|
|
26
|
+
@bytes = file.read
|
27
|
+
end
|
28
|
+
@type = MIME::Types.type_for(source).first.to_s
|
29
|
+
else
|
30
|
+
raise ArgumentError, "Couldn't read file."
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Formats the image as a Discord style.
|
36
|
+
#
|
37
|
+
# @return [String] The image as a Discord style.
|
38
|
+
#
|
39
|
+
def to_s
|
40
|
+
"data:#{@type};base64,#{Base64.strict_encode64(@bytes)}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Discorb
|
4
|
+
#
|
5
|
+
# Represents a integration.
|
6
|
+
#
|
7
|
+
class Integration < DiscordModel
|
8
|
+
# @return [Discorb::Snowflake] The ID of the integration.
|
9
|
+
attr_reader :id
|
10
|
+
# @return [Symbol] The type of integration.
|
11
|
+
attr_reader :type
|
12
|
+
# @return [Boolean] Whether the integration is enabled.
|
13
|
+
attr_reader :enabled
|
14
|
+
alias enabled? enabled
|
15
|
+
# @return [Boolean] Whether the integration is syncing.
|
16
|
+
attr_reader :syncing
|
17
|
+
alias syncing? syncing
|
18
|
+
# @return [Boolean] Whether the integration is enabled emoticons.
|
19
|
+
attr_reader :enable_emoticons
|
20
|
+
alias enable_emoticons? enable_emoticons
|
21
|
+
# @return [:remove_role, :kick] The behavior of the integration when it expires.
|
22
|
+
attr_reader :expire_behavior
|
23
|
+
# @return [Integer] The grace period of the integration.
|
24
|
+
attr_reader :expire_grace_period
|
25
|
+
# @return [Discorb::User] The user for the integration.
|
26
|
+
attr_reader :user
|
27
|
+
# @return [Discorb::Integration::Account] The account for the integration.
|
28
|
+
attr_reader :account
|
29
|
+
# @return [Integer] The number of subscribers for the integration.
|
30
|
+
attr_reader :subscriber_count
|
31
|
+
# @return [Boolean] Whether the integration is revoked.
|
32
|
+
attr_reader :revoked
|
33
|
+
alias revoked? revoked
|
34
|
+
# @return [Discorb::Application] The application for the integration.
|
35
|
+
attr_reader :application
|
36
|
+
|
37
|
+
# @!attribute [r] guild
|
38
|
+
# @macro client_cache
|
39
|
+
# @return [Discorb::Guild] The guild this integration is in.
|
40
|
+
|
41
|
+
@expire_behavior = {
|
42
|
+
0 => :remove_role,
|
43
|
+
1 => :kick,
|
44
|
+
}
|
45
|
+
|
46
|
+
# @!visibility private
|
47
|
+
def initialize(client, data, guild_id, no_cache: false)
|
48
|
+
@client = client
|
49
|
+
@data = data
|
50
|
+
@guild_id = guild_id
|
51
|
+
_set_data(data)
|
52
|
+
guild.integrations[@id] = self unless no_cache
|
53
|
+
end
|
54
|
+
|
55
|
+
def guild
|
56
|
+
@client.guilds[@guild_id]
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Delete the integration.
|
61
|
+
#
|
62
|
+
# @param [String] reason The reason for deleting the integration.
|
63
|
+
#
|
64
|
+
def delete!(reason: nil)
|
65
|
+
Async do
|
66
|
+
@client.internet.delete("/guilds/#{@guild}/integrations/#{@id}", reason: reason).wait
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
alias destroy! delete!
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def _set_data(data)
|
75
|
+
@id = Snowflake.new(data[:id])
|
76
|
+
@type = data[:type].to_sym
|
77
|
+
@enabled = data[:enabled]
|
78
|
+
@syncing = data[:syncing]
|
79
|
+
@role_id = Snowflake.new(data[:role_id])
|
80
|
+
@enable_emoticons = data[:enable_emoticons]
|
81
|
+
@expire_behavior = self.class.expire_behavior[data[:expire_behavior]]
|
82
|
+
@expire_grace_period = data[:expire_grace_period]
|
83
|
+
@user = client.users[data[:user].to_i]
|
84
|
+
@account = Account.new(data[:account])
|
85
|
+
@subscriber_count = data[:subscriber_count]
|
86
|
+
@revoked = data[:revoked]
|
87
|
+
@application = Application.new(@client, data[:application])
|
88
|
+
end
|
89
|
+
|
90
|
+
class << self
|
91
|
+
# @!visibility private
|
92
|
+
attr_reader :expire_behavior
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Represents an account for an integration.
|
97
|
+
#
|
98
|
+
class Account < DiscordModel
|
99
|
+
# @return [String] The ID of the account.
|
100
|
+
attr_reader :id
|
101
|
+
# @return [String] The name of the account.
|
102
|
+
attr_reader :name
|
103
|
+
|
104
|
+
# @!visibility private
|
105
|
+
def initialize(data)
|
106
|
+
@id = data[:id]
|
107
|
+
@name = data[:name]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Discorb
|
4
|
+
# Represents intents.
|
5
|
+
class Intents
|
6
|
+
@intent_bits = {
|
7
|
+
guilds: 1 << 0,
|
8
|
+
members: 1 << 1,
|
9
|
+
bans: 1 << 2,
|
10
|
+
emojis: 1 << 3,
|
11
|
+
integrations: 1 << 4,
|
12
|
+
webhooks: 1 << 5,
|
13
|
+
invites: 1 << 6,
|
14
|
+
voice_states: 1 << 7,
|
15
|
+
presences: 1 << 8,
|
16
|
+
messages: 1 << 9,
|
17
|
+
reactions: 1 << 10,
|
18
|
+
typing: 1 << 11,
|
19
|
+
dm_messages: 1 << 12,
|
20
|
+
dm_reactions: 1 << 13,
|
21
|
+
dm_typing: 1 << 14,
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
#
|
25
|
+
# Create new intents object with default (no members and presence) intents.
|
26
|
+
#
|
27
|
+
# @param guilds [Boolean] Whether guild related events are enabled.
|
28
|
+
# @param members [Boolean] Whether guild members related events are enabled.
|
29
|
+
# @note You must enable members intent on developers portal.
|
30
|
+
# @param bans [Boolean] Whether guild ban related events are enabled.
|
31
|
+
# @param emojis [Boolean] Whether guild emojis related events are enabled.
|
32
|
+
# @param integrations [Boolean] Whether guild integration related events are enabled.
|
33
|
+
# @param webhooks [Boolean] Whether guild webhooks related events are enabled.
|
34
|
+
# @param invites [Boolean] Whether guild invite related events are enabled.
|
35
|
+
# @param voice_states [Boolean] Whether guild voice state related events are enabled.
|
36
|
+
# @param presences [Boolean] Whether guild presences related events are enabled.
|
37
|
+
# @note You must enable members intent on developers portal.
|
38
|
+
# @param messages [Boolean] Whether guild messages related events are enabled.
|
39
|
+
# @param reactions [Boolean] Whether guild reaction related events are enabled.
|
40
|
+
# @param dm_messages [Boolean] Whether dm messages related events are enabled.
|
41
|
+
# @param dm_reactions [Boolean] Whether dm reactions related events are enabled.
|
42
|
+
# @param dm_typing [Boolean] Whether dm typing related events are enabled.
|
43
|
+
#
|
44
|
+
def initialize(guilds: true,
|
45
|
+
members: false,
|
46
|
+
bans: true,
|
47
|
+
emojis: true,
|
48
|
+
integrations: true,
|
49
|
+
webhooks: true,
|
50
|
+
invites: true,
|
51
|
+
voice_states: true,
|
52
|
+
presences: false,
|
53
|
+
messages: true,
|
54
|
+
reactions: true,
|
55
|
+
typing: true,
|
56
|
+
dm_messages: true,
|
57
|
+
dm_reactions: true,
|
58
|
+
dm_typing: true)
|
59
|
+
@raw_value = {
|
60
|
+
guilds: guilds,
|
61
|
+
members: members,
|
62
|
+
bans: bans,
|
63
|
+
emojis: emojis,
|
64
|
+
integrations: integrations,
|
65
|
+
webhooks: webhooks,
|
66
|
+
invites: invites,
|
67
|
+
voice_states: voice_states,
|
68
|
+
presences: presences,
|
69
|
+
messages: messages,
|
70
|
+
reactions: reactions,
|
71
|
+
typing: typing,
|
72
|
+
dm_messages: dm_messages,
|
73
|
+
dm_reactions: dm_reactions,
|
74
|
+
dm_typing: dm_typing,
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def method_missing(name, args = nil)
|
79
|
+
if @raw_value.key?(name)
|
80
|
+
@raw_value[name]
|
81
|
+
elsif name.end_with?("=") && @raw_value.key?(name[0..-2].to_sym)
|
82
|
+
raise ArgumentError, "true/false expected" if (!args.is_a? TrueClass) || args.is_a?(FalseClass)
|
83
|
+
|
84
|
+
@raw_value[name[0..-2].to_sym] = args
|
85
|
+
else
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def respond_to_missing?(sym, include_private)
|
91
|
+
@raw_value.key?(name) ? true : super
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns value of the intent.
|
95
|
+
# @return [Integer] The value of the intent.
|
96
|
+
def value
|
97
|
+
res = 0
|
98
|
+
self.class.intent_bits.each do |intent, bit|
|
99
|
+
res += bit if @raw_value[intent]
|
100
|
+
end
|
101
|
+
res
|
102
|
+
end
|
103
|
+
|
104
|
+
def inspect
|
105
|
+
"#<#{self.class} value=#{value}>"
|
106
|
+
end
|
107
|
+
|
108
|
+
class << self
|
109
|
+
# Create new intent object from raw value.
|
110
|
+
# @param value [Integer] The value of the intent.
|
111
|
+
def from_value(value)
|
112
|
+
raw_value = {}
|
113
|
+
@intent_bits.each do |intent, bit|
|
114
|
+
raw_value[intent] = value & bit != 0
|
115
|
+
end
|
116
|
+
new(**raw_value)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Create new intent object with default values.
|
120
|
+
def default
|
121
|
+
from_value(32_509)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Create new intent object with all intents.
|
125
|
+
def all
|
126
|
+
from_value(32_767)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Create new intent object with no intents.
|
130
|
+
def none
|
131
|
+
from_value(0)
|
132
|
+
end
|
133
|
+
|
134
|
+
attr_reader :intent_bits
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|