From b22f17b596bd0ce36a1e7f7c149ab4ff2fe3e536 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Sat, 21 Dec 2024 23:10:53 -0500 Subject: [PATCH 1/2] Channel pins update event. --- lib/discordrb/bot.rb | 4 ++++ lib/discordrb/container.rb | 11 +++++++++++ lib/discordrb/events/channels.rb | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/lib/discordrb/bot.rb b/lib/discordrb/bot.rb index 9fb43b91d..62a4e23d8 100644 --- a/lib/discordrb/bot.rb +++ b/lib/discordrb/bot.rb @@ -1450,6 +1450,10 @@ def handle_dispatch(type, data) event = ChannelRecipientRemoveEvent.new(data, self) raise_event(event) + when :CHANNEL_PINS_UPDATE + event = ChannelPinsUpdateEvent.new(data, self) + raise_event(event) + when :GUILD_MEMBER_ADD add_guild_member(data) diff --git a/lib/discordrb/container.rb b/lib/discordrb/container.rb index 5521437f9..c04f6b21f 100644 --- a/lib/discordrb/container.rb +++ b/lib/discordrb/container.rb @@ -635,6 +635,17 @@ def channel_select(attributes = {}, &block) register_event(ChannelSelectEvent, attributes, block) end + # This **event** is raised whenever a message is pinned or unpinned. + # @param attributes [Hash] The event's attributes. + # @option attributes [String, Integer, Channel] :channel A channel to match against. + # @option attributes [String, Integer, Server] :server A server to match against. + # @yield The block is executed when the event is raised. + # @yieldparam event [ChannelPinsUpdateEvent] The event that was raised. + # @return [ChannelPinsUpdateEventHandler] The event handler that was registered. + def channel_pins_update(attributes = {}, &block) + register_event(ChannelPinsUpdateEvent, attributes, block) + end + # This **event** is raised for every dispatch received over the gateway, whether supported by discordrb or not. # @param attributes [Hash] The event's attributes. # @option attributes [String, Symbol, Regexp] :type Matches the event type of the dispatch. diff --git a/lib/discordrb/events/channels.rb b/lib/discordrb/events/channels.rb index 992ef305f..48524fa6b 100644 --- a/lib/discordrb/events/channels.rb +++ b/lib/discordrb/events/channels.rb @@ -168,6 +168,39 @@ def matches?(event) end end + # Raised when a message is pinned or unpinned. + class ChannelPinsUpdateEvent < Event + # @return [Time, nil] Time at which the most recent pinned message was pinned. + attr_reader :last_pin_timestamp + + # @return [Channel] The channel this event originates from. + attr_reader :channel + + # @return [Server, nil] The server this event originates from. + attr_reader :server + + def initialize(data, bot) + @bot = bot + + @server = bot.server(data['guild_id']) if data['guild_id'] + @channel = bot.channel(data['channel_id']) + @last_pin_timestamp = Time.iso8601(data['last_pin_timestamp']) if data['last_pin_timestamp'] + end + end + + # Event handler for ChannelPinsUpdateEvent. + class ChannelPinsUpdateEventHandler < EventHandler + def matches?(event) + # Check for the proper event type. + return false unless event.is_a? ChannelPinsUpdateEvent + + [ + matches_all(@attributes[:server], event.server) { |a, e| a.resolve_id == e&.id }, + matches_all(@attributes[:channel], event.channel) { |a, e| a.resolve_id == e.id }, + ].reduce(true, &:&) + end + end + # Raised when a user is added to a private channel class ChannelRecipientAddEvent < ChannelRecipientEvent; end From 1b7a95ba7d66526f3b9853932645cdfb58d3a050 Mon Sep 17 00:00:00 2001 From: DroidDevelopment Date: Sat, 21 Dec 2024 23:15:09 -0500 Subject: [PATCH 2/2] Rubocop --- lib/discordrb/events/channels.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/discordrb/events/channels.rb b/lib/discordrb/events/channels.rb index 48524fa6b..b14893ad4 100644 --- a/lib/discordrb/events/channels.rb +++ b/lib/discordrb/events/channels.rb @@ -196,7 +196,7 @@ def matches?(event) [ matches_all(@attributes[:server], event.server) { |a, e| a.resolve_id == e&.id }, - matches_all(@attributes[:channel], event.channel) { |a, e| a.resolve_id == e.id }, + matches_all(@attributes[:channel], event.channel) { |a, e| a.resolve_id == e.id } ].reduce(true, &:&) end end