dsu3 0.2.0 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cc824dc0c20f13c57b1d52de343242849f438cfd4ad4689d52d67e364640c32
4
- data.tar.gz: 0fd1ff66a6390130bac16711c7dee4ece28ede6022e640a0ce8d9c7365e02604
3
+ metadata.gz: e9a0705c6d460fe073239d8637574b0fa3e8abfb618064aca5d79271f34a0d70
4
+ data.tar.gz: c622c03008ec604396abf1309d7fc87155bc9dca09650c7486305ec05214d6cb
5
5
  SHA512:
6
- metadata.gz: '09d69467460d12018015c06b3913739eab1c596f82b6d6def36601c4f3417e76bd1db62dacd1c9f042971c819a289f1592bd2fa0ad065fc787cbfe9a1ae3b1b5'
7
- data.tar.gz: 822d6e5232aa464cd77e5440313353d917ce0433c46a75442baffcebe9c121cbdf0a88a4d4b680e71a64c44968392dd82cb3aca2592f1ccc391dcd736c6e09c6
6
+ metadata.gz: db0f31beb357f9e01751305fa00c0e914c0b2e9a51664afdbf88a2940e8b23fef2488e84597eb44c1e155d4bb6f6641f51ba86067dc7ca3fa537f86da13088b5
7
+ data.tar.gz: 9a9c5288a110b35e13db5b5879c8d79ab9ba2b26f952b91b203a87ed628c1b100f0f69cfb8c27a267d1cbcd50cff43e770e3f80e5f50161492c051b2039d314d
data/lib/dsu3/bot.rb CHANGED
@@ -5,20 +5,21 @@ require 'json'
5
5
  require 'uri'
6
6
 
7
7
  module DSU3
8
+ # Class representing Discord bot
8
9
  class Bot
9
10
  API_BASE = 'https://discord.com/api/v9'
10
11
 
11
- # @param [Hash] headers
12
- def initialize(headers)
13
- @headers = headers
12
+ # @param [String] token Discord account token
13
+ def initialize(token)
14
+ @headers = Props::HEADERS.merge(authorization: token)
14
15
  end
15
16
 
16
- # Makes an API request, includes simple rate limit handling
17
+ # Makes an API request without any error handling
17
18
  # @param [Symbol, String] method
18
19
  # @param [String] endpoint Discord API endpoint
19
20
  # @param [Hash] headers Additional request headers
20
21
  # @param [String] payload
21
- def request(method, endpoint, headers = {}, payload = nil)
22
+ def raw_request(method, endpoint, headers = {}, payload = nil)
22
23
  args = {
23
24
  method: method,
24
25
  url: URI.join(API_BASE, endpoint).to_s,
@@ -27,15 +28,33 @@ module DSU3
27
28
 
28
29
  args[:payload] = payload if payload
29
30
 
30
- begin
31
- RestClient::Request.execute(args)
32
- rescue RestClient::TooManyRequests => e
33
- LOGGER.warn('rate limit exceeded')
34
- sleep(JSON.parse(e.response)['retry_after'] / 1000.0)
31
+ RestClient::Request.execute(args)
32
+ end
33
+
34
+ # Makes an API request, includes simple error handling
35
+ # @param (see #raw_request)
36
+ def request(...)
37
+ raw_request(...)
38
+ rescue RestClient::ExceptionWithResponse => e
39
+ data = JSON.parse(e.response)
40
+
41
+ if e.is_a?(RestClient::TooManyRequests)
42
+ retry_after = data['retry_after'] / 1000.0
43
+
44
+ LOGGER.warn("rate limit exceeded, waiting #{retry_after} seconds")
45
+ sleep(retry_after)
35
46
  retry
47
+ else
48
+ LOGGER.error("#{data['code']}: #{data['message']}")
36
49
  end
37
50
  end
38
51
 
52
+ # Types text into a particular channel
53
+ # @param [String, Integer] channel Channel ID
54
+ def type(channel)
55
+ request(:post, "channels/#{channel}/typing")
56
+ end
57
+
39
58
  # Sends a message to a specific channel
40
59
  # @param [String, Integer] channel Channel ID
41
60
  # @param [String] message
@@ -55,5 +74,20 @@ module DSU3
55
74
  '{}'
56
75
  )
57
76
  end
77
+
78
+ # Reacts to a message
79
+ # @note To use custom emoji, you must encode it in the format name:id with the emoji name and emoji id
80
+ # @param [String, Integer] channel Channel ID
81
+ # @param [String, Integer] message Message ID
82
+ # @param [String] emoji
83
+ def react(channel, message, emoji)
84
+ emoji = URI.encode_www_form_component(emoji)
85
+
86
+ request(
87
+ :put,
88
+ "channels/#{channel}/messages/#{message}/reactions/#{emoji}/@me",
89
+ { params: { location: 'Message' } }
90
+ )
91
+ end
58
92
  end
59
93
  end
data/lib/dsu3/core.rb CHANGED
@@ -4,19 +4,17 @@ require 'dsu3/props'
4
4
  require 'dsu3/bot'
5
5
 
6
6
  module DSU3
7
- # All DSU3 functionality, used to manage multiple bots
7
+ # Class used to manage multiple bots
8
8
  class Core
9
9
  # @param [Array] tokens List of bot tokens
10
- def initialize(tokens)
11
- headers = Props.headers
12
- @bots = tokens.map { |token| Bot.new(headers.merge(authorization: token)) }
10
+ def initialize(*tokens)
11
+ @bots = tokens.map(&Bot.method(:new))
13
12
  end
14
13
 
15
- # Infinitely writes to a particular channel
16
- # @param [String, Integer] channel Channel ID
14
+ # (see Bot#type)
17
15
  def typespam(channel)
18
16
  loop do
19
- @bots.each { |bot| bot.request(:post, "channels/#{channel}/typing") }
17
+ @bots.each { |bot| bot.type(channel) }
20
18
  sleep 9
21
19
  end
22
20
  end
@@ -39,5 +37,10 @@ module DSU3
39
37
  sleep(0.5)
40
38
  end
41
39
  end
40
+
41
+ # (see Bot#react)
42
+ def react(channel, message, emoji)
43
+ @bots.each { |bot| bot.react(channel, message, emoji) }
44
+ end
42
45
  end
43
46
  end
data/lib/dsu3/props.rb CHANGED
@@ -6,8 +6,6 @@ require 'base64'
6
6
 
7
7
  module DSU3
8
8
  module Props
9
- module_function
10
-
11
9
  # user-agent header
12
10
  USER_AGENT =
13
11
  'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '\
@@ -28,7 +26,9 @@ module DSU3
28
26
  client_event_source: nil
29
27
  }.freeze
30
28
 
31
- # Main headers
29
+ resp = RestClient.get('https://discord.com/api/v9/experiments')
30
+
31
+ # Headers necessary for normal interaction with the Discord API
32
32
  HEADERS = {
33
33
  accept: '*/*',
34
34
  accept_encoding: 'gzip, deflate, br',
@@ -40,17 +40,10 @@ module DSU3
40
40
  x_debug_options: 'bugReporterEnabled',
41
41
  x_discord_locale: 'en-US',
42
42
  x_super_properties: Base64.strict_encode64(SUPER_PROPERTIES.to_json),
43
- content_type: 'application/json'
43
+ content_type: 'application/json',
44
+ origin: 'https://discord.com',
45
+ cookie: HTTP::Cookie.cookie_value(resp.cookie_jar.cookies),
46
+ x_fingerprint: JSON.parse(resp.body)['fingerprint']
44
47
  }.freeze
45
-
46
- # Gets the headers necessary for normal interaction with the Discord API
47
- def headers
48
- resp = RestClient.get('https://discord.com/api/v9/experiments')
49
-
50
- {
51
- cookie: HTTP::Cookie.cookie_value(resp.cookie_jar.cookies),
52
- x_fingerprint: JSON.parse(resp.body)['fingerprint']
53
- }.merge(HEADERS)
54
- end
55
48
  end
56
49
  end
data/lib/dsu3.rb CHANGED
@@ -5,7 +5,7 @@ require 'dsu3/core'
5
5
 
6
6
  # Main DSU3 namespace
7
7
  module DSU3
8
- VERSION = '0.2.0'
8
+ VERSION = '0.4'
9
9
 
10
10
  LOGGER = Logger.new($stdout)
11
11
  LOGGER.level = Logger::WARN
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsu3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Sheremetjev IV
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-17 00:00:00.000000000 Z
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -42,6 +42,7 @@ licenses:
42
42
  metadata:
43
43
  source_code_uri: https://github.com/hackers-pr/dsu3
44
44
  bug_tracker_uri: https://github.com/hackers-pr/dsu3/issues
45
+ wiki_uri: https://www.rubydoc.info/gems/dsu3
45
46
  post_install_message:
46
47
  rdoc_options: []
47
48
  require_paths:
@@ -50,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
51
  requirements:
51
52
  - - ">="
52
53
  - !ruby/object:Gem::Version
53
- version: 2.0.0
54
+ version: 2.7.0
54
55
  required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  requirements:
56
57
  - - ">="