discorb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +56 -0
  3. data/.yardopts +6 -0
  4. data/Changelog.md +5 -0
  5. data/Gemfile +23 -0
  6. data/Gemfile.lock +70 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +53 -0
  9. data/Rakefile +46 -0
  10. data/bin/console +15 -0
  11. data/bin/setup +8 -0
  12. data/discorb.gemspec +37 -0
  13. data/docs/Examples.md +26 -0
  14. data/docs/events.md +480 -0
  15. data/docs/voice_events.md +283 -0
  16. data/examples/components/authorization_button.rb +43 -0
  17. data/examples/components/select_menu.rb +61 -0
  18. data/examples/extension/main.rb +12 -0
  19. data/examples/extension/message_expander.rb +41 -0
  20. data/examples/simple/eval.rb +32 -0
  21. data/examples/simple/ping_pong.rb +16 -0
  22. data/examples/simple/rolepanel.rb +65 -0
  23. data/examples/simple/wait_for_message.rb +30 -0
  24. data/lib/discorb/application.rb +157 -0
  25. data/lib/discorb/asset.rb +57 -0
  26. data/lib/discorb/audit_logs.rb +323 -0
  27. data/lib/discorb/channel.rb +1101 -0
  28. data/lib/discorb/client.rb +363 -0
  29. data/lib/discorb/color.rb +173 -0
  30. data/lib/discorb/common.rb +123 -0
  31. data/lib/discorb/components.rb +290 -0
  32. data/lib/discorb/dictionary.rb +119 -0
  33. data/lib/discorb/embed.rb +345 -0
  34. data/lib/discorb/emoji.rb +218 -0
  35. data/lib/discorb/emoji_table.rb +3799 -0
  36. data/lib/discorb/error.rb +98 -0
  37. data/lib/discorb/event.rb +35 -0
  38. data/lib/discorb/extend.rb +18 -0
  39. data/lib/discorb/extension.rb +54 -0
  40. data/lib/discorb/file.rb +69 -0
  41. data/lib/discorb/flag.rb +109 -0
  42. data/lib/discorb/gateway.rb +967 -0
  43. data/lib/discorb/gateway_requests.rb +47 -0
  44. data/lib/discorb/guild.rb +1244 -0
  45. data/lib/discorb/guild_template.rb +211 -0
  46. data/lib/discorb/image.rb +43 -0
  47. data/lib/discorb/integration.rb +111 -0
  48. data/lib/discorb/intents.rb +137 -0
  49. data/lib/discorb/interaction.rb +333 -0
  50. data/lib/discorb/internet.rb +285 -0
  51. data/lib/discorb/invite.rb +145 -0
  52. data/lib/discorb/log.rb +70 -0
  53. data/lib/discorb/member.rb +232 -0
  54. data/lib/discorb/message.rb +583 -0
  55. data/lib/discorb/modules.rb +138 -0
  56. data/lib/discorb/permission.rb +270 -0
  57. data/lib/discorb/presence.rb +308 -0
  58. data/lib/discorb/reaction.rb +48 -0
  59. data/lib/discorb/role.rb +189 -0
  60. data/lib/discorb/sticker.rb +157 -0
  61. data/lib/discorb/user.rb +163 -0
  62. data/lib/discorb/utils.rb +16 -0
  63. data/lib/discorb/voice_state.rb +251 -0
  64. data/lib/discorb/webhook.rb +420 -0
  65. data/lib/discorb.rb +51 -0
  66. metadata +120 -0
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a reaction to a message.
6
+ #
7
+ class Reaction < DiscordModel
8
+ # @return [Integer] The number of users that have reacted with this emoji.
9
+ attr_reader :count
10
+ # @return [Discorb::Emoji] The emoji that was reacted with.
11
+ attr_reader :emoji
12
+ # @return [Discorb::Message] The message that this reaction is on.
13
+ attr_reader :message
14
+ # @return [Boolean] Whether client user reacted with this emoji.
15
+ attr_reader :me
16
+ alias me? me
17
+ alias reacted? me
18
+
19
+ # @!visibility private
20
+ def initialize(message, data)
21
+ @message = message
22
+ _set_data(data)
23
+ end
24
+
25
+ #
26
+ # Fetch the user that reacted with this emoji.
27
+ #
28
+ # @param (see Message#fetch_reacted_users)
29
+ #
30
+ # @return [Array<Discorb::User>] The users that reacted with this emoji.
31
+ #
32
+ def fetch_users(...)
33
+ message.fetch_reacted_users(@emoji, ...)
34
+ end
35
+
36
+ private
37
+
38
+ def _set_data(data)
39
+ @count = data[:count]
40
+ @me = data[:me]
41
+ @emoji = if data[:emoji][:id].nil?
42
+ UnicodeEmoji.new(data[:emoji][:name])
43
+ else
44
+ PartialEmoji.new(data[:emoji])
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a role in the guild.
6
+ #
7
+ class Role < DiscordModel
8
+ # @return [Discorb::Snowflake] The ID of the role.
9
+ attr_reader :id
10
+ # @return [String] The name of the role.
11
+ attr_reader :name
12
+ # @return [Discorb::Color] The color of the role.
13
+ attr_reader :color
14
+ # @return [Discorb::Permission] The permissions of the role.
15
+ attr_reader :permissions
16
+ # @return [Integer] The position of the role.
17
+ attr_reader :position
18
+ # @return [Discorb::Guild] The guild this role belongs to.
19
+ attr_reader :guild
20
+ # @return [Boolean] Whether the role is hoisted.
21
+ attr_reader :hoist
22
+ alias hoist? hoist
23
+ # @return [Boolean] Whether the role is managed.
24
+ attr_reader :managed
25
+ alias managed? managed
26
+ # @return [Boolean] Whether the role is a default role.
27
+ attr_reader :mentionable
28
+ alias mentionable? mentionable
29
+
30
+ # @!attribute [r] mention
31
+ # @return [String] The mention of the role.
32
+ # @!attribute [r] color?
33
+ # @return [Boolean] Whether the role has a color.
34
+ # @!attribute [r] tag
35
+ # @return [Discorb::Role::Tag] The tag of the role.
36
+
37
+ include Comparable
38
+
39
+ # @!visibility private
40
+ def initialize(client, guild, data)
41
+ @client = client
42
+ @guild = guild
43
+ @data = {}
44
+ _set_data(data)
45
+ end
46
+
47
+ #
48
+ # Compares two roles by their position.
49
+ #
50
+ # @param [Discorb::Role] other The role to compare to.
51
+ #
52
+ # @return [Integer] -1 if the other role is higher, 0 if they are equal, 1 if the other role is lower.
53
+ #
54
+ def <=>(other)
55
+ return false unless other.is_a?(Role)
56
+
57
+ @position <=> other.position
58
+ end
59
+
60
+ #
61
+ # Formats the role as a string.
62
+ #
63
+ # @return [String] The formatted string.
64
+ #
65
+ def to_s
66
+ "@#{@name}"
67
+ end
68
+
69
+ def mention
70
+ "<@&#{@id}>"
71
+ end
72
+
73
+ def color?
74
+ @color != 0
75
+ end
76
+
77
+ def inspect
78
+ "#<#{self.class} @#{@name} id=#{@id}>"
79
+ end
80
+
81
+ #
82
+ # Moves the role to a new position.
83
+ # @macro async
84
+ # @macro http
85
+ #
86
+ # @param [Integer] position The new position.
87
+ # @param [String] reason The reason for moving the role.
88
+ #
89
+ def move(position, reason: nil)
90
+ Async do
91
+ @client.internet.patch("/guilds/#{@guild_id}/roles", { id: @id, position: position }, reason: reason).wait
92
+ end
93
+ end
94
+
95
+ #
96
+ # Edits the role.
97
+ # @macro async
98
+ # @macro http
99
+ # @macro edit
100
+ #
101
+ # @param [String] name The new name of the role.
102
+ # @param [Integer] position The new position of the role.
103
+ # @param [Discorb::Color] color The new color of the role.
104
+ # @param [Boolean] hoist Whether the role should be hoisted.
105
+ # @param [Boolean] mentionable Whether the role should be mentionable.
106
+ # @param [String] reason The reason for editing the role.
107
+ #
108
+ def edit(name: :unset, position: :unset, color: :unset, hoist: :unset, mentionable: :unset, reason: nil)
109
+ Async do
110
+ payload = {}
111
+ payload[:name] = name if name != :unset
112
+ payload[:position] = position if position != :unset
113
+ payload[:color] = color.to_i if color != :unset
114
+ payload[:hoist] = hoist if hoist != :unset
115
+ payload[:mentionable] = mentionable if mentionable != :unset
116
+ @client.internet.patch("/guilds/#{@guild_id}/roles/#{@id}", payload, reason: reason).wait
117
+ end
118
+ end
119
+
120
+ alias modify edit
121
+
122
+ #
123
+ # Deletes the role.
124
+ #
125
+ # @param [String] reason The reason for deleting the role.
126
+ #
127
+ def delete!(reason: nil)
128
+ Async do
129
+ @client.internet.delete("/guilds/#{@guild_id}/roles/#{@id}", reason: reason).wait
130
+ end
131
+ end
132
+
133
+ alias destroy! delete!
134
+
135
+ def tag
136
+ Tag.new(@tags)
137
+ end
138
+
139
+ alias tags tag
140
+
141
+ #
142
+ # Represents a tag of a role.
143
+ #
144
+ class Tag < DiscordModel
145
+ # @return [Discorb::Snowflake] The ID of the bot that owns the role.
146
+ attr_reader :bot_id
147
+ # @return [Discorb::Snowflake] The ID of the integration.
148
+ attr_reader :integration_id
149
+ # @return [Boolean] Whether the tag is a premium subscriber role.
150
+ attr_reader :premium_subscriber
151
+ alias premium_subscriber? premium_subscriber
152
+ # @!attribute [r] bot?
153
+ # @return [Boolean] Whether the role is a bot role.
154
+ # @!attribute [r] integration?
155
+ # @return [Boolean] Whether the role is an integration role.
156
+
157
+ # @!visibility private
158
+ def initialize(data)
159
+ @bot_id = Snowflake.new(data[:bot_id])
160
+ @integration_id = Snowflake.new(data[:integration_id])
161
+ @premium_subscriber = data.key?(:premium_subscriber)
162
+ end
163
+
164
+ def bot?
165
+ !@bot_id.nil?
166
+ end
167
+
168
+ def integration?
169
+ !@integration_id.nil?
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def _set_data(data)
176
+ @id = Snowflake.new(data[:id])
177
+ @name = data[:name]
178
+ @color = Color.new(data[:color])
179
+ @hoist = data[:hoist]
180
+ @position = data[:position]
181
+ @permissions = Permission.new(data[:permissions].to_i)
182
+ @managed = data[:managed]
183
+ @mentionable = data[:mentionable]
184
+ @tags = data[:tags] || {}
185
+ @guild.roles[@id] = self unless data[:no_cache]
186
+ @data.update(data)
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a sticker.
6
+ #
7
+ class Sticker < DiscordModel
8
+ # @return [Discorb::Snowflake] The ID of the sticker.
9
+ attr_reader :id
10
+ # @return [String] The name of the sticker.
11
+ attr_reader :name
12
+ # @return [Array<String>] The tags of the sticker.
13
+ attr_reader :tags
14
+ # @return [:official, :guild] The type of sticker.
15
+ attr_reader :type
16
+ # @return [:png, :apng, :lottie] The format of the sticker.
17
+ attr_reader :format
18
+ # @return [String] The URL of the sticker.
19
+ attr_reader :description
20
+ # @return [Discorb::Sticker] The ID of the sticker pack.
21
+ attr_reader :pack_id
22
+ # @return [Integer] The sort value of the sticker.
23
+ attr_reader :sort_value
24
+ # @return [Discorb::Snowflake] The ID of the guild the sticker is in.
25
+ attr_reader :guild_id
26
+ # @return [Discorb::User] The user who created the sticker.
27
+ attr_reader :user
28
+ # @return [Boolean] Whether the sticker is available.
29
+ attr_reader :available
30
+ alias available? available
31
+
32
+ @sticker_type = {
33
+ 1 => :official,
34
+ 2 => :guild,
35
+ }.freeze
36
+ @sticker_format = {
37
+ 1 => :png,
38
+ 2 => :apng,
39
+ 3 => :lottie,
40
+ }
41
+ # @!visibility private
42
+ def initialize(client, data)
43
+ @client = client
44
+ _set_data(data)
45
+ end
46
+
47
+ #
48
+ # Represents a sticker of guilds.
49
+ #
50
+ class GuildSticker < Sticker
51
+ # @!attribute [r] guild
52
+ # @macro client_cache
53
+ # @return [Discorb::Guild] The guild the sticker is in.
54
+
55
+ def guild
56
+ @client.guilds[@guild_id]
57
+ end
58
+
59
+ #
60
+ # Edits the sticker.
61
+ # @macro async
62
+ # @macro http
63
+ # @macro edit
64
+ #
65
+ # @param [String] name The new name of the sticker.
66
+ # @param [String] description The new description of the sticker.
67
+ # @param [Discorb::Emoji] tag The new tags of the sticker.
68
+ # @param [String] reason The reason for the edit.
69
+ #
70
+ def edit(name: :unset, description: :unset, tag: :unset, reason: :unset)
71
+ Async do
72
+ payload = {}
73
+ payload[:name] = name unless name == :unset
74
+ payload[:description] = description unless description == :unset
75
+ payload[:tags] = tag.name unless tag == :unset
76
+ @client.internet.patch("/guilds/#{@guild_id}/stickers/#{@id}", payload, audit_log_reason: reason).wait
77
+ end
78
+ end
79
+
80
+ alias modify edit
81
+
82
+ #
83
+ # Deletes the sticker.
84
+ # @macro async
85
+ # @macro http
86
+ #
87
+ # @param [String] reason The reason for the deletion.
88
+ #
89
+ def delete!(reason: nil)
90
+ Async do
91
+ @client.internet.delete("/guilds/#{@guild_id}/stickers/#{@id}", audit_log_reason: reason).wait
92
+ end
93
+ end
94
+
95
+ alias destroy! delete!
96
+ end
97
+
98
+ #
99
+ # Represents a sticker pack.
100
+ #
101
+ class Pack < DiscordModel
102
+ # @return [Discorb::Snowflake] The ID of the sticker pack.
103
+ attr_reader :id
104
+ # @return [String] The name of the sticker pack.
105
+ attr_reader :name
106
+ # @return [Discorb::Snowflake] The ID of the SKU.
107
+ attr_reader :sku_id
108
+ # @return [Discorb::Snowflake] The cover sticker of the pack.
109
+ attr_reader :cover_sticker_id
110
+ # @return [String] The description of the pack.
111
+ attr_reader :description
112
+ # @return [Discorb::Store::SKU] The banner asset ID of the pack.
113
+ attr_reader :banner_asset_id
114
+ # @return [Array<Discorb::Sticker>] The stickers in the pack.
115
+ attr_reader :stickers
116
+ # @return [Discorb::Asset] The banner of the pack.
117
+ attr_reader :banner
118
+
119
+ # @!visibility private
120
+ def initialize(client, data)
121
+ @client = client
122
+ @id = Snowflake.new(data[:id])
123
+ @name = data[:name]
124
+ @sku_id = Snowflake.new(data[:sku_id])
125
+ @cover_sticker_id = Snowflake.new(data[:cover_sticker_id])
126
+ @description = data[:description]
127
+ @banner_asset_id = Snowflake.new(data[:banner_asset_id])
128
+ @banner = Asset.new(self, data[:banner_asset_id], path: "app-assets/710982414301790216/store")
129
+ @stickers = data[:stickers].map { |s| Sticker.new(@client, s) }
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def _set_data(data)
136
+ @id = Snowflake.new(data[:id])
137
+ @name = data[:name]
138
+ @tags = data[:tags].split(",")
139
+ @type = self.class.sticker_type[data[:type]]
140
+ @format = self.class.sticker_format[data[:format]]
141
+ @description = data[:description]
142
+ @available = data[:available]
143
+ if @type == :official
144
+ @pack_id = Snowflake.new(data[:guild_id])
145
+ @sort_value = data[:sort_value]
146
+ else
147
+ @guild_id = Snowflake.new(data[:guild_id])
148
+ @user = data[:user] && (@client.users[data[:user][:id]] || User.new(@client, data[:user]))
149
+ end
150
+ end
151
+
152
+ class << self
153
+ # @!visibility private
154
+ attr_reader :sticker_type, :sticker_format
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a user of discord.
6
+ #
7
+ class User < DiscordModel
8
+ # @return [Boolean] Whether the user is verified.
9
+ attr_reader :verified
10
+ # @return [String] The user's username.
11
+ attr_reader :username
12
+ alias name username
13
+ # @return [Discorb::Snowflake] The user's ID.
14
+ attr_reader :id
15
+ # @return [Discorb::User::Flag] The user's flags.
16
+ attr_reader :flag
17
+ # @return [String] The user's discriminator.
18
+ attr_reader :discriminator
19
+ # @return [Discorb::Asset] The user's avatar.
20
+ attr_reader :avatar
21
+ # @return [Boolean] Whether the user is a bot.
22
+ attr_reader :bot
23
+ alias bot? bot
24
+
25
+ include Discorb::Messageable
26
+
27
+ # @!visibility private
28
+ def initialize(client, data)
29
+ @client = client
30
+ @data = {}
31
+ @dm_channel_id = nil
32
+ _set_data(data)
33
+ end
34
+
35
+ #
36
+ # Format the user as `Username#Discriminator` style.
37
+ #
38
+ # @return [String] The formatted username.
39
+ #
40
+ def to_s
41
+ "#{@username}##{@discriminator}"
42
+ end
43
+
44
+ def inspect
45
+ "#<#{self.class} #{self}>"
46
+ end
47
+
48
+ #
49
+ # Whether the user is a owner of the client.
50
+ # @macro async
51
+ # @macro http
52
+ #
53
+ # @param [Boolean] strict Whether don't allow if the user is a member of the team.
54
+ #
55
+ # @return [Boolean] Whether the user is a owner of the client.
56
+ #
57
+ def bot_owner?(strict: false)
58
+ Async do
59
+ app = @client.fetch_application.wait
60
+ if app.team.nil?
61
+ app.owner == self
62
+ elsif strict
63
+ app.team.owner == self
64
+ else
65
+ app.team.members.any? { |m| m.user == self }
66
+ end
67
+ end
68
+ end
69
+
70
+ alias app_owner? bot_owner?
71
+
72
+ # @!visibility private
73
+ def base_url
74
+ Async do
75
+ next @dm_channel_id if @dm_channel_id
76
+
77
+ dm_channel = @client.internet.post("/users/#{@id}/channels", { recipient_id: @client.user.id }).wait
78
+ @dm_channel_id = dm_channel[:id]
79
+ "/channels/#{@dm_channel_id}"
80
+ end
81
+ end
82
+
83
+ #
84
+ # Represents the user's flags.
85
+ # ## Flag fields
86
+ # |`1 << 0`|`:discord_employee`|
87
+ # |`1 << 1`|`:partnered_server_owner`|
88
+ # |`1 << 2`|`:hypesquad_events`|
89
+ # |`1 << 3`|`:bug_hunter_level_1`|
90
+ # |`1 << 6`|`:house_bravery`|
91
+ # |`1 << 7`|`:house_brilliance`|
92
+ # |`1 << 8`|`:house_balance`|
93
+ # |`1 << 9`|`:early_supporter`|
94
+ # |`1 << 10`|`:team_user`|
95
+ # |`1 << 14`|`:bug_hunter_level_2`|
96
+ # |`1 << 16`|`:verified_bot`|
97
+ # |`1 << 17`|`:early_verified_bot_developer`|
98
+ # |`1 << 18`|`:discord_certified_moderator`|
99
+ #
100
+ class Flag < Discorb::Flag
101
+ @bits = {
102
+ discord_employee: 0,
103
+ partnered_server_owner: 1,
104
+ hypesquad_events: 2,
105
+ bug_hunter_level_1: 3,
106
+ house_bravery: 6,
107
+ house_brilliance: 7,
108
+ house_balance: 8,
109
+ early_supporter: 9,
110
+ team_user: 10,
111
+ bug_hunter_level_2: 14,
112
+ verified_bot: 16,
113
+ early_verified_bot_developer: 17,
114
+ discord_certified_moderator: 18,
115
+ }.freeze
116
+ end
117
+
118
+ private
119
+
120
+ def _set_data(data)
121
+ @username = data[:username]
122
+ @verified = data[:verified]
123
+ @id = Snowflake.new(data[:id])
124
+ @flag = User::Flag.new(data[:public_flags] | (data[:flags] || 0))
125
+ @discriminator = data[:discriminator]
126
+ @avatar = Asset.new(self, data[:avatar])
127
+ @bot = data[:bot]
128
+ @raw_data = data
129
+ @client.users[@id] = self if !data[:no_cache] && data.is_a?(User)
130
+ @data.update(data)
131
+ end
132
+ end
133
+
134
+ #
135
+ # Represents a client user.
136
+ #
137
+ class ClientUser < User
138
+ #
139
+ # Edit the client user.
140
+ # @macro async
141
+ # @macro http
142
+ # @macro edit
143
+ #
144
+ # @param [String] name The new username.
145
+ # @param [Discorb::Image] avatar The new avatar.
146
+ #
147
+ def edit(name: :unset, avatar: :unset)
148
+ Async do
149
+ payload = {}
150
+ payload[:username] = name unless name == :unset
151
+ if avatar == :unset
152
+ # Nothing
153
+ elsif avatar.nil?
154
+ payload[:avatar] = nil
155
+ else
156
+ payload[:avatar] = avatar.to_s
157
+ end
158
+ @client.internet.patch("/users/@me", payload).wait
159
+ self
160
+ end
161
+ end
162
+ end
163
+ end