mij-discord 1.0.10 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -81,7 +81,10 @@ module MijDiscord::Cache
81
81
  return nil if local
82
82
 
83
83
  begin
84
- response = MijDiscord::Core::API::User.resolve(@bot.auth, id)
84
+ response = case @bot.auth.type
85
+ when :bot then MijDiscord::Core::API::User.resolve(@bot.auth, id)
86
+ when :user then MijDiscord::Core::API::User.resolve2(@bot.auth, id)
87
+ end
85
88
  rescue MijDiscord::Errors::NotFound
86
89
  return nil
87
90
  end
@@ -275,7 +278,10 @@ module MijDiscord::Cache
275
278
  return nil if local
276
279
 
277
280
  begin
278
- response = MijDiscord::Core::API::Channel.message(@bot.auth, @channel.id, key)
281
+ response = case @bot.auth.type
282
+ when :bot then MijDiscord::Core::API::Channel.message(@bot.auth, @channel.id, key)
283
+ when :user then MijDiscord::Core::API::Channel.messages(@bot.auth, @channel.id, 1, nil, nil, key)
284
+ end
279
285
  rescue MijDiscord::Errors::NotFound
280
286
  return nil
281
287
  end
@@ -12,6 +12,9 @@ module MijDiscord::Core::API
12
12
  bot_name = auth.name || 'generic'
13
13
  ua_base = "DiscordBot (https://github.com/Mijyuoon/mij-discord, v#{MijDiscord::VERSION})"
14
14
  "#{ua_base} mij-discord/#{MijDiscord::VERSION} #{bot_name}"
15
+
16
+ when :user
17
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0.1'
15
18
  end
16
19
  end
17
20
 
@@ -461,5 +461,18 @@ module MijDiscord::Core::API::Server
461
461
  Authorization: auth
462
462
  )
463
463
  end
464
+
465
+ # Search messages (for userbots only)
466
+ # Not officially documented, reverse engineered from tracking Discord's network activity
467
+ def search_messages(auth, server_id, options)
468
+ options = URI.encode_www_form(options)
469
+ MijDiscord::Core::API.request(
470
+ :guilds_guild_messages_search,
471
+ server_id,
472
+ :get,
473
+ "#{MijDiscord::Core::API::APIBASE_URL}/guilds/#{server_id}/messages/search?#{options}",
474
+ Authorization: auth
475
+ )
476
+ end
464
477
  end
465
478
  end
@@ -14,6 +14,18 @@ module MijDiscord::Core::API::User
14
14
  )
15
15
  end
16
16
 
17
+ # Get profile data (for userbots only)
18
+ # Not officially documented, reverse engineered from tracking Discord's network activity
19
+ def resolve2(auth, user_id)
20
+ MijDiscord::Core::API.request(
21
+ :users_uid,
22
+ nil,
23
+ :get,
24
+ "#{MijDiscord::Core::API::APIBASE_URL}/users/#{user_id}/profile",
25
+ Authorization: auth
26
+ )
27
+ end
28
+
17
29
  # Get current user data
18
30
  # https://discordapp.com/developers/docs/resources/user#get-current-user
19
31
  def profile(auth)
@@ -20,14 +20,18 @@ module MijDiscord::Data
20
20
  alias_method :eql?, :==
21
21
 
22
22
  def creation_time
23
- ms = (@id >> 22) + DISCORD_EPOCH
24
- Time.at(ms / 1000.0).utc
23
+ IDObject.timestamp(@id)
25
24
  end
26
25
 
27
26
  def self.synthesize(time)
28
27
  ms = (time.to_f * 1000).to_i
29
28
  (ms - DISCORD_EPOCH) << 22
30
29
  end
30
+
31
+ def self.timestamp(id)
32
+ ms = (id >> 22) + DISCORD_EPOCH
33
+ Time.at(ms / 1000.0).utc
34
+ end
31
35
  end
32
36
 
33
37
  module PermissionObject
@@ -63,13 +63,11 @@ module MijDiscord::Data
63
63
  @parent_id = data.fetch('parent_id', @parent_id).to_i
64
64
 
65
65
  if (perms = data['permission_overwrites'])
66
- @permission_overwrites = {}
67
-
68
- perms.each do |elem|
69
- id = elem['id'].to_i
70
- @permission_overwrites[id] = Overwrite.from_hash(elem)
66
+ @permission_overwrites = perms.each_with_object({}) do |el, p|
67
+ p[el['id'].to_i] = Overwrite.from_hash(el)
71
68
  end
72
69
  end
70
+ @permission_overwrites ||= {}
73
71
  end
74
72
 
75
73
  def mention
@@ -81,30 +81,30 @@ module MijDiscord::Data
81
81
 
82
82
  @mention_everyone = !!data['mention_everyone']
83
83
 
84
- @reactions = []
85
84
  if (reactions = data['reactions'])
86
- reactions.each {|x| @reactions << Reaction.new(x, self) }
85
+ @reactions = reactions.map {|x| Reaction.new(x, self) }
87
86
  end
87
+ @reactions ||= []
88
88
 
89
- @user_mentions = []
90
89
  if (mentions = data['mentions'])
91
- mentions.each {|x| @user_mentions << @bot.cache.put_user(x) }
90
+ @user_mentions = mentions.map {|x| @bot.cache.put_user(x) }
92
91
  end
92
+ @user_mentions ||= []
93
93
 
94
- @role_mentions = []
95
94
  if @channel.text? && (mentions = data['mention_roles'])
96
- mentions.each {|x| @role_mentions << @channel.server.role(x) }
95
+ @role_mentions = mentions.map {|x| @channel.server.role(x) }
97
96
  end
97
+ @role_mentions ||= []
98
98
 
99
- @attachments = []
100
99
  if (attachments = data['attachments'])
101
- attachments.each {|x| @attachments << Attachment.new(x, self) }
100
+ @attachments = attachments.map {|x| Attachment.new(x, self) }
102
101
  end
102
+ @attachments ||= []
103
103
 
104
- @embeds = []
105
104
  if (embeds = data['embeds'])
106
- embeds.each {|x| @embeds << Embed.new(x) }
105
+ @embeds = embeds.map {|x| Embed.new(x) }
107
106
  end
107
+ @embeds ||= []
108
108
  end
109
109
 
110
110
  def update_reaction(add: nil, remove: nil, clear: false)
@@ -67,7 +67,6 @@ module MijDiscord::Data
67
67
  @id = data['id'].to_i
68
68
  @large = data['large']
69
69
  @member_count = data['member_count']
70
- @members_chunked = 0
71
70
 
72
71
  @cache = MijDiscord::Cache::ServerCache.new(self, @bot)
73
72
 
@@ -107,11 +106,12 @@ module MijDiscord::Data
107
106
  end
108
107
 
109
108
  def update_emojis(data)
110
- @emojis = {}
111
- data['emojis'].each do |em|
112
- emoji = MijDiscord::Data::Emoji.new(em, self)
113
- @emojis[emoji.id] = emoji
109
+ if (emojis = data['emojis'])
110
+ @emojis = emojis.each_with_object({}) do |el, p|
111
+ p[el['id'].to_i] = Emoji.new(el, self)
112
+ end
114
113
  end
114
+ @emojis ||= {}
115
115
  end
116
116
 
117
117
  def update_voice_state(data)
@@ -136,7 +136,7 @@ module MijDiscord::Data
136
136
  return if @members_chunked.nil?
137
137
 
138
138
  @members_chunked += data.length
139
- data.each {|mb| @cache.put_member(mb) }
139
+ data.each {|mb| @cache.put_member(mb, update: true) }
140
140
 
141
141
  @members_chunked = nil if @members_chunked == @member_count
142
142
  end
@@ -183,12 +183,14 @@ module MijDiscord::Data
183
183
  @cache.get_role(id)
184
184
  end
185
185
 
186
- def members
187
- unless @members_chunked.nil?
186
+ def members(full = false)
187
+ if full && !@members_chunk_sent
188
+ @members_chunked, @members_chunk_sent = 0, true
188
189
  @bot.gateway.send_request_members(@id, '', 0)
189
- sleep(0.05) while @members_chunked
190
190
  end
191
191
 
192
+ sleep(0.05) while @members_chunked
193
+
192
194
  @cache.list_members
193
195
  end
194
196
 
@@ -396,10 +398,73 @@ module MijDiscord::Data
396
398
  MijDiscord::Core::API.widget_url(@id, style)
397
399
  end
398
400
 
401
+ def search_messages(limit: 25, offset: 0, sort_by: nil, sort_asc: false,
402
+ nsfw: true, around: 2, content: nil, author: nil, channel: nil, mentions: nil,
403
+ author_is: nil, author_not: nil, has: nil, has_not: nil, before: nil, after: nil)
404
+ author = author ? [*author].map(&:to_id) : nil
405
+ channel = channel ? [*channel].map(&:to_id) : nil
406
+ mentions = mentions ? [*mentions].map(&:to_id) : nil
407
+
408
+ has_not = has_not ? [*has_not].map {|x| "-#{x}" } : []
409
+ author_not = author_not ? [*author_not].map {|x| "-#{x}" } : []
410
+
411
+ has = has_not | (has ? [*has].map(&:to_s) : [])
412
+ author_is = author_not | (author_is ? [*author_is].map(&:to_s) : [])
413
+
414
+ before = before ? IDObject.synthesize(before) : nil
415
+ after = after ? IDObject.synthesize(after) : nil
416
+
417
+ options = {
418
+ limit: limit,
419
+ offset: offset,
420
+ sort_by: sort_by,
421
+ sort_order: sort_asc ? 'asc' : 'desc',
422
+ context_size: around,
423
+ include_nsfw: nsfw,
424
+ author_id: author,
425
+ author_type: author_is,
426
+ channel_id: channel,
427
+ mentions: mentions,
428
+ max_id: before,
429
+ min_id: after,
430
+ content: content,
431
+ has: has,
432
+ }.delete_if {|_,v| v.nil? }
433
+
434
+ response = MijDiscord::Core::API::Server.search_messages(@bot.auth, @id, options)
435
+ SearchResults.new(JSON.parse(response), @bot)
436
+ end
437
+
399
438
  def inspect
400
439
  MijDiscord.make_inspect(self,
401
440
  :id, :name, :owner, :member_count, :features, :embed_enabled, :verification_level,
402
441
  :content_filter_level, :default_notifications, :afk_timeout, :afk_channel)
403
442
  end
404
443
  end
444
+
445
+ class SearchResults
446
+ ResultData = Struct.new(:result, :context) do
447
+ def inspect
448
+ MijDiscord.make_inspect(self, :result, :context)
449
+ end
450
+ end
451
+
452
+ attr_reader :total_count
453
+
454
+ attr_reader :messages
455
+
456
+ def initialize(data, bot)
457
+ @total_count = data['total_results']
458
+
459
+ @messages = data['messages'].map do |group|
460
+ context = group.map {|x| Message.new(x, bot) }
461
+ result = context.delete_at(context.length / 2)
462
+ ResultData.new(result, context)
463
+ end
464
+ end
465
+
466
+ def inspect
467
+ MijDiscord.make_inspect(self, :total_count, :messages)
468
+ end
469
+ end
405
470
  end
@@ -44,10 +44,10 @@ module MijDiscord::Data
44
44
  @application = data['application_id']
45
45
 
46
46
  if (start_time = data.dig('timestamps', 'start'))
47
- @start_time = Time.at(start_time).utc
47
+ @start_time = Time.at(start_time.to_i).utc
48
48
  end
49
49
  if (end_time = data.dig('timestamps', 'end'))
50
- @end_time = Time.at(end_time).utc
50
+ @end_time = Time.at(end_time.to_i).utc
51
51
  end
52
52
 
53
53
  if (assets = data['assets'])
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MijDiscord
4
- VERSION = '1.0.10'
4
+ VERSION = '1.0.11'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mij-discord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mijyuoon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-23 00:00:00.000000000 Z
11
+ date: 2018-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client