discordrb 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of discordrb might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eae3c0ee1c9f49e7e8c498ce1db7361a9e812053
4
- data.tar.gz: ed4937a0c6c8a4c1c084fa684c78e5b8f11ace7b
3
+ metadata.gz: 746a55903872da764ecd6fb4c82992781e147245
4
+ data.tar.gz: 519c53e91471feda8194fcc059ce825f4e4e613b
5
5
  SHA512:
6
- metadata.gz: 6b1fddaa771f734968bc3d0172b8fe87a95a442d9d6ee3ace91ad8e9a182a94c8b1829d333272d01dcad6a8a0cfad676d0c7f1018b34ba9afa66acdca58cf32e
7
- data.tar.gz: d1fe90193e8abd77d749a5982bb8ba4bb23b88625acd80f80fbdb0a37ab99127af5574cd3284c187258b254dbb863e235d9d85c313ec8ff5e71253a12bca0923
6
+ metadata.gz: 52df6acf8f65d4ce36d3111d66e8b0957c3c02a6b0b3c01b98bbb638344d07e67c65dc704ed70a0104785c1874e83ab56609a8054531ddad3c740919518bd798
7
+ data.tar.gz: 9edacd6ca16c117ac57f7dc1a9a327831ef497944a3a4508332ef6e8f633ae3b55646cb597d211728d11d32d6848b25e287da29969a2b8a2a37a61cb53a4045e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.0.1
4
+
5
+ A tiny update to support webhook-sent messages properly!
6
+
7
+ - Added the utility methods `Message#webhook?` and `User#webhook?` to check whether a message or a user belongs to a webhook
8
+ - Added `Message#webhook_id` to get the ID of the sending webhook for webhook messages
9
+ - Added the `webhook_commands` parameter to CommandBot that, if false (default true), prevents webhook-sent messages from being parsed and handled as commands.
10
+
11
+ ### Bugfixes
12
+
13
+ - Fixed webhook-sent messages being ignored because their author couldn't be resolved.
14
+ - Fixed a minor performance problem where a CommandBot would create unnecessarily create redundant objects for every received message.
15
+
3
16
  ## 3.0.0
4
17
 
5
18
  I didn't think there could possibly be a release larger than 2.0.0 was, but here it is! Including the respective release commit, there were 540 commits from 1.8.1 to 2.0.0, but a whopping 734 commits from 2.1.3 to 3.0.0.
data/lib/discordrb/bot.rb CHANGED
@@ -874,9 +874,10 @@ module Discordrb
874
874
  return
875
875
  end
876
876
 
877
- create_message(data)
878
-
879
- message = Message.new(data, self)
877
+ # If create_message is overwritten with a method that returns the parsed message, use that instead, so we don't
878
+ # parse the message twice (which is just thrown away performance)
879
+ message = create_message(data)
880
+ message = Message.new(data, self) unless message.is_a? Message
880
881
 
881
882
  return if message.from_bot? && !should_parse_self
882
883
 
@@ -48,6 +48,8 @@ module Discordrb::Commands
48
48
  # @option attributes [String] :no_permission_message The message to be displayed when `NoPermission` error is raised.
49
49
  # @option attributes [true, false] :spaces_allowed Whether spaces are allowed to occur between the prefix and the
50
50
  # command. Default is false.
51
+ # @option attributes [true, false] :webhook_commands Whether messages sent by webhooks are allowed to trigger
52
+ # commands. Default is true.
51
53
  # @option attributes [String] :previous Character that should designate the result of the previous command in
52
54
  # a command chain (see :advanced_functionality). Default is '~'.
53
55
  # @option attributes [String] :chain_delimiter Character that should designate that a new command begins in the
@@ -95,6 +97,9 @@ module Discordrb::Commands
95
97
  # Spaces allowed between prefix and command
96
98
  spaces_allowed: attributes[:spaces_allowed].nil? ? false : attributes[:spaces_allowed],
97
99
 
100
+ # Webhooks allowed to trigger commands
101
+ webhook_commands: attributes[:webhook_commands].nil? ? true : attributes[:webhook_commands],
102
+
98
103
  # All of the following need to be one character
99
104
  # String to designate previous result in command chain
100
105
  previous: attributes[:previous] || '~',
@@ -216,9 +221,14 @@ module Discordrb::Commands
216
221
  # @param server [Server] The server on which to check
217
222
  # @return [true, false] whether or not the user has the given permission
218
223
  def permission?(user, level, server)
219
- determined_level = server.nil? ? 0 : user.roles.reduce(0) do |memo, role|
220
- [@permissions[:roles][role.id] || 0, memo].max
221
- end
224
+ determined_level = if user.webhook? || server.nil?
225
+ 0
226
+ else
227
+ user.roles.reduce(0) do |memo, role|
228
+ [@permissions[:roles][role.id] || 0, memo].max
229
+ end
230
+ end
231
+
222
232
  [@permissions[:users][user.id] || 0, determined_level].max >= level
223
233
  end
224
234
 
@@ -227,7 +237,8 @@ module Discordrb::Commands
227
237
  # Internal handler for MESSAGE_CREATE that is overwritten to allow for command handling
228
238
  def create_message(data)
229
239
  message = Discordrb::Message.new(data, self)
230
- return if message.from_bot? && !@should_parse_self
240
+ return message if message.from_bot? && !@should_parse_self
241
+ return message if message.webhook? && !@attributes[:webhook_commands]
231
242
 
232
243
  unless message.author
233
244
  Discordrb::LOGGER.warn("Received a message (#{message.inspect}) with nil author! Ignoring, please report this if you can")
@@ -237,20 +248,23 @@ module Discordrb::Commands
237
248
  event = CommandEvent.new(message, self)
238
249
 
239
250
  chain = trigger?(message)
240
- return unless chain
251
+ return message unless chain
241
252
 
242
253
  # Don't allow spaces between the prefix and the command
243
254
  if chain.start_with?(' ') && !@attributes[:spaces_allowed]
244
255
  debug('Chain starts with a space')
245
- return
256
+ return message
246
257
  end
247
258
 
248
259
  if chain.strip.empty?
249
260
  debug('Chain is empty')
250
- return
261
+ return message
251
262
  end
252
263
 
253
264
  execute_chain(chain, event)
265
+
266
+ # Return the message so it doesn't get parsed again during the rest of the dispatch handling
267
+ message
254
268
  end
255
269
 
256
270
  # Check whether a message should trigger command execution, and if it does, return the raw chain
@@ -271,11 +285,12 @@ module Discordrb::Commands
271
285
 
272
286
  def required_permissions?(member, required, channel = nil)
273
287
  required.reduce(true) do |a, action|
274
- a && member.permission?(action, channel)
288
+ a && !member.webhook? && member.permission?(action, channel)
275
289
  end
276
290
  end
277
291
 
278
292
  def required_roles?(member, required)
293
+ return (required.nil? || required.empty?) if member.webhook?
279
294
  if required.is_a? Array
280
295
  required.all? do |role|
281
296
  member.role?(role)
@@ -211,6 +211,11 @@ module Discordrb
211
211
  @bot.profile.id == @id
212
212
  end
213
213
 
214
+ # @return [true, false] whether this user is a fake user for a webhook message
215
+ def webhook?
216
+ @discriminator == Message::ZERO_DISCRIM
217
+ end
218
+
214
219
  [:offline, :idle, :online].each do |e|
215
220
  define_method(e.to_s + '?') do
216
221
  @status.to_sym == e
@@ -1583,6 +1588,12 @@ module Discordrb
1583
1588
  attr_reader :pinned
1584
1589
  alias_method :pinned?, :pinned
1585
1590
 
1591
+ # @return [Integer, nil] the webhook ID that sent this message, or nil if it wasn't sent through a webhook.
1592
+ attr_reader :webhook_id
1593
+
1594
+ # The discriminator that webhook user accounts have.
1595
+ ZERO_DISCRIM = '0000'.freeze
1596
+
1586
1597
  # @!visibility private
1587
1598
  def initialize(data, bot)
1588
1599
  @bot = bot
@@ -1594,7 +1605,12 @@ module Discordrb
1594
1605
  @mention_everyone = data['mention_everyone']
1595
1606
 
1596
1607
  @author = if data['author']
1597
- if @channel.private?
1608
+ if data['author']['discriminator'] == ZERO_DISCRIM
1609
+ # This is a webhook user! It would be pointless to try to resolve a member here, so we just create
1610
+ # a User and return that instead.
1611
+ Discordrb::LOGGER.debug("Webhook user: #{data['author']['id']}")
1612
+ User.new(data['author'], @bot)
1613
+ elsif @channel.private?
1598
1614
  # Turn the message user into a recipient - we can't use the channel recipient
1599
1615
  # directly because the bot may also send messages to the channel
1600
1616
  Recipient.new(bot.user(data['author']['id'].to_i), @channel, bot)
@@ -1605,6 +1621,8 @@ module Discordrb
1605
1621
  end
1606
1622
  end
1607
1623
 
1624
+ @webhook_id = data['webhook_id'].to_i if data['webhook_id']
1625
+
1608
1626
  @timestamp = Time.parse(data['timestamp']) if data['timestamp']
1609
1627
  @edited_timestamp = data['edited_timestamp'].nil? ? nil : Time.parse(data['edited_timestamp'])
1610
1628
  @edited = !@edited_timestamp.nil?
@@ -1677,6 +1695,11 @@ module Discordrb
1677
1695
  @author && @author.current_bot?
1678
1696
  end
1679
1697
 
1698
+ # @return [true, false] whether this message has been sent over a webhook.
1699
+ def webhook?
1700
+ !@webhook_id.nil?
1701
+ end
1702
+
1680
1703
  # The inspect method is overwritten to give more useful output
1681
1704
  def inspect
1682
1705
  "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>"
@@ -3,5 +3,5 @@
3
3
  # Discordrb and all its functionality, in this case only the version.
4
4
  module Discordrb
5
5
  # The current version of discordrb.
6
- VERSION = '3.0.0'.freeze
6
+ VERSION = '3.0.1'.freeze
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discordrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - meew0
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-30 00:00:00.000000000 Z
11
+ date: 2016-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client