discorb 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module Discorb
4
4
  # @return [String] The API base URL.
5
5
  API_BASE_URL = "https://discord.com/api/v9"
6
6
  # @return [String] The version of discorb.
7
- VERSION = "0.0.8"
7
+ VERSION = "0.1.0"
8
8
  # @return [String] The user agent for the bot.
9
9
  USER_AGENT = "DiscordBot (https://github.com/discorb-lib/discorb #{VERSION}) Ruby/#{RUBY_VERSION}"
10
10
 
@@ -108,19 +108,19 @@ module Discorb
108
108
  end
109
109
 
110
110
  def timestamp
111
- Time.at(((sf >> 22) + 1_420_070_400_000) / 1000)
111
+ Time.at(((@value >> 22) + 1_420_070_400_000) / 1000)
112
112
  end
113
113
 
114
114
  def worker_id
115
- (snowflake & 0x3E0000) >> 17
115
+ (@value & 0x3E0000) >> 17
116
116
  end
117
117
 
118
118
  def process_id
119
- (snowflake & 0x1F000) >> 12
119
+ (@value & 0x1F000) >> 12
120
120
  end
121
121
 
122
122
  def increment
123
- snowflake & 0xFFF
123
+ @value & 0xFFF
124
124
  end
125
125
  end
126
126
  end
@@ -29,7 +29,7 @@ module Discorb
29
29
  def register(id, body)
30
30
  @cache[id.to_s] = body
31
31
  @cache = @cache.sort_by(&@sort).to_h if @sort
32
- @cache.remove(@cache.values[-1]) if !@limit.nil? && @cache.size > @limit
32
+ @cache.delete(@cache.keys[0]) if !@limit.nil? && @cache.size > @limit
33
33
  body
34
34
  end
35
35
 
data/lib/discorb/embed.rb CHANGED
@@ -41,8 +41,8 @@ module Discorb
41
41
  # @param [Discorb::Embed::Author] author The author field of embed.
42
42
  # @param [Array<Discorb::Embed::Field>] fields The fields of embed.
43
43
  # @param [Discorb::Embed::Footer] footer The footer of embed.
44
- # @param [Discorb::Embed::Image] image The image of embed.
45
- # @param [Discorb::Embed::Thumbnail] thumbnail The thumbnail of embed.
44
+ # @param [Discorb::Embed::Image, String] image The image of embed.
45
+ # @param [Discorb::Embed::Thumbnail, String] thumbnail The thumbnail of embed.
46
46
  #
47
47
  def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
48
48
  fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil)
@@ -55,8 +55,8 @@ module Discorb
55
55
  @author = author
56
56
  @fields = fields || []
57
57
  @footer = footer
58
- @image = image
59
- @thumbnail = thumbnail
58
+ @image = image && (image.is_a?(String) ? Image.new(image) : image)
59
+ @thumbnail = thumbnail && (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
60
60
  @type = "rich"
61
61
  else
62
62
  @title = data[:title]
@@ -336,9 +336,9 @@ module Discorb
336
336
  attr_reader :url
337
337
 
338
338
  # @!visibility private
339
- def initialize(name, url)
340
- @name = name
341
- @url = url
339
+ def initialize(data)
340
+ @name = data[:name]
341
+ @url = data[:url]
342
342
  end
343
343
  end
344
344
  end
data/lib/discorb/emoji.rb CHANGED
@@ -179,9 +179,11 @@ module Discorb
179
179
  attr_reader :name
180
180
  # @return [String] The unicode value of the emoji. (e.g. U+1F600)
181
181
  attr_reader :value
182
+ # @return [Integer] The skin tone of the emoji.
183
+ attr_reader :skin_tone
182
184
 
183
185
  # @!visibility private
184
- def initialize(name)
186
+ def initialize(name, tone: 0)
185
187
  if EmojiTable::DISCORD_TO_UNICODE.key?(name)
186
188
  @name = name
187
189
  @value = EmojiTable::DISCORD_TO_UNICODE[name]
@@ -191,6 +193,9 @@ module Discorb
191
193
  else
192
194
  raise ArgumentError, "No such emoji: #{name}"
193
195
  end
196
+ if tone > 0
197
+ @value += EmojiTable::SKIN_TONES[tone]
198
+ end
194
199
  end
195
200
 
196
201
  # @return [String] The unicode string of the emoji.
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/ModuleLength
4
3
  module Discorb
5
4
  #
6
5
  # A table of emoji names and their unicode values.
@@ -3794,6 +3793,14 @@ module Discorb
3794
3793
  "\xF0\x9F\x95\x9B" => %w[clock12],
3795
3794
  "\xF0\x9F\x95\xA7" => %w[clock1230],
3796
3795
  }.freeze
3797
- # rubocop:enable Metrics/ModuleLength
3796
+
3797
+ SKIN_TONES = [
3798
+ :dummy,
3799
+ "\xf0\x9f\x8f\xbb",
3800
+ "\xf0\x9f\x8f\xbc",
3801
+ "\xf0\x9f\x8f\xbd",
3802
+ "\xf0\x9f\x8f\xbe",
3803
+ "\xf0\x9f\x8f\xbf",
3804
+ ].freeze
3798
3805
  end
3799
3806
  end
data/lib/discorb/error.rb CHANGED
@@ -11,6 +11,9 @@ module Discorb
11
11
  def enumerate_errors(hash)
12
12
  res = {}
13
13
  _recr_items([], hash, res)
14
+ if res == { "" => nil }
15
+ res = {}
16
+ end
14
17
  res
15
18
  end
16
19
 
@@ -56,7 +59,7 @@ module Discorb
56
59
  @code = data[:code]
57
60
  @response = resp
58
61
  DiscorbError.instance_method(:initialize).bind(self).call(
59
- [data[:message], "\n", enumerate_errors(data[:errors]).map do |ek, ev|
62
+ [data[:message], enumerate_errors(data[:errors]).map do |ek, ev|
60
63
  "#{ek}=>#{ev}"
61
64
  end.join("\n")].join("\n")
62
65
  )
@@ -25,9 +25,8 @@ module Discorb
25
25
  raise ArgumentError, "Event name must be a symbol" unless event_name.is_a?(Symbol)
26
26
  raise ArgumentError, "block must be a Proc" unless block.is_a?(Proc)
27
27
 
28
- @events = {} if @events.nil?
29
28
  @events[event_name] ||= []
30
- discriminator[:extension] = Extension
29
+ discriminator[:extension] = self.name
31
30
  @events[event_name] << Discorb::Event.new(block, id, discriminator)
32
31
  end
33
32
 
@@ -50,5 +49,9 @@ module Discorb
50
49
 
51
50
  # @!visibility private
52
51
  attr_accessor :client
52
+
53
+ def self.extended(obj)
54
+ obj.instance_variable_set(:@events, {})
55
+ end
53
56
  end
54
57
  end
@@ -4,10 +4,10 @@ require "async/http"
4
4
 
5
5
  module Discorb
6
6
  #
7
- # A module to handle gateway.
7
+ # A module for Discord Gateway.
8
8
  # This module is internal use only.
9
9
  #
10
- module GatewayHandler
10
+ module Gateway
11
11
  #
12
12
  # Represents an event.
13
13
  #
@@ -451,519 +451,528 @@ module Discorb
451
451
  end
452
452
  end
453
453
 
454
- private
455
-
456
- def connect_gateway(first)
457
- @log.info "Connecting to gateway."
458
- Async do
459
- @http = HTTP.new(self) if first
460
- @first = first
461
- _, gateway_response = @http.get("/gateway").wait
462
- gateway_url = gateway_response[:url]
463
- endpoint = Async::HTTP::Endpoint.parse("#{gateway_url}?v=9&encoding=json",
464
- alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
465
- begin
466
- Async::WebSocket::Client.connect(endpoint, headers: [["User-Agent", Discorb::USER_AGENT]]) do |connection|
467
- @connection = connection
468
- while (message = @connection.read)
469
- handle_gateway(message)
454
+ #
455
+ # A module to handle gateway events.
456
+ #
457
+ module Handler
458
+ private
459
+
460
+ def connect_gateway(first)
461
+ @log.info "Connecting to gateway."
462
+ Async do
463
+ @http = HTTP.new(self) if first
464
+ @first = first
465
+ _, gateway_response = @http.get("/gateway").wait
466
+ gateway_url = gateway_response[:url]
467
+ endpoint = Async::HTTP::Endpoint.parse("#{gateway_url}?v=9&encoding=json",
468
+ alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
469
+ begin
470
+ Async::WebSocket::Client.connect(endpoint, headers: [["User-Agent", Discorb::USER_AGENT]]) do |connection|
471
+ @connection = connection
472
+ while (message = @connection.read)
473
+ handle_gateway(message)
474
+ end
470
475
  end
471
- end
472
- rescue Protocol::WebSocket::ClosedError => e
473
- case e.message
474
- when "Authentication failed."
475
- @tasks.map(&:stop)
476
- raise ClientError.new("Authentication failed."), cause: nil
477
- when "Discord WebSocket requesting client reconnect."
478
- @log.info "Discord WebSocket requesting client reconnect"
479
- @tasks.map(&:stop)
476
+ rescue Protocol::WebSocket::ClosedError => e
477
+ case e.message
478
+ when "Authentication failed."
479
+ @tasks.map(&:stop)
480
+ raise ClientError.new("Authentication failed."), cause: nil
481
+ when "Discord WebSocket requesting client reconnect."
482
+ @log.info "Discord WebSocket requesting client reconnect"
483
+ @tasks.map(&:stop)
484
+ connect_gateway(false)
485
+ end
486
+ rescue EOFError, Async::Wrapper::Cancelled
480
487
  connect_gateway(false)
481
488
  end
482
- rescue EOFError, Async::Wrapper::Cancelled
483
- connect_gateway(false)
484
489
  end
485
490
  end
486
- end
487
-
488
- def send_gateway(opcode, **value)
489
- @connection.write({ op: opcode, d: value })
490
- @connection.flush
491
- @log.debug "Sent message with opcode #{opcode}: #{value.to_json.gsub(@token, "[Token]")}"
492
- end
493
491
 
494
- def handle_gateway(payload)
495
- Async do |task|
496
- data = payload[:d]
497
- @last_s = payload[:s] if payload[:s]
498
- @log.debug "Received message with opcode #{payload[:op]} from gateway: #{data}"
499
- case payload[:op]
500
- when 10
501
- @heartbeat_interval = data[:heartbeat_interval]
502
- @tasks << handle_heartbeat(@heartbeat_interval)
503
- if @first
504
- payload = {
505
- token: @token,
506
- intents: @intents.value,
507
- compress: false,
508
- properties: { "$os" => RUBY_PLATFORM, "$browser" => "discorb", "$device" => "discorb" },
509
- }
510
- payload[:presence] = @identify_presence if @identify_presence
511
- send_gateway(2, **payload)
512
- else
513
- payload = {
514
- token: @token,
515
- session_id: @session_id,
516
- seq: @last_s,
517
- }
518
- send_gateway(6, **payload)
519
- end
520
- when 9
521
- @log.warn "Received opcode 9, closed connection"
522
- @tasks.map(&:stop)
523
- if data
524
- @log.info "Connection is resumable, reconnecting"
525
- @connection.close
526
- connect_gateway(false)
527
- else
528
- @log.info "Connection is not resumable, reconnecting with opcode 2"
529
- task.sleep(2)
530
- @connection.close
531
- connect_gateway(true)
492
+ def send_gateway(opcode, **value)
493
+ @connection.write({ op: opcode, d: value })
494
+ @connection.flush
495
+ @log.debug "Sent message with opcode #{opcode}: #{value.to_json.gsub(@token, "[Token]")}"
496
+ end
497
+
498
+ def handle_gateway(payload)
499
+ Async do |task|
500
+ data = payload[:d]
501
+ @last_s = payload[:s] if payload[:s]
502
+ @log.debug "Received message with opcode #{payload[:op]} from gateway: #{data}"
503
+ case payload[:op]
504
+ when 10
505
+ @heartbeat_interval = data[:heartbeat_interval]
506
+ @tasks << handle_heartbeat(@heartbeat_interval)
507
+ if @first
508
+ payload = {
509
+ token: @token,
510
+ intents: @intents.value,
511
+ compress: false,
512
+ properties: { "$os" => RUBY_PLATFORM, "$browser" => "discorb", "$device" => "discorb" },
513
+ }
514
+ payload[:presence] = @identify_presence if @identify_presence
515
+ send_gateway(2, **payload)
516
+ else
517
+ payload = {
518
+ token: @token,
519
+ session_id: @session_id,
520
+ seq: @last_s,
521
+ }
522
+ send_gateway(6, **payload)
523
+ end
524
+ when 9
525
+ @log.warn "Received opcode 9, closed connection"
526
+ @tasks.map(&:stop)
527
+ if data
528
+ @log.info "Connection is resumable, reconnecting"
529
+ @connection.close
530
+ connect_gateway(false)
531
+ else
532
+ @log.info "Connection is not resumable, reconnecting with opcode 2"
533
+ task.sleep(2)
534
+ @connection.close
535
+ connect_gateway(true)
536
+ end
537
+ when 11
538
+ @log.info "Received opcode 11"
539
+ @ping = Time.now.to_f - @heartbeat_before
540
+ when 0
541
+ handle_event(payload[:t], data)
532
542
  end
533
- when 0
534
- handle_event(payload[:t], data)
535
543
  end
536
544
  end
537
- end
538
545
 
539
- def handle_heartbeat(interval)
540
- Async do |task|
541
- task.sleep((interval / 1000.0 - 1) * rand)
542
- loop do
543
- @connection.write({ op: 1, d: @last_s })
544
- @connection.flush
545
- @log.debug "Sent opcode 1."
546
- @log.debug "Waiting for heartbeat."
547
- sleep(interval / 1000.0 - 1)
546
+ def handle_heartbeat(interval)
547
+ Async do |task|
548
+ task.sleep((interval / 1000.0 - 1) * rand)
549
+ loop do
550
+ @heartbeat_before = Time.now.to_f
551
+ @connection.write({ op: 1, d: @last_s })
552
+ @connection.flush
553
+ @log.debug "Sent opcode 1."
554
+ @log.debug "Waiting for heartbeat."
555
+ sleep(interval / 1000.0 - 1)
556
+ end
548
557
  end
549
558
  end
550
- end
551
559
 
552
- def handle_event(event_name, data)
553
- return @log.debug "Client isn't ready; event #{event_name} wasn't handled" if @wait_until_ready && !@ready && !%w[READY GUILD_CREATE].include?(event_name)
554
-
555
- dispatch(:event_receive, event_name, data)
556
- @log.debug "Handling event #{event_name}"
557
- case event_name
558
- when "READY"
559
- @api_version = data[:v]
560
- @session_id = data[:session_id]
561
- @user = ClientUser.new(self, data[:user])
562
- @uncached_guilds = data[:guilds].map { |g| g[:id] }
563
- when "GUILD_CREATE"
564
- if @uncached_guilds.include?(data[:id])
565
- Guild.new(self, data, true)
566
- @uncached_guilds.delete(data[:id])
567
- if @uncached_guilds == []
568
- @ready = true
569
- dispatch(:ready)
570
- @log.info("Guilds were cached")
560
+ def handle_event(event_name, data)
561
+ return @log.debug "Client isn't ready; event #{event_name} wasn't handled" if @wait_until_ready && !@ready && !%w[READY GUILD_CREATE].include?(event_name)
562
+
563
+ dispatch(:event_receive, event_name, data)
564
+ @log.debug "Handling event #{event_name}"
565
+ case event_name
566
+ when "READY"
567
+ @api_version = data[:v]
568
+ @session_id = data[:session_id]
569
+ @user = ClientUser.new(self, data[:user])
570
+ @uncached_guilds = data[:guilds].map { |g| g[:id] }
571
+ when "GUILD_CREATE"
572
+ if @uncached_guilds.include?(data[:id])
573
+ Guild.new(self, data, true)
574
+ @uncached_guilds.delete(data[:id])
575
+ if @uncached_guilds == []
576
+ @ready = true
577
+ setup_commands.wait if @overwrite_application_commands
578
+ dispatch(:ready)
579
+ @log.info("Guilds were cached")
580
+ end
581
+ elsif @guilds.has?(data[:id])
582
+ @guilds[data[:id]].send(:_set_data, data)
583
+ dispatch(:guild_available, guild)
584
+ else
585
+ guild = Guild.new(self, data, true)
586
+ dispatch(:guild_join, guild)
571
587
  end
572
- elsif @guilds.has?(data[:id])
573
- @guilds[data[:id]].send(:_set_data, data)
574
- dispatch(:guild_available, guild)
575
- else
576
- guild = Guild.new(self, data, true)
577
- dispatch(:guild_join, guild)
578
- end
579
- dispatch(:guild_create, @guilds[data[:id]])
580
- when "MESSAGE_CREATE"
581
- message = Message.new(self, data)
582
- dispatch(:message, message)
583
- when "GUILD_UPDATE"
584
- if @guilds.has?(data[:id])
585
- current = @guilds[data[:id]]
586
- before = Guild.new(self, current.instance_variable_get(:@data).merge(no_cache: true), false)
587
- current.send(:_set_data, data, false)
588
- dispatch(:guild_update, before, current)
589
- else
590
- @log.warn "Unknown guild id #{data[:id]}, ignoring"
591
- end
592
- when "GUILD_DELETE"
593
- return @log.warn "Unknown guild id #{data[:id]}, ignoring" unless (guild = @guilds.delete(data[:id]))
594
-
595
- dispatch(:guild_delete, guild)
596
- if guild.has?(:unavailable)
597
- dispatch(:guild_destroy, guild)
598
- else
599
- dispatch(:guild_leave, guild)
600
- end
601
- when "GUILD_ROLE_CREATE"
602
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
603
-
604
- nr = Role.new(@client, guild, data[:role])
605
- guild.roles[data[:role][:id]] = nr
606
- dispatch(:role_create, nr)
607
- when "GUILD_ROLE_UPDATE"
608
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
609
- return @log.warn "Unknown role id #{data[:role][:id]}, ignoring" unless guild.roles.has?(data[:role][:id])
610
-
611
- current = guild.roles[data[:role][:id]]
612
- before = Role.new(@client, guild, current.instance_variable_get(:@data).update({ no_cache: true }))
613
- current.send(:_set_data, data[:role])
614
- dispatch(:role_update, before, current)
615
- when "GUILD_ROLE_DELETE"
616
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
617
- return @log.warn "Unknown role id #{data[:role_id]}, ignoring" unless (role = guild.roles.delete(data[:role_id]))
618
-
619
- dispatch(:role_delete, role)
620
- when "CHANNEL_CREATE"
621
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
622
-
623
- nc = Channel.make_channel(self, data)
624
- guild.channels[data[:id]] = nc
625
-
626
- dispatch(:channel_create, nc)
627
- when "CHANNEL_UPDATE"
628
- return @log.warn "Unknown channel id #{data[:id]}, ignoring" unless (current = @channels[data[:id]])
629
-
630
- before = Channel.make_channel(self, current.instance_variable_get(:@data), no_cache: true)
631
- current.send(:_set_data, data)
632
- dispatch(:channel_update, before, current)
633
- when "CHANNEL_DELETE"
634
- return @log.warn "Unknown channel id #{data[:id]}, ignoring" unless (channel = @channels.delete(data[:id]))
635
-
636
- @guilds[data[:guild_id]]&.channels&.delete(data[:id])
637
- dispatch(:channel_delete, channel)
638
- when "CHANNEL_PINS_UPDATE"
639
- nil # do in MESSAGE_UPDATE
640
- when "THREAD_CREATE"
641
- thread = Channel.make_channel(self, data)
642
-
643
- dispatch(:thread_create, thread)
644
- if data.key?(:member)
645
- dispatch(:thread_join, thread)
646
- else
647
- dispatch(:thread_new, thread)
648
- end
649
- when "THREAD_UPDATE"
650
- return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
651
-
652
- before = Channel.make_channel(self, thread.instance_variable_get(:@data), no_cache: true)
653
- thread.send(:_set_data, data)
654
- dispatch(:thread_update, before, thread)
655
- when "THREAD_DELETE"
656
- return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels.delete(data[:id]))
657
-
658
- @guilds[data[:guild_id]]&.channels&.delete(data[:id])
659
- dispatch(:thread_delete, thread)
660
- when "THREAD_LIST_SYNC"
661
- data[:threads].each do |raw_thread|
662
- thread = Channel.make_channel(self, raw_thread.merge({ member: raw_thread[:members].find { |m| m[:id] == raw_thread[:id] } }))
663
- @channels[thread.id] = thread
664
- end
665
- when "THREAD_MEMBER_UPDATE"
666
- return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
667
-
668
- if (member = thread.members[data[:id]])
669
- old = ThreadChannel::Member.new(self, member.instance_variable_get(:@data))
670
- member._set_data(data)
671
- else
672
- old = nil
673
- member = ThreadChannel::Member.new(self, data)
674
- thread.members[data[:user_id]] = member
675
- end
676
- dispatch(:thread_member_update, thread, old, member)
677
- when "THREAD_MEMBERS_UPDATE"
678
- return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
679
-
680
- thread.instance_variable_set(:@member_count, data[:member_count])
681
- members = []
682
- (data[:added_members] || []).each do |raw_member|
683
- member = ThreadChannel::Member.new(self, raw_member)
684
- thread.members[member.id] = member
685
- members << member
686
- end
687
- removed_members = []
688
- (data[:removed_member_ids] || []).each do |id|
689
- removed_members << thread.members.delete(id)
690
- end
691
- dispatch(:thread_members_update, thread, members, removed_members)
692
- when "STAGE_INSTANCE_CREATE"
693
- instance = StageInstance.new(self, data)
694
- dispatch(:stage_instance_create, instance)
695
- when "STAGE_INSTANCE_UPDATE"
696
- return @log.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
697
- return @log.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances[data[:id]])
698
-
699
- old = StageInstance.new(self, instance.instance_variable_get(:@data), no_cache: true)
700
- current.send(:_set_data, data)
701
- dispatch(:stage_instance_update, old, current)
702
- when "STAGE_INSTANCE_DELETE"
703
- return @log.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
704
- return @log.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances.delete(data[:id]))
705
-
706
- dispatch(:stage_instance_delete, instance)
707
- when "GUILD_MEMBER_ADD"
708
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
709
-
710
- nm = Member.new(self, data[:guild_id], data[:user].update({ no_cache: true }), data)
711
- guild.members[nm.id] = nm
712
- dispatch(:member_add, nm)
713
- when "GUILD_MEMBER_UPDATE"
714
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
715
- return @log.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (nm = guild.members[data[:user][:id]])
716
-
717
- old = Member.new(self, data[:guild_id], data[:user], data.update({ no_cache: true }))
718
- nm.send(:_set_data, data[:user], data)
719
- dispatch(:member_update, old, nm)
720
- when "GUILD_MEMBER_REMOVE"
721
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
722
- return @log.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (member = guild.members.delete(data[:user][:id]))
723
-
724
- dispatch(:member_remove, member)
725
- when "GUILD_BAN_ADD"
726
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
727
-
728
- user = if @users.has? data[:user][:id]
729
- @users[data[:user][:id]]
588
+ dispatch(:guild_create, @guilds[data[:id]])
589
+ when "MESSAGE_CREATE"
590
+ message = Message.new(self, data)
591
+ dispatch(:message, message)
592
+ when "GUILD_UPDATE"
593
+ if @guilds.has?(data[:id])
594
+ current = @guilds[data[:id]]
595
+ before = Guild.new(self, current.instance_variable_get(:@data).merge(no_cache: true), false)
596
+ current.send(:_set_data, data, false)
597
+ dispatch(:guild_update, before, current)
730
598
  else
731
- User.new(self, data[:user].update({ no_cache: true }))
599
+ @log.warn "Unknown guild id #{data[:id]}, ignoring"
732
600
  end
601
+ when "GUILD_DELETE"
602
+ return @log.warn "Unknown guild id #{data[:id]}, ignoring" unless (guild = @guilds.delete(data[:id]))
733
603
 
734
- dispatch(:guild_ban_add, guild, user)
735
- when "GUILD_BAN_REMOVE"
736
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
604
+ dispatch(:guild_delete, guild)
605
+ if guild.has?(:unavailable)
606
+ dispatch(:guild_destroy, guild)
607
+ else
608
+ dispatch(:guild_leave, guild)
609
+ end
610
+ when "GUILD_ROLE_CREATE"
611
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
612
+
613
+ nr = Role.new(@client, guild, data[:role])
614
+ guild.roles[data[:role][:id]] = nr
615
+ dispatch(:role_create, nr)
616
+ when "GUILD_ROLE_UPDATE"
617
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
618
+ return @log.warn "Unknown role id #{data[:role][:id]}, ignoring" unless guild.roles.has?(data[:role][:id])
619
+
620
+ current = guild.roles[data[:role][:id]]
621
+ before = Role.new(@client, guild, current.instance_variable_get(:@data).update({ no_cache: true }))
622
+ current.send(:_set_data, data[:role])
623
+ dispatch(:role_update, before, current)
624
+ when "GUILD_ROLE_DELETE"
625
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
626
+ return @log.warn "Unknown role id #{data[:role_id]}, ignoring" unless (role = guild.roles.delete(data[:role_id]))
627
+
628
+ dispatch(:role_delete, role)
629
+ when "CHANNEL_CREATE"
630
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
631
+
632
+ nc = Channel.make_channel(self, data)
633
+ guild.channels[data[:id]] = nc
634
+
635
+ dispatch(:channel_create, nc)
636
+ when "CHANNEL_UPDATE"
637
+ return @log.warn "Unknown channel id #{data[:id]}, ignoring" unless (current = @channels[data[:id]])
638
+
639
+ before = Channel.make_channel(self, current.instance_variable_get(:@data), no_cache: true)
640
+ current.send(:_set_data, data)
641
+ dispatch(:channel_update, before, current)
642
+ when "CHANNEL_DELETE"
643
+ return @log.warn "Unknown channel id #{data[:id]}, ignoring" unless (channel = @channels.delete(data[:id]))
644
+
645
+ @guilds[data[:guild_id]]&.channels&.delete(data[:id])
646
+ dispatch(:channel_delete, channel)
647
+ when "CHANNEL_PINS_UPDATE"
648
+ nil # do in MESSAGE_UPDATE
649
+ when "THREAD_CREATE"
650
+ thread = Channel.make_channel(self, data)
651
+
652
+ dispatch(:thread_create, thread)
653
+ if data.key?(:member)
654
+ dispatch(:thread_join, thread)
655
+ else
656
+ dispatch(:thread_new, thread)
657
+ end
658
+ when "THREAD_UPDATE"
659
+ return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
660
+
661
+ before = Channel.make_channel(self, thread.instance_variable_get(:@data), no_cache: true)
662
+ thread.send(:_set_data, data)
663
+ dispatch(:thread_update, before, thread)
664
+ when "THREAD_DELETE"
665
+ return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels.delete(data[:id]))
666
+
667
+ @guilds[data[:guild_id]]&.channels&.delete(data[:id])
668
+ dispatch(:thread_delete, thread)
669
+ when "THREAD_LIST_SYNC"
670
+ data[:threads].each do |raw_thread|
671
+ thread = Channel.make_channel(self, raw_thread.merge({ member: raw_thread[:members].find { |m| m[:id] == raw_thread[:id] } }))
672
+ @channels[thread.id] = thread
673
+ end
674
+ when "THREAD_MEMBER_UPDATE"
675
+ return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
737
676
 
738
- user = if @users.has? data[:user][:id]
739
- @users[data[:user][:id]]
677
+ if (member = thread.members[data[:id]])
678
+ old = ThreadChannel::Member.new(self, member.instance_variable_get(:@data))
679
+ member._set_data(data)
740
680
  else
741
- User.new(self, data[:user].update({ no_cache: true }))
681
+ old = nil
682
+ member = ThreadChannel::Member.new(self, data)
683
+ thread.members[data[:user_id]] = member
684
+ end
685
+ dispatch(:thread_member_update, thread, old, member)
686
+ when "THREAD_MEMBERS_UPDATE"
687
+ return @log.warn "Unknown thread id #{data[:id]}, ignoring" unless (thread = @channels[data[:id]])
688
+
689
+ thread.instance_variable_set(:@member_count, data[:member_count])
690
+ members = []
691
+ (data[:added_members] || []).each do |raw_member|
692
+ member = ThreadChannel::Member.new(self, raw_member)
693
+ thread.members[member.id] = member
694
+ members << member
695
+ end
696
+ removed_members = []
697
+ (data[:removed_member_ids] || []).each do |id|
698
+ removed_members << thread.members.delete(id)
742
699
  end
700
+ dispatch(:thread_members_update, thread, members, removed_members)
701
+ when "STAGE_INSTANCE_CREATE"
702
+ instance = StageInstance.new(self, data)
703
+ dispatch(:stage_instance_create, instance)
704
+ when "STAGE_INSTANCE_UPDATE"
705
+ return @log.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
706
+ return @log.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances[data[:id]])
707
+
708
+ old = StageInstance.new(self, instance.instance_variable_get(:@data), no_cache: true)
709
+ current.send(:_set_data, data)
710
+ dispatch(:stage_instance_update, old, current)
711
+ when "STAGE_INSTANCE_DELETE"
712
+ return @log.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
713
+ return @log.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances.delete(data[:id]))
714
+
715
+ dispatch(:stage_instance_delete, instance)
716
+ when "GUILD_MEMBER_ADD"
717
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
718
+
719
+ nm = Member.new(self, data[:guild_id], data[:user].update({ no_cache: true }), data)
720
+ guild.members[nm.id] = nm
721
+ dispatch(:member_add, nm)
722
+ when "GUILD_MEMBER_UPDATE"
723
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
724
+ return @log.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (nm = guild.members[data[:user][:id]])
725
+
726
+ old = Member.new(self, data[:guild_id], data[:user], data.update({ no_cache: true }))
727
+ nm.send(:_set_data, data[:user], data)
728
+ dispatch(:member_update, old, nm)
729
+ when "GUILD_MEMBER_REMOVE"
730
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
731
+ return @log.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (member = guild.members.delete(data[:user][:id]))
732
+
733
+ dispatch(:member_remove, member)
734
+ when "GUILD_BAN_ADD"
735
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
736
+
737
+ user = if @users.has? data[:user][:id]
738
+ @users[data[:user][:id]]
739
+ else
740
+ User.new(self, data[:user].update({ no_cache: true }))
741
+ end
743
742
 
744
- dispatch(:guild_ban_remove, guild, user)
745
- when "GUILD_EMOJIS_UPDATE"
746
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
743
+ dispatch(:guild_ban_add, guild, user)
744
+ when "GUILD_BAN_REMOVE"
745
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
747
746
 
748
- before_emojis = guild.emojis.values.map(&:id).to_set
749
- data[:emojis].each do |emoji|
750
- guild.emojis[emoji[:id]] = CustomEmoji.new(self, guild, emoji)
751
- end
752
- deleted_emojis = before_emojis - guild.emojis.values.map(&:id).to_set
753
- deleted_emojis.each do |emoji|
754
- guild.emojis.delete(emoji)
755
- end
756
- when "GUILD_INTEGRATIONS_UPDATE"
757
- # dispatch(:guild_integrations_update, GuildIntegrationsUpdateEvent.new(self, data))
758
- # Currently not implemented
759
- when "INTEGRATION_CREATE"
760
- dispatch(:integration_create, Integration.new(self, data, data[:guild_id]))
761
- when "INTEGRATION_UPDATE"
762
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
763
- return @log.warn "Unknown integration id #{data[:id]}, ignoring" unless (integration = guild.integrations[data[:id]])
764
-
765
- before = Integration.new(self, integration.instance_variable_get(:@data), data[:guild_id], no_cache: true)
766
- integration.send(:_set_data, data)
767
- dispatch(:integration_update, before, integration)
768
- when "INTEGRATION_DELETE"
769
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
770
- return @log.warn "Unknown integration id #{data[:id]}, ignoring" unless (integration = guild.integrations.delete(data[:id]))
771
-
772
- dispatch(:integration_delete, integration)
773
- when "WEBHOOKS_UPDATE"
774
- dispatch(:webhooks_update, WebhooksUpdateEvent.new(self, data))
775
- when "INVITE_CREATE"
776
- dispatch(:invite_create, Invite.new(self, data, true))
777
- when "INVITE_DELETE"
778
- dispatch(:invite_delete, InviteDeleteEvent.new(self, data))
779
- when "VOICE_STATE_UPDATE"
780
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
781
-
782
- current = guild.voice_states[data[:user_id]]
783
- if current.nil?
784
- old = nil
785
- current = VoiceState.new(self, data)
786
- guild.voice_states[data[:user_id]] = current
787
- else
788
- old = VoiceState.new(self, current.instance_variable_get(:@data))
789
- current.send(:_set_data, data)
790
- end
791
- dispatch(:voice_state_update, old, current)
792
- if old&.channel != current&.channel
793
- dispatch(:voice_channel_update, old, current)
794
- case [old&.channel, current&.channel]
795
- in [nil, _]
796
- dispatch(:voice_channel_connect, current)
797
- in [_, nil]
798
- dispatch(:voice_channel_disconnect, old)
799
- in _
800
- dispatch(:voice_channel_move, old, current)
747
+ user = if @users.has? data[:user][:id]
748
+ @users[data[:user][:id]]
749
+ else
750
+ User.new(self, data[:user].update({ no_cache: true }))
751
+ end
752
+
753
+ dispatch(:guild_ban_remove, guild, user)
754
+ when "GUILD_EMOJIS_UPDATE"
755
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
756
+
757
+ before_emojis = guild.emojis.values.map(&:id).to_set
758
+ data[:emojis].each do |emoji|
759
+ guild.emojis[emoji[:id]] = CustomEmoji.new(self, guild, emoji)
801
760
  end
802
- end
803
- if old&.mute? != current&.mute?
804
- dispatch(:voice_mute_update, old, current)
805
- case [old&.mute?, current&.mute?]
806
- when [false, true]
807
- dispatch(:voice_mute_enable, current)
808
- when [true, false]
809
- dispatch(:voice_mute_disable, old)
761
+ deleted_emojis = before_emojis - guild.emojis.values.map(&:id).to_set
762
+ deleted_emojis.each do |emoji|
763
+ guild.emojis.delete(emoji)
810
764
  end
811
- end
812
- if old&.deaf? != current&.deaf?
813
- dispatch(:voice_deaf_update, old, current)
814
- case [old&.deaf?, current&.deaf?]
815
- when [false, true]
816
- dispatch(:voice_deaf_enable, current)
817
- when [true, false]
818
- dispatch(:voice_deaf_disable, old)
765
+ when "GUILD_INTEGRATIONS_UPDATE"
766
+ # dispatch(:guild_integrations_update, GuildIntegrationsUpdateEvent.new(self, data))
767
+ # Currently not implemented
768
+ when "INTEGRATION_CREATE"
769
+ dispatch(:integration_create, Integration.new(self, data, data[:guild_id]))
770
+ when "INTEGRATION_UPDATE"
771
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
772
+ return @log.warn "Unknown integration id #{data[:id]}, ignoring" unless (integration = guild.integrations[data[:id]])
773
+
774
+ before = Integration.new(self, integration.instance_variable_get(:@data), data[:guild_id], no_cache: true)
775
+ integration.send(:_set_data, data)
776
+ dispatch(:integration_update, before, integration)
777
+ when "INTEGRATION_DELETE"
778
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
779
+ return @log.warn "Unknown integration id #{data[:id]}, ignoring" unless (integration = guild.integrations.delete(data[:id]))
780
+
781
+ dispatch(:integration_delete, integration)
782
+ when "WEBHOOKS_UPDATE"
783
+ dispatch(:webhooks_update, WebhooksUpdateEvent.new(self, data))
784
+ when "INVITE_CREATE"
785
+ dispatch(:invite_create, Invite.new(self, data, true))
786
+ when "INVITE_DELETE"
787
+ dispatch(:invite_delete, InviteDeleteEvent.new(self, data))
788
+ when "VOICE_STATE_UPDATE"
789
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
790
+
791
+ current = guild.voice_states[data[:user_id]]
792
+ if current.nil?
793
+ old = nil
794
+ current = VoiceState.new(self, data)
795
+ guild.voice_states[data[:user_id]] = current
796
+ else
797
+ old = VoiceState.new(self, current.instance_variable_get(:@data))
798
+ current.send(:_set_data, data)
819
799
  end
820
- end
821
- if old&.self_mute? != current&.self_mute?
822
- dispatch(:voice_self_mute_update, old, current)
823
- case [old&.self_mute?, current&.self_mute?]
824
- when [false, true]
825
- dispatch(:voice_self_mute_enable, current)
826
- when [true, false]
827
- dispatch(:voice_self_mute_disable, old)
800
+ dispatch(:voice_state_update, old, current)
801
+ if old&.channel != current&.channel
802
+ dispatch(:voice_channel_update, old, current)
803
+ case [old&.channel, current&.channel]
804
+ in [nil, _]
805
+ dispatch(:voice_channel_connect, current)
806
+ in [_, nil]
807
+ dispatch(:voice_channel_disconnect, old)
808
+ in _
809
+ dispatch(:voice_channel_move, old, current)
810
+ end
828
811
  end
829
- end
830
- if old&.self_deaf? != current&.self_deaf?
831
- dispatch(:voice_self_deaf_update, old, current)
832
- case [old&.self_deaf?, current&.self_deaf?]
833
- when [false, true]
834
- dispatch(:voice_self_deaf_enable, current)
835
- when [true, false]
836
- dispatch(:voice_self_deaf_disable, old)
812
+ if old&.mute? != current&.mute?
813
+ dispatch(:voice_mute_update, old, current)
814
+ case [old&.mute?, current&.mute?]
815
+ when [false, true]
816
+ dispatch(:voice_mute_enable, current)
817
+ when [true, false]
818
+ dispatch(:voice_mute_disable, old)
819
+ end
837
820
  end
838
- end
839
- if old&.server_mute? != current&.server_mute?
840
- dispatch(:voice_server_mute_update, old, current)
841
- case [old&.server_mute?, current&.server_mute?]
842
- when [false, true]
843
- dispatch(:voice_server_mute_enable, current)
844
- when [true, false]
845
- dispatch(:voice_server_mute_disable, old)
821
+ if old&.deaf? != current&.deaf?
822
+ dispatch(:voice_deaf_update, old, current)
823
+ case [old&.deaf?, current&.deaf?]
824
+ when [false, true]
825
+ dispatch(:voice_deaf_enable, current)
826
+ when [true, false]
827
+ dispatch(:voice_deaf_disable, old)
828
+ end
846
829
  end
847
- end
848
- if old&.server_deaf? != current&.server_deaf?
849
- dispatch(:voice_server_deaf_update, old, current)
850
- case [old&.server_deaf?, current&.server_deaf?]
851
- when [false, true]
852
- dispatch(:voice_server_deaf_enable, current)
853
- when [true, false]
854
- dispatch(:voice_server_deaf_disable, old)
830
+ if old&.self_mute? != current&.self_mute?
831
+ dispatch(:voice_self_mute_update, old, current)
832
+ case [old&.self_mute?, current&.self_mute?]
833
+ when [false, true]
834
+ dispatch(:voice_self_mute_enable, current)
835
+ when [true, false]
836
+ dispatch(:voice_self_mute_disable, old)
837
+ end
855
838
  end
856
- end
857
- if old&.video? != current&.video?
858
- dispatch(:voice_video_update, old, current)
859
- case [old&.video?, current&.video?]
860
- when [false, true]
861
- dispatch(:voice_video_start, current)
862
- when [true, false]
863
- dispatch(:voice_video_end, old)
839
+ if old&.self_deaf? != current&.self_deaf?
840
+ dispatch(:voice_self_deaf_update, old, current)
841
+ case [old&.self_deaf?, current&.self_deaf?]
842
+ when [false, true]
843
+ dispatch(:voice_self_deaf_enable, current)
844
+ when [true, false]
845
+ dispatch(:voice_self_deaf_disable, old)
846
+ end
864
847
  end
865
- end
866
- if old&.stream? != current&.stream?
867
- dispatch(:voice_stream_update, old, current)
868
- case [old&.stream?, current&.stream?]
869
- when [false, true]
870
- dispatch(:voice_stream_start, current)
871
- when [true, false]
872
- dispatch(:voice_stream_end, old)
848
+ if old&.server_mute? != current&.server_mute?
849
+ dispatch(:voice_server_mute_update, old, current)
850
+ case [old&.server_mute?, current&.server_mute?]
851
+ when [false, true]
852
+ dispatch(:voice_server_mute_enable, current)
853
+ when [true, false]
854
+ dispatch(:voice_server_mute_disable, old)
855
+ end
873
856
  end
874
- end
875
- when "PRESENCE_UPDATE"
876
- return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
877
-
878
- guild.presences[data[:user][:id]] = Presence.new(self, data)
879
- when "MESSAGE_UPDATE"
880
- if (message = @messages[data[:id]])
881
- before = Message.new(self, message.instance_variable_get(:@data), no_cache: true)
882
- message.send(:_set_data, message.instance_variable_get(:@data).merge(data))
883
- else
884
- @log.info "Uncached message ID #{data[:id]}, ignoring"
885
- before = nil
886
- message = nil
887
- end
888
- if data[:edited_timestamp].nil?
889
- if message.nil?
890
- nil
891
- elsif message.pinned?
892
- message.instance_variable_set(:@pinned, false)
893
- else
894
- message.instance_variable_set(:@pinned, true)
857
+ if old&.server_deaf? != current&.server_deaf?
858
+ dispatch(:voice_server_deaf_update, old, current)
859
+ case [old&.server_deaf?, current&.server_deaf?]
860
+ when [false, true]
861
+ dispatch(:voice_server_deaf_enable, current)
862
+ when [true, false]
863
+ dispatch(:voice_server_deaf_disable, old)
864
+ end
895
865
  end
896
- dispatch(:message_pin_update, MessagePinEvent.new(self, data, message))
897
- else
898
- dispatch(:message_update, MessageUpdateEvent.new(self, data, before, current))
899
- end
900
- when "MESSAGE_DELETE"
901
- message.instance_variable_set(:@deleted, true) if (message = @messages[data[:id]])
902
-
903
- dispatch(:message_delete_id, Snowflake.new(data[:id]), channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
904
- dispatch(:message_delete, message, channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
905
- when "MESSAGE_DELETE_BULK"
906
- messages = []
907
- data[:ids].each do |id|
908
- if (message = @messages[id])
909
- message.instance_variable_set(:@deleted, true)
910
- messages.push(message)
866
+ if old&.video? != current&.video?
867
+ dispatch(:voice_video_update, old, current)
868
+ case [old&.video?, current&.video?]
869
+ when [false, true]
870
+ dispatch(:voice_video_start, current)
871
+ when [true, false]
872
+ dispatch(:voice_video_end, old)
873
+ end
874
+ end
875
+ if old&.stream? != current&.stream?
876
+ dispatch(:voice_stream_update, old, current)
877
+ case [old&.stream?, current&.stream?]
878
+ when [false, true]
879
+ dispatch(:voice_stream_start, current)
880
+ when [true, false]
881
+ dispatch(:voice_stream_end, old)
882
+ end
883
+ end
884
+ when "PRESENCE_UPDATE"
885
+ return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
886
+
887
+ guild.presences[data[:user][:id]] = Presence.new(self, data)
888
+ when "MESSAGE_UPDATE"
889
+ if (message = @messages[data[:id]])
890
+ before = Message.new(self, message.instance_variable_get(:@data), no_cache: true)
891
+ message.send(:_set_data, message.instance_variable_get(:@data).merge(data))
911
892
  else
912
- messages.push(UnknownDeleteBulkMessage.new(self, id))
893
+ before = nil
894
+ message = nil
913
895
  end
914
- end
915
- dispatch(:message_delete_bulk, messages)
916
- when "MESSAGE_REACTION_ADD"
917
- if (target_message = @messages[data[:message_id]])
918
- if (target_reaction = target_message.reactions.find do |r|
919
- r.emoji.is_a?(UnicodeEmoji) ? r.emoji.value == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
920
- end)
921
- target_reaction.set_instance_variable(:@count, target_reaction.count + 1)
896
+ if data[:edited_timestamp].nil?
897
+ if message.nil?
898
+ nil
899
+ elsif message.pinned?
900
+ message.instance_variable_set(:@pinned, false)
901
+ else
902
+ message.instance_variable_set(:@pinned, true)
903
+ end
904
+ dispatch(:message_pin_update, MessagePinEvent.new(self, data, message))
922
905
  else
923
- target_message.reactions << Reaction.new(
924
- self,
925
- {
926
- count: 1,
927
- me: @user.id == data[:user_id],
928
- emoji: data[:emoji],
929
- }
930
- )
906
+ dispatch(:message_update, MessageUpdateEvent.new(self, data, before, current))
931
907
  end
908
+ when "MESSAGE_DELETE"
909
+ message.instance_variable_set(:@deleted, true) if (message = @messages[data[:id]])
910
+
911
+ dispatch(:message_delete_id, Snowflake.new(data[:id]), channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
912
+ dispatch(:message_delete, message, channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
913
+ when "MESSAGE_DELETE_BULK"
914
+ messages = []
915
+ data[:ids].each do |id|
916
+ if (message = @messages[id])
917
+ message.instance_variable_set(:@deleted, true)
918
+ messages.push(message)
919
+ else
920
+ messages.push(UnknownDeleteBulkMessage.new(self, id))
921
+ end
922
+ end
923
+ dispatch(:message_delete_bulk, messages)
924
+ when "MESSAGE_REACTION_ADD"
925
+ if (target_message = @messages[data[:message_id]])
926
+ if (target_reaction = target_message.reactions.find do |r|
927
+ r.emoji.is_a?(UnicodeEmoji) ? r.emoji.value == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
928
+ end)
929
+ target_reaction.set_instance_variable(:@count, target_reaction.count + 1)
930
+ else
931
+ target_message.reactions << Reaction.new(
932
+ self,
933
+ {
934
+ count: 1,
935
+ me: @user.id == data[:user_id],
936
+ emoji: data[:emoji],
937
+ }
938
+ )
939
+ end
940
+ end
941
+ dispatch(:reaction_add, ReactionEvent.new(self, data))
942
+ when "MESSAGE_REACTION_REMOVE"
943
+ if (target_message = @messages[data[:message_id]]) &&
944
+ (target_reaction = target_message.reactions.find do |r|
945
+ data[:emoji][:id].nil? ? r.emoji.name == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
946
+ end)
947
+ target_reaction.set_instance_variable(:@count, target_reaction.count - 1)
948
+ target_message.reactions.delete(target_reaction) if target_reaction.count.zero?
949
+ end
950
+ dispatch(:reaction_remove, ReactionEvent.new(self, data))
951
+ when "MESSAGE_REACTION_REMOVE_ALL"
952
+ if (target_message = @messages[data[:message_id]])
953
+ target_message.reactions = []
954
+ end
955
+ dispatch(:reaction_remove_all, ReactionRemoveAllEvent.new(self, data))
956
+ when "MESSAGE_REACTION_REMOVE_EMOJI"
957
+ if (target_message = @messages[data[:message_id]]) &&
958
+ (target_reaction = target_message.reactions.find { |r| data[:emoji][:id].nil? ? r.name == data[:emoji][:name] : r.id == data[:emoji][:id] })
959
+ target_message.reactions.delete(target_reaction)
960
+ end
961
+ dispatch(:reaction_remove_emoji, ReactionRemoveEmojiEvent.new(data))
962
+ when "TYPING_START"
963
+ dispatch(:typing_start, TypingStartEvent.new(self, data))
964
+ when "INTERACTION_CREATE"
965
+ interaction = Interaction.make_interaction(self, data)
966
+ dispatch(:integration_create, interaction)
967
+
968
+ dispatch(interaction.class.event_name, interaction)
969
+ when "RESUMED"
970
+ @log.info("Successfully resumed connection")
971
+ dispatch(:resumed)
972
+ else
973
+ @log.warn "Unknown event: #{event_name}\n#{data.inspect}"
932
974
  end
933
- dispatch(:reaction_add, ReactionEvent.new(self, data))
934
- when "MESSAGE_REACTION_REMOVE"
935
- if (target_message = @messages[data[:message_id]]) &&
936
- (target_reaction = target_message.reactions.find do |r|
937
- data[:emoji][:id].nil? ? r.emoji.name == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
938
- end)
939
- target_reaction.set_instance_variable(:@count, target_reaction.count - 1)
940
- target_message.reactions.delete(target_reaction) if target_reaction.count.zero?
941
- end
942
- dispatch(:reaction_remove, ReactionEvent.new(self, data))
943
- when "MESSAGE_REACTION_REMOVE_ALL"
944
- if (target_message = @messages[data[:message_id]])
945
- target_message.reactions = []
946
- end
947
- dispatch(:reaction_remove_all, ReactionRemoveAllEvent.new(self, data))
948
- when "MESSAGE_REACTION_REMOVE_EMOJI"
949
- if (target_message = @messages[data[:message_id]]) &&
950
- (target_reaction = target_message.reactions.find { |r| data[:emoji][:id].nil? ? r.name == data[:emoji][:name] : r.id == data[:emoji][:id] })
951
- target_message.reactions.delete(target_reaction)
952
- end
953
- dispatch(:reaction_remove_emoji, ReactionRemoveEmojiEvent.new(data))
954
- when "TYPING_START"
955
- dispatch(:typing_start, TypingStartEvent.new(self, data))
956
- when "INTERACTION_CREATE"
957
- interaction = Interaction.make_interaction(self, data)
958
- dispatch(:integration_create, interaction)
959
-
960
- dispatch(interaction.class.event_name, interaction)
961
- when "RESUMED"
962
- @log.info("Successfully resumed connection")
963
- dispatch(:resumed)
964
- else
965
- @log.warn "Unknown event: #{event_name}\n#{data.inspect}"
966
975
  end
967
976
  end
968
977
  end
969
- end
978
+ end