onyxcord 1.1.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.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/.devcontainer/Dockerfile +13 -0
  3. data/.devcontainer/devcontainer.json +29 -0
  4. data/.devcontainer/postcreate.sh +4 -0
  5. data/.github/CONTRIBUTING.md +13 -0
  6. data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  7. data/.github/ISSUE_TEMPLATE/feature_request.md +24 -0
  8. data/.github/pull_request_template.md +37 -0
  9. data/.github/workflows/ci.yml +78 -0
  10. data/.github/workflows/codeql.yml +65 -0
  11. data/.github/workflows/deploy.yml +54 -0
  12. data/.github/workflows/release.yml +51 -0
  13. data/.gitignore +16 -0
  14. data/.markdownlint.json +4 -0
  15. data/.overcommit.yml +7 -0
  16. data/.rspec +2 -0
  17. data/.rubocop.yml +129 -0
  18. data/.yardopts +1 -0
  19. data/CHANGELOG.md +0 -0
  20. data/Gemfile +7 -0
  21. data/LICENSE.txt +21 -0
  22. data/README.md +305 -0
  23. data/Rakefile +17 -0
  24. data/bin/console +15 -0
  25. data/bin/setup +7 -0
  26. data/lib/onyxcord/allowed_mentions.rb +43 -0
  27. data/lib/onyxcord/api/application.rb +316 -0
  28. data/lib/onyxcord/api/channel.rb +700 -0
  29. data/lib/onyxcord/api/interaction.rb +67 -0
  30. data/lib/onyxcord/api/invite.rb +44 -0
  31. data/lib/onyxcord/api/server.rb +775 -0
  32. data/lib/onyxcord/api/user.rb +158 -0
  33. data/lib/onyxcord/api/webhook.rb +163 -0
  34. data/lib/onyxcord/api.rb +335 -0
  35. data/lib/onyxcord/await.rb +51 -0
  36. data/lib/onyxcord/bot.rb +1971 -0
  37. data/lib/onyxcord/cache.rb +326 -0
  38. data/lib/onyxcord/colour_rgb.rb +43 -0
  39. data/lib/onyxcord/commands/command_bot.rb +511 -0
  40. data/lib/onyxcord/commands/container.rb +112 -0
  41. data/lib/onyxcord/commands/events.rb +11 -0
  42. data/lib/onyxcord/commands/parser.rb +327 -0
  43. data/lib/onyxcord/commands/rate_limiter.rb +144 -0
  44. data/lib/onyxcord/configuration.rb +125 -0
  45. data/lib/onyxcord/container.rb +988 -0
  46. data/lib/onyxcord/data/activity.rb +271 -0
  47. data/lib/onyxcord/data/application.rb +341 -0
  48. data/lib/onyxcord/data/attachment.rb +91 -0
  49. data/lib/onyxcord/data/audit_logs.rb +438 -0
  50. data/lib/onyxcord/data/avatar_decoration.rb +26 -0
  51. data/lib/onyxcord/data/call.rb +22 -0
  52. data/lib/onyxcord/data/channel.rb +1355 -0
  53. data/lib/onyxcord/data/channel_tag.rb +69 -0
  54. data/lib/onyxcord/data/collectibles.rb +47 -0
  55. data/lib/onyxcord/data/component.rb +583 -0
  56. data/lib/onyxcord/data/embed.rb +258 -0
  57. data/lib/onyxcord/data/emoji.rb +123 -0
  58. data/lib/onyxcord/data/install_params.rb +24 -0
  59. data/lib/onyxcord/data/integration.rb +144 -0
  60. data/lib/onyxcord/data/interaction.rb +1141 -0
  61. data/lib/onyxcord/data/invite.rb +137 -0
  62. data/lib/onyxcord/data/member.rb +528 -0
  63. data/lib/onyxcord/data/message.rb +612 -0
  64. data/lib/onyxcord/data/message_activity.rb +41 -0
  65. data/lib/onyxcord/data/overwrite.rb +109 -0
  66. data/lib/onyxcord/data/poll.rb +365 -0
  67. data/lib/onyxcord/data/primary_server.rb +60 -0
  68. data/lib/onyxcord/data/profile.rb +79 -0
  69. data/lib/onyxcord/data/reaction.rb +64 -0
  70. data/lib/onyxcord/data/recipient.rb +34 -0
  71. data/lib/onyxcord/data/role.rb +449 -0
  72. data/lib/onyxcord/data/role_connection_data.rb +69 -0
  73. data/lib/onyxcord/data/role_subscription.rb +41 -0
  74. data/lib/onyxcord/data/scheduled_event.rb +513 -0
  75. data/lib/onyxcord/data/server.rb +1614 -0
  76. data/lib/onyxcord/data/server_preview.rb +68 -0
  77. data/lib/onyxcord/data/snapshot.rb +112 -0
  78. data/lib/onyxcord/data/team.rb +98 -0
  79. data/lib/onyxcord/data/timestamp.rb +69 -0
  80. data/lib/onyxcord/data/user.rb +324 -0
  81. data/lib/onyxcord/data/voice_region.rb +46 -0
  82. data/lib/onyxcord/data/voice_state.rb +41 -0
  83. data/lib/onyxcord/data/webhook.rb +238 -0
  84. data/lib/onyxcord/data.rb +57 -0
  85. data/lib/onyxcord/errors.rb +246 -0
  86. data/lib/onyxcord/event_executor.rb +80 -0
  87. data/lib/onyxcord/events/await.rb +48 -0
  88. data/lib/onyxcord/events/bans.rb +60 -0
  89. data/lib/onyxcord/events/channels.rb +225 -0
  90. data/lib/onyxcord/events/generic.rb +129 -0
  91. data/lib/onyxcord/events/guilds.rb +269 -0
  92. data/lib/onyxcord/events/integrations.rb +100 -0
  93. data/lib/onyxcord/events/interactions.rb +624 -0
  94. data/lib/onyxcord/events/invites.rb +127 -0
  95. data/lib/onyxcord/events/lifetime.rb +31 -0
  96. data/lib/onyxcord/events/members.rb +110 -0
  97. data/lib/onyxcord/events/message.rb +399 -0
  98. data/lib/onyxcord/events/polls.rb +118 -0
  99. data/lib/onyxcord/events/presence.rb +131 -0
  100. data/lib/onyxcord/events/raw.rb +74 -0
  101. data/lib/onyxcord/events/reactions.rb +218 -0
  102. data/lib/onyxcord/events/roles.rb +87 -0
  103. data/lib/onyxcord/events/scheduled_events.rb +171 -0
  104. data/lib/onyxcord/events/threads.rb +100 -0
  105. data/lib/onyxcord/events/typing.rb +73 -0
  106. data/lib/onyxcord/events/voice_server_update.rb +48 -0
  107. data/lib/onyxcord/events/voice_state_update.rb +106 -0
  108. data/lib/onyxcord/events/webhooks.rb +65 -0
  109. data/lib/onyxcord/gateway.rb +890 -0
  110. data/lib/onyxcord/id_object.rb +39 -0
  111. data/lib/onyxcord/light/data.rb +62 -0
  112. data/lib/onyxcord/light/integrations.rb +73 -0
  113. data/lib/onyxcord/light/light_bot.rb +58 -0
  114. data/lib/onyxcord/light.rb +8 -0
  115. data/lib/onyxcord/logger.rb +120 -0
  116. data/lib/onyxcord/message_components.rb +70 -0
  117. data/lib/onyxcord/paginator.rb +60 -0
  118. data/lib/onyxcord/permissions.rb +255 -0
  119. data/lib/onyxcord/rate_limiter/gateway.rb +42 -0
  120. data/lib/onyxcord/rate_limiter/rest.rb +89 -0
  121. data/lib/onyxcord/version.rb +7 -0
  122. data/lib/onyxcord/voice/encoder.rb +115 -0
  123. data/lib/onyxcord/voice/network.rb +380 -0
  124. data/lib/onyxcord/voice/opcodes.rb +29 -0
  125. data/lib/onyxcord/voice/sodium.rb +157 -0
  126. data/lib/onyxcord/voice/timer.rb +19 -0
  127. data/lib/onyxcord/voice/voice_bot.rb +386 -0
  128. data/lib/onyxcord/webhooks.rb +14 -0
  129. data/lib/onyxcord/websocket.rb +62 -0
  130. data/lib/onyxcord.rb +180 -0
  131. data/onyxcord-webhooks.gemspec +30 -0
  132. data/onyxcord.gemspec +50 -0
  133. metadata +421 -0
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'onyxcord/data'
4
+ require 'onyxcord/events/generic'
5
+
6
+ module OnyxCord::Events
7
+ # Generic superclass for poll events.
8
+ class PollVoteEvent < Event
9
+ # @return [Integer] the ID of the user associated with the event.
10
+ attr_reader :user_id
11
+
12
+ # @return [Integer, nil] the ID of the server associated with the
13
+ # event.
14
+ attr_reader :server_id
15
+
16
+ # @return [Integer] the ID of the answer associated with the event.
17
+ attr_reader :answer_id
18
+
19
+ # @return [Integer] the ID of the channel associated with the event.
20
+ attr_reader :channel_id
21
+
22
+ # @return [Integer] the ID of the message associated with the event.
23
+ attr_reader :message_id
24
+
25
+ # @!visibility private
26
+ def initialize(data, bot)
27
+ @bot = bot
28
+ @user_id = data['user_id']&.to_i
29
+ @server_id = data['guild_id']&.to_i
30
+ @answer_id = data['answer_id']&.to_i
31
+ @channel_id = data['channel_id']&.to_i
32
+ @message_id = data['message_id']&.to_i
33
+ end
34
+
35
+ # Get the poll associated with the event.
36
+ # @return [Poll] The poll associated with the event.
37
+ def poll
38
+ message&.poll
39
+ end
40
+
41
+ # Get the poll answer associated with the event.
42
+ # @return [Poll::Answer] The poll answer associated with the event.
43
+ def answer
44
+ poll&.answer(@answer_id)
45
+ end
46
+
47
+ # Get the channel that the poll originates from.
48
+ # @return [Channel] The channel that the poll originates from.
49
+ def channel
50
+ @bot.channel(@channel_id)
51
+ end
52
+
53
+ # Get the server that the poll originates from.
54
+ # @return [Server, nil] The server that the poll originates from.
55
+ def server
56
+ @bot.server(@server_id) if @server_id
57
+ end
58
+
59
+ # Get the message that the poll originates from.
60
+ # @return [Message] The message that the poll originates from.
61
+ def message
62
+ @message ||= channel.load_message(@message_id)
63
+ end
64
+
65
+ # Get the user who added or removed their poll vote.
66
+ # @return [User, Member] The member who added the poll vote, or
67
+ # a user if the member cannot be reached, or the poll was created
68
+ # in a DM channel.
69
+ def user
70
+ @user ||= (server&.member(@user_id) || @bot.user(@user_id))
71
+ end
72
+
73
+ alias_method :member, :user
74
+ end
75
+
76
+ # Generic event handler for polls.
77
+ class PollVoteEventHandler < EventHandler
78
+ # @!visibility private
79
+ def matches?(event)
80
+ # Check for the proper event type.
81
+ return false unless event.is_a?(PollVoteEvent)
82
+
83
+ [
84
+ matches_all(@attributes[:answer], event.answer_id) do |a, e|
85
+ (a.respond_to?(:id) ? a.id : a&.resolve_id) == e
86
+ end,
87
+
88
+ matches_all(@attributes[:server], event.server_id) do |a, e|
89
+ a&.resolve_id == e
90
+ end,
91
+
92
+ matches_all(@attributes[:message], event.message_id) do |a, e|
93
+ a&.resolve_id == e
94
+ end,
95
+
96
+ matches_all(@attributes[:channel], event.channel_id) do |a, e|
97
+ a&.resolve_id == e
98
+ end,
99
+
100
+ matches_all(@attributes[:user] || @attributes[:member], event.user_id) do |a, e|
101
+ a&.resolve_id == e
102
+ end
103
+ ].reduce(true, &:&)
104
+ end
105
+ end
106
+
107
+ # Raised whenever someone votes on a poll.
108
+ class PollVoteAddEvent < PollVoteEvent; end
109
+
110
+ # Raised whenever someone removes a poll vote.
111
+ class PollVoteRemoveEvent < PollVoteEvent; end
112
+
113
+ # Event handler for the :MESSAGE_POLL_VOTE_ADD event.
114
+ class PollVoteAddEventHandler < PollVoteEventHandler; end
115
+
116
+ # Event handler for the :MESSAGE_POLL_VOTE_REMOVE event.
117
+ class PollVoteRemoveEventHandler < PollVoteEventHandler; end
118
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'onyxcord/events/generic'
4
+ require 'onyxcord/data'
5
+
6
+ module OnyxCord::Events
7
+ # Event raised when a user's presence state updates (idle or online)
8
+ class PresenceEvent < Event
9
+ # @return [Server] the server on which the presence update happened.
10
+ attr_reader :server
11
+
12
+ # @return [User] the user whose status got updated.
13
+ attr_reader :user
14
+
15
+ # @return [Symbol] the new status.
16
+ attr_reader :status
17
+
18
+ # @return [Hash<Symbol, Symbol>] the current online status (`:online`, `:idle` or `:dnd`) of the user
19
+ # on various device types (`:desktop`, `:mobile`, or `:web`). The value will be `nil` if the user is offline or invisible.
20
+ attr_reader :client_status
21
+
22
+ # @!visibility private
23
+ def initialize(data, bot)
24
+ @bot = bot
25
+
26
+ @user = bot.user(data['user']['id'].to_i)
27
+ @status = data['status'].to_sym
28
+ @client_status = user.client_status
29
+ @server = bot.server(data['guild_id'].to_i)
30
+ end
31
+ end
32
+
33
+ # Event handler for PresenceEvent
34
+ class PresenceEventHandler < EventHandler
35
+ def matches?(event)
36
+ # Check for the proper event type
37
+ return false unless event.is_a? PresenceEvent
38
+
39
+ [
40
+ matches_all(@attributes[:from], event.user) do |a, e|
41
+ a == case a
42
+ when String
43
+ e.name
44
+ when Integer
45
+ e.id
46
+ else
47
+ e
48
+ end
49
+ end,
50
+ matches_all(@attributes[:status], event.status) do |a, e|
51
+ a == if a.is_a? String
52
+ e.to_s
53
+ else
54
+ e
55
+ end
56
+ end
57
+ ].reduce(true, &:&)
58
+ end
59
+ end
60
+
61
+ # Event raised when a user starts or stops playing a game
62
+ class PlayingEvent < Event
63
+ # @return [Server] the server on which the presence update happened.
64
+ attr_reader :server
65
+
66
+ # @return [User] the user whose status got updated.
67
+ attr_reader :user
68
+
69
+ # @return [OnyxCord::Activity] The new activity
70
+ attr_reader :activity
71
+
72
+ # @!attribute [r] url
73
+ # @return [String] the URL to the stream
74
+
75
+ # @!attribute [r] details
76
+ # @return [String] what the player is currently doing (ex. game being streamed)
77
+
78
+ # @!attribute [r] type
79
+ # @return [Integer] the type of play. See {OnyxCord::Activity}
80
+ delegate :url, :details, :type, to: :activity
81
+
82
+ # @return [Hash<Symbol, Symbol>] the current online status (`:online`, `:idle` or `:dnd`) of the user
83
+ # on various device types (`:desktop`, `:mobile`, or `:web`). The value will be `nil` if the user is offline or invisible.
84
+ attr_reader :client_status
85
+
86
+ # @!visibility private
87
+ def initialize(data, activity, bot)
88
+ @bot = bot
89
+ @activity = activity
90
+
91
+ @server = bot.server(data['guild_id'].to_i)
92
+ @user = bot.user(data['user']['id'].to_i)
93
+ @client_status = @user.client_status
94
+ end
95
+
96
+ # @return [String] the name of the new game the user is playing.
97
+ def game
98
+ @activity.name
99
+ end
100
+ end
101
+
102
+ # Event handler for PlayingEvent
103
+ class PlayingEventHandler < EventHandler
104
+ def matches?(event)
105
+ # Check for the proper event type
106
+ return false unless event.is_a? PlayingEvent
107
+
108
+ [
109
+ matches_all(@attributes[:from], event.user) do |a, e|
110
+ a == case a
111
+ when String
112
+ e.name
113
+ when Integer
114
+ e.id
115
+ else
116
+ e
117
+ end
118
+ end,
119
+ matches_all(@attributes[:game], event.game) do |a, e|
120
+ a == e
121
+ end,
122
+ matches_all(@attributes[:type], event.type) do |a, e|
123
+ a == e
124
+ end,
125
+ matches_all(@attributes[:client_status], event.client_status) do |a, e|
126
+ e.slice(a.keys) == a
127
+ end
128
+ ].reduce(true, &:&)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'onyxcord/events/generic'
4
+
5
+ # Event classes and handlers
6
+ module OnyxCord::Events
7
+ # Event raised when any dispatch is received
8
+ class RawEvent < Event
9
+ # @return [Symbol] the type of this dispatch.
10
+ attr_reader :type
11
+ alias_method :t, :type
12
+
13
+ # @return [Hash] the data of this dispatch.
14
+ attr_reader :data
15
+ alias_method :d, :data
16
+
17
+ # @!visibility private
18
+ def initialize(type, data, bot)
19
+ @type = type
20
+ @data = data
21
+ @bot = bot
22
+ end
23
+ end
24
+
25
+ # Event handler for {RawEvent}
26
+ class RawEventHandler < EventHandler
27
+ def matches?(event)
28
+ # Check for the proper event type
29
+ return false unless event.is_a? RawEvent
30
+
31
+ [
32
+ matches_all(@attributes[:type] || @attributes[:t], event.type) do |a, e|
33
+ if a.is_a? Regexp
34
+ a.match?(e)
35
+ else
36
+ e.to_s.casecmp(a.to_s).zero?
37
+ end
38
+ end
39
+ ].reduce(true, &:&)
40
+ end
41
+ end
42
+
43
+ # Event raised when an unknown dispatch is received
44
+ class UnknownEvent < RawEvent; end
45
+
46
+ # Event handler for {UnknownEvent}
47
+ class UnknownEventHandler < RawEventHandler; end
48
+
49
+ # Handler for raw Gateway payloads. This bypasses Event object allocation and
50
+ # receives the original parsed Gateway packet.
51
+ class RawDispatchHandler
52
+ def initialize(filter, block)
53
+ @filter = filter
54
+ @block = block
55
+ end
56
+
57
+ def matches?(packet)
58
+ return true unless @filter
59
+
60
+ type = packet['t']
61
+ return false unless type
62
+
63
+ if @filter.is_a?(Regexp)
64
+ @filter.match?(type)
65
+ else
66
+ type.to_s.casecmp(@filter.to_s).zero?
67
+ end
68
+ end
69
+
70
+ def call(packet)
71
+ @block.call(packet)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'onyxcord/events/generic'
4
+ require 'onyxcord/data'
5
+
6
+ module OnyxCord::Events
7
+ # Generic superclass for events about adding and removing reactions
8
+ class ReactionEvent < Event
9
+ include Respondable
10
+
11
+ # @return [Emoji] the emoji that was reacted with.
12
+ attr_reader :emoji
13
+
14
+ # @!visibility private
15
+ attr_reader :message_id
16
+
17
+ # @return [true, false] whether the reaction is a burst reaction.
18
+ attr_reader :burst
19
+ alias_method :burst?, :burst
20
+
21
+ # @return [Integer] the type of the reaction. 0 for normal, 1 for burst.
22
+ attr_reader :type
23
+
24
+ # @return [Array<ColourRGB>] an array of colors used for animations in burst reactions.
25
+ attr_reader :burst_colours
26
+ alias_method :burst_colors, :burst_colours
27
+
28
+ # @!visibility private
29
+ def initialize(data, bot)
30
+ @bot = bot
31
+
32
+ @emoji = OnyxCord::Emoji.new(data['emoji'], bot, nil)
33
+ @user_id = data['user_id'].to_i
34
+ @message_id = data['message_id'].to_i
35
+ @channel_id = data['channel_id'].to_i
36
+ @burst = data['burst']
37
+ @type = data['type']
38
+ @burst_colours = data['burst_colors']&.map { |b| OnyxCord::ColourRGB.new(b.delete('#')) } || []
39
+ end
40
+
41
+ # @return [User, Member] the user that reacted to this message, or member if a server exists.
42
+ def user
43
+ # Cache the user so we don't do requests all the time
44
+ @user ||= if server
45
+ @server.member(@user_id)
46
+ else
47
+ @bot.user(@user_id)
48
+ end
49
+ end
50
+
51
+ # @return [Message] the message that was reacted to.
52
+ def message
53
+ @message ||= channel.load_message(@message_id)
54
+ end
55
+
56
+ # @return [Channel] the channel that was reacted in.
57
+ def channel
58
+ @channel ||= @bot.channel(@channel_id)
59
+ end
60
+
61
+ # @return [Server, nil] the server that was reacted in. If reacted in a PM channel, it will be nil.
62
+ def server
63
+ @server ||= channel.server
64
+ end
65
+ end
66
+
67
+ # Generic superclass for event handlers pertaining to adding and removing reactions
68
+ class ReactionEventHandler < EventHandler
69
+ def matches?(event)
70
+ # Check for the proper event type
71
+ return false unless event.is_a? ReactionEvent
72
+
73
+ [
74
+ matches_all(@attributes[:emoji], event.emoji) do |a, e|
75
+ case a
76
+ when Integer
77
+ e.id == a
78
+ when String
79
+ e.name == a || e.name == a.delete(':') || e.id == a.resolve_id
80
+ else
81
+ e == a
82
+ end
83
+ end,
84
+ matches_all(@attributes[:message], event.message_id) do |a, e|
85
+ a == e
86
+ end,
87
+ matches_all(@attributes[:in], event.channel) do |a, e|
88
+ case a
89
+ when String
90
+ # Make sure to remove the "#" from channel names in case it was specified
91
+ a.delete('#') == e.name
92
+ when Integer
93
+ a == e.id
94
+ else
95
+ a == e
96
+ end
97
+ end,
98
+ matches_all(@attributes[:from], event.user) do |a, e|
99
+ case a
100
+ when String
101
+ a == e.name
102
+ when :bot
103
+ e.current_bot?
104
+ else
105
+ a == e
106
+ end
107
+ end,
108
+ matches_all(@attributes[:type], event.type) do |a, e|
109
+ case a
110
+ when Integer
111
+ a == e
112
+ when Symbol, String
113
+ OnyxCord::Reaction::TYPES[a.to_sym] == e
114
+ end
115
+ end
116
+ ].reduce(true, &:&)
117
+ end
118
+ end
119
+
120
+ # Event raised when somebody reacts to a message
121
+ class ReactionAddEvent < ReactionEvent; end
122
+
123
+ # Event handler for {ReactionAddEvent}
124
+ class ReactionAddEventHandler < ReactionEventHandler; end
125
+
126
+ # Event raised when somebody removes a reaction to a message
127
+ class ReactionRemoveEvent < ReactionEvent; end
128
+
129
+ # Event handler for {ReactionRemoveEvent}
130
+ class ReactionRemoveEventHandler < ReactionEventHandler; end
131
+
132
+ # Event raised when somebody removes all reactions from a message
133
+ class ReactionRemoveAllEvent < Event
134
+ include Respondable
135
+
136
+ # @!visibility private
137
+ attr_reader :message_id
138
+
139
+ # @!visibility private
140
+ def initialize(data, bot)
141
+ @bot = bot
142
+
143
+ @message_id = data['message_id'].to_i
144
+ @channel_id = data['channel_id'].to_i
145
+ end
146
+
147
+ # @return [Channel] the channel where the removal occurred.
148
+ def channel
149
+ @channel ||= @bot.channel(@channel_id)
150
+ end
151
+
152
+ # @return [Message] the message all reactions were removed from.
153
+ def message
154
+ @message ||= channel.load_message(@message_id)
155
+ end
156
+ end
157
+
158
+ # Event handler for {ReactionRemoveAllEvent}
159
+ class ReactionRemoveAllEventHandler < EventHandler
160
+ def matches?(event)
161
+ # Check for the proper event type
162
+ return false unless event.is_a? ReactionRemoveAllEvent
163
+
164
+ [
165
+ matches_all(@attributes[:message], event.message_id) do |a, e|
166
+ a == e
167
+ end,
168
+ matches_all(@attributes[:in], event.channel) do |a, e|
169
+ case a
170
+ when String
171
+ # Make sure to remove the "#" from channel names in case it was specified
172
+ a.delete('#') == e.name
173
+ when Integer
174
+ a == e.id
175
+ else
176
+ a == e
177
+ end
178
+ end
179
+ ].reduce(true, &:&)
180
+ end
181
+ end
182
+
183
+ # Event raised when all instances of a single reaction are removed from a message.
184
+ class ReactionRemoveEmojiEvent < ReactionRemoveAllEvent
185
+ # @return [Emoji] the emoji that was removed.
186
+ attr_reader :emoji
187
+
188
+ # @!visibility private
189
+ def initialize(data, bot)
190
+ super
191
+
192
+ @emoji = OnyxCord::Emoji.new(data['emoji'], bot)
193
+ end
194
+ end
195
+
196
+ # Event handler for {ReactionRemoveEmojiEvent}.
197
+ class ReactionRemoveEmojiEventHandler < ReactionRemoveAllEventHandler
198
+ # @!visibility private
199
+ def matches?(event)
200
+ # Check for the proper event type.
201
+ return false unless super
202
+ return false unless event.is_a?(ReactionRemoveEmojiEvent)
203
+
204
+ [
205
+ matches_all(@attributes[:emoji], event.emoji) do |a, e|
206
+ case a
207
+ when Integer
208
+ e.id == a
209
+ when String
210
+ e.name == a || e.name == a.delete(':') || e.id == a.resolve_id
211
+ else
212
+ e == a
213
+ end
214
+ end
215
+ ].reduce(true, &:&)
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'onyxcord/events/generic'
4
+ require 'onyxcord/data'
5
+
6
+ module OnyxCord::Events
7
+ # Raised when a role is created on a server
8
+ class ServerRoleCreateEvent < Event
9
+ # @return [Role] the role that got created
10
+ attr_reader :role
11
+
12
+ # @return [Server] the server on which a role got created
13
+ attr_reader :server
14
+
15
+ # @!attribute [r] name
16
+ # @return [String] this role's name
17
+ # @see Role#name
18
+ delegate :name, to: :role
19
+
20
+ # @!visibility private
21
+ def initialize(data, bot)
22
+ @bot = bot
23
+
24
+ @server = bot.server(data['guild_id'].to_i)
25
+ @role = @server&.role(data['role']['id'].to_i)
26
+ end
27
+ end
28
+
29
+ # Event handler for ServerRoleCreateEvent
30
+ class ServerRoleCreateEventHandler < EventHandler
31
+ def matches?(event)
32
+ # Check for the proper event type
33
+ return false unless event.is_a? ServerRoleCreateEvent
34
+
35
+ [
36
+ matches_all(@attributes[:name], event.name) do |a, e|
37
+ a == if a.is_a? String
38
+ e.to_s
39
+ else
40
+ e
41
+ end
42
+ end
43
+ ].reduce(true, &:&)
44
+ end
45
+ end
46
+
47
+ # Raised when a role is deleted from a server
48
+ class ServerRoleDeleteEvent < Event
49
+ # @return [Integer] the ID of the role that got deleted.
50
+ attr_reader :id
51
+
52
+ # @return [Server] the server on which a role got deleted.
53
+ attr_reader :server
54
+
55
+ # @!visibility private
56
+ def initialize(data, bot)
57
+ @bot = bot
58
+
59
+ # The role should already be deleted from the server's list
60
+ # by the time we create this event, so we'll create a temporary
61
+ # role object for event consumers to use.
62
+ @id = data['role_id'].to_i
63
+ server_id = data['guild_id'].to_i
64
+ @server = bot.server(server_id)
65
+ end
66
+ end
67
+
68
+ # EventHandler for ServerRoleDeleteEvent
69
+ class ServerRoleDeleteEventHandler < EventHandler
70
+ def matches?(event)
71
+ # Check for the proper event type
72
+ return false unless event.is_a? ServerRoleDeleteEvent
73
+
74
+ [
75
+ matches_all(@attributes[:id], event.id) do |a, e|
76
+ a.resolve_id == e.resolve_id
77
+ end
78
+ ].reduce(true, &:&)
79
+ end
80
+ end
81
+
82
+ # Event raised when a role updates on a server
83
+ class ServerRoleUpdateEvent < ServerRoleCreateEvent; end
84
+
85
+ # Event handler for ServerRoleUpdateEvent
86
+ class ServerRoleUpdateEventHandler < ServerRoleCreateEventHandler; end
87
+ end