rubycord 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rubycord/allowed_mentions.rb +34 -0
  3. data/lib/rubycord/api/application.rb +200 -0
  4. data/lib/rubycord/api/channel.rb +597 -0
  5. data/lib/rubycord/api/interaction.rb +52 -0
  6. data/lib/rubycord/api/invite.rb +42 -0
  7. data/lib/rubycord/api/server.rb +557 -0
  8. data/lib/rubycord/api/user.rb +153 -0
  9. data/lib/rubycord/api/webhook.rb +138 -0
  10. data/lib/rubycord/api.rb +356 -0
  11. data/lib/rubycord/await.rb +49 -0
  12. data/lib/rubycord/bot.rb +1757 -0
  13. data/lib/rubycord/cache.rb +259 -0
  14. data/lib/rubycord/colour_rgb.rb +41 -0
  15. data/lib/rubycord/commands/command_bot.rb +519 -0
  16. data/lib/rubycord/commands/container.rb +110 -0
  17. data/lib/rubycord/commands/events.rb +9 -0
  18. data/lib/rubycord/commands/parser.rb +325 -0
  19. data/lib/rubycord/commands/rate_limiter.rb +142 -0
  20. data/lib/rubycord/container.rb +753 -0
  21. data/lib/rubycord/data/activity.rb +269 -0
  22. data/lib/rubycord/data/application.rb +48 -0
  23. data/lib/rubycord/data/attachment.rb +109 -0
  24. data/lib/rubycord/data/audit_logs.rb +343 -0
  25. data/lib/rubycord/data/channel.rb +996 -0
  26. data/lib/rubycord/data/component.rb +227 -0
  27. data/lib/rubycord/data/embed.rb +249 -0
  28. data/lib/rubycord/data/emoji.rb +80 -0
  29. data/lib/rubycord/data/integration.rb +120 -0
  30. data/lib/rubycord/data/interaction.rb +798 -0
  31. data/lib/rubycord/data/invite.rb +135 -0
  32. data/lib/rubycord/data/member.rb +370 -0
  33. data/lib/rubycord/data/message.rb +412 -0
  34. data/lib/rubycord/data/overwrite.rb +106 -0
  35. data/lib/rubycord/data/profile.rb +89 -0
  36. data/lib/rubycord/data/reaction.rb +31 -0
  37. data/lib/rubycord/data/recipient.rb +32 -0
  38. data/lib/rubycord/data/role.rb +246 -0
  39. data/lib/rubycord/data/server.rb +1002 -0
  40. data/lib/rubycord/data/user.rb +261 -0
  41. data/lib/rubycord/data/voice_region.rb +43 -0
  42. data/lib/rubycord/data/voice_state.rb +39 -0
  43. data/lib/rubycord/data/webhook.rb +232 -0
  44. data/lib/rubycord/data.rb +40 -0
  45. data/lib/rubycord/errors.rb +737 -0
  46. data/lib/rubycord/events/await.rb +46 -0
  47. data/lib/rubycord/events/bans.rb +58 -0
  48. data/lib/rubycord/events/channels.rb +186 -0
  49. data/lib/rubycord/events/generic.rb +126 -0
  50. data/lib/rubycord/events/guilds.rb +191 -0
  51. data/lib/rubycord/events/interactions.rb +480 -0
  52. data/lib/rubycord/events/invites.rb +123 -0
  53. data/lib/rubycord/events/lifetime.rb +29 -0
  54. data/lib/rubycord/events/members.rb +91 -0
  55. data/lib/rubycord/events/message.rb +337 -0
  56. data/lib/rubycord/events/presence.rb +127 -0
  57. data/lib/rubycord/events/raw.rb +45 -0
  58. data/lib/rubycord/events/reactions.rb +156 -0
  59. data/lib/rubycord/events/roles.rb +86 -0
  60. data/lib/rubycord/events/threads.rb +94 -0
  61. data/lib/rubycord/events/typing.rb +70 -0
  62. data/lib/rubycord/events/voice_server_update.rb +45 -0
  63. data/lib/rubycord/events/voice_state_update.rb +103 -0
  64. data/lib/rubycord/events/webhooks.rb +62 -0
  65. data/lib/rubycord/gateway.rb +867 -0
  66. data/lib/rubycord/id_object.rb +37 -0
  67. data/lib/rubycord/light/data.rb +60 -0
  68. data/lib/rubycord/light/integrations.rb +71 -0
  69. data/lib/rubycord/light/light_bot.rb +56 -0
  70. data/lib/rubycord/light.rb +6 -0
  71. data/lib/rubycord/logger.rb +118 -0
  72. data/lib/rubycord/paginator.rb +55 -0
  73. data/lib/rubycord/permissions.rb +251 -0
  74. data/lib/rubycord/version.rb +5 -0
  75. data/lib/rubycord/voice/encoder.rb +113 -0
  76. data/lib/rubycord/voice/network.rb +366 -0
  77. data/lib/rubycord/voice/sodium.rb +96 -0
  78. data/lib/rubycord/voice/voice_bot.rb +408 -0
  79. data/lib/rubycord/webhooks/builder.rb +100 -0
  80. data/lib/rubycord/webhooks/client.rb +132 -0
  81. data/lib/rubycord/webhooks/embeds.rb +248 -0
  82. data/lib/rubycord/webhooks/modal.rb +78 -0
  83. data/lib/rubycord/webhooks/version.rb +7 -0
  84. data/lib/rubycord/webhooks/view.rb +192 -0
  85. data/lib/rubycord/webhooks.rb +12 -0
  86. data/lib/rubycord/websocket.rb +70 -0
  87. data/lib/rubycord.rb +140 -0
  88. metadata +231 -0
@@ -0,0 +1,246 @@
1
+ module Rubycord
2
+ # A Discord role that contains permissions and applies to certain users
3
+ class Role
4
+ include IDObject
5
+
6
+ # @return [Permissions] this role's permissions.
7
+ attr_reader :permissions
8
+
9
+ # @return [String] this role's name ("new role" if it hasn't been changed)
10
+ attr_reader :name
11
+
12
+ # @return [Server] the server this role belongs to
13
+ attr_reader :server
14
+
15
+ # @return [true, false] whether or not this role should be displayed separately from other users
16
+ attr_reader :hoist
17
+
18
+ # @return [true, false] whether or not this role is managed by an integration or a bot
19
+ attr_reader :managed
20
+ alias_method :managed?, :managed
21
+
22
+ # @return [true, false] whether this role can be mentioned using a role mention
23
+ attr_reader :mentionable
24
+ alias_method :mentionable?, :mentionable
25
+
26
+ # @return [ColourRGB] the role colour
27
+ attr_reader :colour
28
+ alias_method :color, :colour
29
+
30
+ # @return [Integer] the position of this role in the hierarchy
31
+ attr_reader :position
32
+
33
+ # @return [String, nil] The icon hash for this role.
34
+ attr_reader :icon
35
+
36
+ # @return [Tags, nil] The role tags
37
+ attr_reader :tags
38
+
39
+ # Wrapper for the role tags
40
+ class Tags
41
+ # @return [Integer, nil] The ID of the bot this role belongs to
42
+ attr_reader :bot_id
43
+
44
+ # @return [Integer, nil] The ID of the integration this role belongs to
45
+ attr_reader :integration_id
46
+
47
+ # @return [true, false] Whether this is the guild's Booster role
48
+ attr_reader :premium_subscriber
49
+
50
+ # @return [Integer, nil] The id of this role's subscription sku and listing
51
+ attr_reader :subscription_listing_id
52
+
53
+ # @return [true, false] Whether this role is available for purchase
54
+ attr_reader :available_for_purchase
55
+
56
+ # @return [true, false] Whether this role is a guild's linked role
57
+ attr_reader :guild_connections
58
+
59
+ def initialize(data)
60
+ @bot_id = data["bot_id"]&.resolve_id
61
+ @integration_id = data["integration_id"]&.resolve_id
62
+ @premium_subscriber = data.key?("premium_subscriber")
63
+ @subscription_listing_id = data["subscription_listing_id"]&.resolve_id
64
+ @available_for_purchase = data.key?("available_for_purchase")
65
+ @guild_connections = data.key?("guild_connections")
66
+ end
67
+ end
68
+
69
+ # This class is used internally as a wrapper to a Role object that allows easy writing of permission data.
70
+ class RoleWriter
71
+ # @!visibility private
72
+ def initialize(role, token)
73
+ @role = role
74
+ @token = token
75
+ end
76
+
77
+ # Write the specified permission data to the role, without updating the permission cache
78
+ # @param bits [Integer] The packed permissions to write.
79
+ def write(bits)
80
+ @role.send(:packed=, bits, false)
81
+ end
82
+
83
+ # The inspect method is overridden, in this case to prevent the token being leaked
84
+ def inspect
85
+ "<RoleWriter role=#{@role} token=...>"
86
+ end
87
+ end
88
+
89
+ # @!visibility private
90
+ def initialize(data, bot, server = nil)
91
+ @bot = bot
92
+ @server = server
93
+ @permissions = Permissions.new(data["permissions"], RoleWriter.new(self, @bot.token))
94
+ @name = data["name"]
95
+ @id = data["id"].to_i
96
+
97
+ @position = data["position"]
98
+
99
+ @hoist = data["hoist"]
100
+ @mentionable = data["mentionable"]
101
+ @managed = data["managed"]
102
+
103
+ @colour = ColourRGB.new(data["color"])
104
+
105
+ @icon = data["icon"]
106
+
107
+ @tags = Tags.new(data["tags"]) if data["tags"]
108
+ end
109
+
110
+ # @return [String] a string that will mention this role, if it is mentionable.
111
+ def mention
112
+ "<@&#{@id}>"
113
+ end
114
+
115
+ # @return [Array<Member>] an array of members who have this role.
116
+ # @note This requests a member chunk if it hasn't for the server before, which may be slow initially
117
+ def members
118
+ @server.members.select { |m| m.role? self }
119
+ end
120
+
121
+ alias_method :users, :members
122
+
123
+ # Updates the data cache from another Role object
124
+ # @note For internal use only
125
+ # @!visibility private
126
+ def update_from(other)
127
+ @permissions = other.permissions
128
+ @name = other.name
129
+ @hoist = other.hoist
130
+ @colour = other.colour
131
+ @position = other.position
132
+ @managed = other.managed
133
+ @icon = other.icon
134
+ end
135
+
136
+ # Updates the data cache from a hash containing data
137
+ # @note For internal use only
138
+ # @!visibility private
139
+ def update_data(new_data)
140
+ @name = new_data[:name] || new_data["name"] || @name
141
+ @hoist = new_data["hoist"] unless new_data["hoist"].nil?
142
+ @hoist = new_data[:hoist] unless new_data[:hoist].nil?
143
+ @colour = new_data[:colour] || (new_data["color"] ? ColourRGB.new(new_data["color"]) : @colour)
144
+ end
145
+
146
+ # Sets the role name to something new
147
+ # @param name [String] The name that should be set
148
+ def name=(name)
149
+ update_role_data(name: name)
150
+ end
151
+
152
+ # Changes whether or not this role is displayed at the top of the user list
153
+ # @param hoist [true, false] The value it should be changed to
154
+ def hoist=(hoist)
155
+ update_role_data(hoist: hoist)
156
+ end
157
+
158
+ # Changes whether or not this role can be mentioned
159
+ # @param mentionable [true, false] The value it should be changed to
160
+ def mentionable=(mentionable)
161
+ update_role_data(mentionable: mentionable)
162
+ end
163
+
164
+ # Sets the role colour to something new
165
+ # @param colour [ColourRGB] The new colour
166
+ def colour=(colour)
167
+ update_role_data(colour: colour)
168
+ end
169
+
170
+ # Upload a role icon for servers with the ROLE_ICONS feature.
171
+ # @param file [File]
172
+ def icon=(file)
173
+ update_role_data(icon: file)
174
+ end
175
+
176
+ # @param format ['webp', 'png', 'jpeg']
177
+ # @return [String] URL to the icon on Discord's CDN.
178
+ def icon_url(format = "webp")
179
+ return nil unless @icon
180
+
181
+ Rubycord::API.role_icon_url(@id, @icon, format)
182
+ end
183
+
184
+ alias_method :color=, :colour=
185
+
186
+ # Changes this role's permissions to a fixed bitfield. This allows setting multiple permissions at once with just
187
+ # one API call.
188
+ #
189
+ # Information on how this bitfield is structured can be found at
190
+ # https://discord.com/developers/docs/topics/permissions.
191
+ # @example Remove all permissions from a role
192
+ # role.packed = 0
193
+ # @param packed [Integer] A bitfield with the desired permissions value.
194
+ # @param update_perms [true, false] Whether the internal data should also be updated. This should always be true
195
+ # when calling externally.
196
+ def packed=(packed, update_perms = true)
197
+ update_role_data(permissions: packed)
198
+ @permissions.bits = packed if update_perms
199
+ end
200
+
201
+ # Moves this role above another role in the list.
202
+ # @param other [Role, String, Integer, nil] The role, or its ID, above which this role should be moved. If it is `nil`,
203
+ # the role will be moved above the @everyone role.
204
+ # @return [Integer] the new position of this role
205
+ def sort_above(other = nil)
206
+ other = @server.role(other.resolve_id) if other
207
+ roles = @server.roles.sort_by(&:position)
208
+ roles.delete_at(@position)
209
+
210
+ index = other ? roles.index { |role| role.id == other.id } + 1 : 1
211
+ roles.insert(index, self)
212
+
213
+ updated_roles = roles.map.with_index { |role, position| {id: role.id, position: position} }
214
+ @server.update_role_positions(updated_roles)
215
+ index
216
+ end
217
+
218
+ alias_method :move_above, :sort_above
219
+
220
+ # Deletes this role. This cannot be undone without recreating the role!
221
+ # @param reason [String] the reason for this role's deletion
222
+ def delete(reason = nil)
223
+ API::Server.delete_role(@bot.token, @server.id, @id, reason)
224
+ @server.delete_role(@id)
225
+ end
226
+
227
+ # The inspect method is overwritten to give more useful output
228
+ def inspect
229
+ "<Role name=#{@name} permissions=#{@permissions.inspect} hoist=#{@hoist} colour=#{@colour.inspect} server=#{@server.inspect} position=#{@position} mentionable=#{@mentionable}>"
230
+ end
231
+
232
+ private
233
+
234
+ def update_role_data(new_data)
235
+ API::Server.update_role(@bot.token, @server.id, @id,
236
+ new_data[:name] || @name,
237
+ (new_data[:colour] || @colour).combined,
238
+ new_data[:hoist].nil? ? @hoist : new_data[:hoist],
239
+ new_data[:mentionable].nil? ? @mentionable : new_data[:mentionable],
240
+ new_data[:permissions] || @permissions.bits,
241
+ nil,
242
+ new_data.key?(:icon) ? new_data[:icon] : :undef)
243
+ update_data(new_data)
244
+ end
245
+ end
246
+ end