Skip to content

Commit

Permalink
Add invite event support
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley committed Nov 15, 2020
1 parent f0701d2 commit 32ba2b8
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'discordrb/events/raw'
require 'discordrb/events/reactions'
require 'discordrb/events/webhooks'
require 'discordrb/events/invites'

require 'discordrb/api'
require 'discordrb/api/channel'
Expand Down Expand Up @@ -1077,6 +1078,11 @@ def handle_dispatch(type, data)
id = data['guild_id'].to_i
server = server(id)
server.process_chunk(data['members'])
when :INVITE_CREATE
invite = Invite.new(data, self)
raise_event(InviteCreateEvent.new(data, invite, self))
when :INVITE_DELETE
raise_event(InviteDeleteEvent.new(data, self))
when :MESSAGE_CREATE
if ignored?(data['author']['id'].to_i)
debug("Ignored author with ID #{data['author']['id']}")
Expand Down
24 changes: 24 additions & 0 deletions lib/discordrb/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,30 @@ def pm(attributes = {}, &block)
alias_method :direct_message, :pm
alias_method :dm, :pm

# This **event** is raised when an invite is created.
# @param attributes [Hash] The event's attributes.
# @option attributes [String, Integer, User] :inviter Matches the user that created the invite.
# @option attributes [String, Integer, Channel] :channel Matches the channel the invite was created for.
# @option attributes [String, Integer, Server] :server Matches the server the invite was created for.
# @option attributes [true, false] :temporary Matches whether the invite is temporary or not.
# @yield The block is executed when the event is raised.
# @yieldparam event [InviteCreateEvent] The event that was raised.
# @return [InviteCreateEventHandler] The event handler that was registered.
def invite_create(attributes = {}, &block)
register_event(InviteCreateEvent, attributes, block)
end

# This **event** is raised when an invite is deleted.
# @param attributes [Hash] The event's attributes.
# @option attributes [String, Integer, Channel] :channel Matches the channel the deleted invite was for.
# @option attributes [String, Integer, Server] :server Matches the server the deleted invite was for.
# @yield The block is executed when the event is raised
# @yieldparam event [InviteDeleteEvent] The event that was raised.
# @return [InviteDeleteEventHandler] The event handler that was registered.
def invite_delete(attributes = {}, &block)
register_event(InviteDeleteEvent, 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
18 changes: 14 additions & 4 deletions lib/discordrb/data/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def initialize(data, bot)

# A Discord invite to a channel
class Invite
# @return [InviteChannel] the channel this invite references.
# @return [InviteChannel, Channel] the channel this invite references.
attr_reader :channel

# @return [InviteServer] the server this invite references.
# @return [InviteServer, Server] the server this invite references.
attr_reader :server

# @return [Integer] the amount of uses left on this invite.
Expand Down Expand Up @@ -87,8 +87,18 @@ class Invite
def initialize(data, bot)
@bot = bot

@channel = InviteChannel.new(data['channel'], bot)
@server = InviteServer.new(data['guild'], bot)
@channel = if data['channel_id'] || bot.channel
bot.channel(data['channel_id'])
else
InviteChannel.new(data['channel'], bot)
end

@server = if data['guild_id']
bot.server(data['guild_id'])
else
InviteServer.new(data['guild'], bot)
end

@uses = data['uses']
@inviter = data['inviter'] ? (@bot.user(data['inviter']['id'].to_i) || User.new(data['inviter'], bot)) : nil
@temporary = data['temporary']
Expand Down
1 change: 1 addition & 0 deletions lib/discordrb/data/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ def widget_banner_url(style)
def splash_id
@splash_id ||= JSON.parse(API::Server.resolve(@bot.token, @id))['splash']
end
alias splash_hash splash_id

# @return [String, nil] the splash image URL for the server's VIP invite page.
# `nil` if there is no splash image.
Expand Down
121 changes: 121 additions & 0 deletions lib/discordrb/events/invites.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# frozen_string_literal: true

module Discordrb::Events
# Raised when an invite is created.
class InviteCreateEvent < Event
# @return [Invite] The invite that was created.
attr_reader :invite

# @return [Server, nil] The server the invite was created for.
attr_reader :server

# @return [Channel] The channel the invite was created for.
attr_reader :channel

# @!attribute [r] code
# @return [String] The code for the created invite.
# @see Invite#code
# @!attribute [r] created_at
# @return [Time] The time the invite was created at.
# @see Invite#created_at
# @!attribute [r] max_age
# @return [Integer] The maximum age of the created invite.
# @see Invite#max_age
# @!attribute [r] max_uses
# @return [Integer] The maximum number of uses before the invite expires.
# @see Invite#max_uses
# @!attribute [r] temporary
# @return [true, false] Whether or not this invite grants temporary membership.
# @see Invite#temporary
# @!attribute [r] inviter
# @return [User] The user that created the invite.
# @see Invite#inviter
delegate :code, :created_at, :max_age, :max_uses, :temporary, :inviter, to: :invite

alias temporary? temporary

def initialize(data, invite, bot)
@bot = bot
@invite = invite
@channel = bot.channel(data['channel_id'])
@server = bot.server(data['guild_id']) if data['guild_id']
end
end

# Raised when an invite is deleted.
class InviteDeleteEvent < Event
# @return [Channel] The channel the deleted invite was for.
attr_reader :channel

# @return [Server, nil] The server the deleted invite was for.
attr_reader :server

# @return [String] The code of the deleted invite.
attr_reader :code

def initialize(data, bot)
@bot = bot
@channel = bot.channel(data['channel_id'])
@server = bot.server(data['guild_id']) if data['guild_id']
@code = data['code']
end
end

# Event handler for InviteCreateEvent.
class InviteCreateEventHandler < EventHandler
def matches?(event)
return false unless event.is_a? InviteCreateEvent

[
matches_all(@attributes[:server], event.server) do |a, e|
a == if a.is_a? String
e.name
elsif a.is_a? Integer
e.id
else
e
end
end,
matches_all(@attributes[:channel], event.channel) do |a, e|
a == if a.is_a? String
e.name
elsif a.is_a? Integer
e.id
else
e
end
end,
matches_all(@attributes[:temporary], event.temporary?, &:==),
matches_all(@attributes[:inviter], event.inviter, &:==)
].reduce(true, &:&)
end
end

# Event handler for InviteDeleteEvent
class InviteDeleteEventHandler < EventHandler
def matches?(event)
return false unless event.is_a? InviteDeleteEvent

[
matches_all(@attributes[:server], event.server) do |a, e|
a == if a.is_a? String
e.name
elsif a.is_a? Integer
e.id
else
e
end
end,
matches_all(@attributes[:channel], event.channel) do |a, e|
a == if a.is_a? String
e.name
elsif a.is_a? Integer
e.id
else
e
end
end
].reduce(true, &:&)
end
end
end

0 comments on commit 32ba2b8

Please sign in to comment.