Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: channel pins update event #294

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions lib/discordrb/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions lib/discordrb/events/channels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading