revoltrb 0.0.1 → 0.0.3

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: be934ade9a4122fe6ea640bf5aafe2930eb1a11ee2a8f4340d282b3fa99f163c
4
- data.tar.gz: 6e384401d88c604fa0bfa05b1a840697c0382a288683ac27db23d98e425b4a25
3
+ metadata.gz: 4a47d743074af0c8a29f14e720df6488e8db97c325c721e52fa5c110ffc2b8ba
4
+ data.tar.gz: cc98be2028dd30a7089fd1e0e5ee83fb62a518cac32812184cd6c8e0b6974fe0
5
5
  SHA512:
6
- metadata.gz: e36bad7e4adec3333f2341b6d1e85aadc1cbab691552d69a1f9e777a7df5722b2fc048e1e98be0e361a4a1447d2597e65f3cc8888d7afb9fb589e9dfa4f79b27
7
- data.tar.gz: db5fa947e54394afb19495145a3b3beb8ef50bf6bc17c584fc8369659a281603c48f522fe7a7c0ea4dcf56c7a1f1a35185d5a593ffaea5586951862a19a40535
6
+ metadata.gz: 014fbf6c33d831f9b7804299454e57017aa684fe84184a8ddbd21711cac2771ca0ef537592bb113d27e2870e9260c2c97eac00dd09d9fba9f579110dcfa69207
7
+ data.tar.gz: 718a3ad4ca444aa2ca55fd451190ae937e6fbff8f19f22aed7f935869085527279e0a9bb04736f437a9bf9752f7ac1005e21f61148906ed116cb9e1d9e87df80
data/README.md CHANGED
@@ -1,18 +1,17 @@
1
1
  # revoltrb
2
2
 
3
- Revoltrb is a Ruby package (a.k.a. Gem) that allows you to make Revolt.chat bots using the Ruby programming language.
4
-
5
- This package (a.k.a. Gem) is not officially endorsed by Revolt.chat amd this is not an official Revolt.chat product.
3
+ Revoltrb is a Ruby package (a.k.a. Gem) that allows you to make Revolt.chat bots using the Ruby programming language. This package (a.k.a. Gem) is not officially endorsed by Revolt.chat amd this is not an official Revolt.chat product.
6
4
 
7
5
  You need Ruby 3.0 or newer in order to use this package (Ruby 3.2 or newer is recommended)
8
6
 
7
+ > [!NOTE]
8
+ > This package (a.k.a. Gem) is in a early alpha state so expect things to be buggy and/or broken.
9
+
9
10
  ## ToDo
10
11
 
11
12
  This list contains a list of things that I know is broken and gotta fix. Contributing will be super helpful.
12
13
 
13
14
  - Fix reactions support
14
- - Fix obtaining server information
15
- - Fix not being able to have an embed color
16
15
 
17
16
  ## Setup
18
17
 
@@ -20,7 +19,7 @@ You can install Revoltrb through several methods:
20
19
 
21
20
  #### Method 1: Install from rubygems.org (Bundler)
22
21
 
23
- Add the following to your Gemfile file and run the "bundle install" command:
22
+ Add the following to your Gemfile file and run the "[bundle install](https://rubygems.org/gems/revoltrb)" command:
24
23
 
25
24
  ```ruby
26
25
  gem 'discordrb'
@@ -105,6 +104,21 @@ ensure
105
104
  end
106
105
  ```
107
106
 
107
+ Webhook example:
108
+
109
+ ```ruby
110
+ require 'revoltrb'
111
+ # Webhook url should look like this: https://beta.revolt.chat/api/webhooks/<WEBHOOK_ID>/<WEBHOOK_TOKEN>
112
+ # Webhook urls can be obtained and created through beta.revolt.chat
113
+ WEBHOOK_ID = ''
114
+ WEBHOOK_TOKEN = ''
115
+ webhook = RevoltWebhooks::Webhook.new(WEBHOOK_ID, WEBHOOK_TOKEN)
116
+
117
+ webhook.send_message(
118
+ content: "Revoltrb webhook message content",
119
+ )
120
+ ```
121
+
108
122
  ## Support and Help
109
123
 
110
124
  If you need help with this ruby package (a.k.a. Gem), feel free to join the [Roxanne Studios Revolt Server](https://rvlt.gg/r4Ee2R1Z) and use the REVOLTRB category to talk about this package.
data/lib/revoltrb/bot.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # lib/revoltrb/bot.rb
2
-
3
2
  require 'json'
4
3
  require 'net/http'
5
4
  require 'websocket-client-simple' # This is required for WebSocket communication
@@ -7,10 +6,26 @@ require 'thread'
7
6
  require 'time'
8
7
  require_relative 'debuglogger'
9
8
  require_relative 'request_queue'
9
+ require_relative 'webhooks'
10
10
 
11
11
  module Revoltrb
12
12
  class RevoltBot
13
- attr_reader :token, :user_id, :bot_name, :servers, :prefix, :bot_owner_id, :bot_discriminator, :bot_discoverable, :bot_creation_date
13
+ EMOJI_MAP = {
14
+ ':grinning:' => '😀',
15
+ ':heart:' => '❤️',
16
+ ':joy:' => '😂',
17
+ ':unamused:' => '😒',
18
+ ':sunglasses:' => '😎',
19
+ ':thinking:' => '🤔',
20
+ ':clap:' => '👏',
21
+ ':thumbsup:' => '👍',
22
+ ':thumbsdown:' => '👎',
23
+ ':point_up:' => '☝️',
24
+ ':+1:' => '👍',
25
+ ':-1:' => '👎'
26
+ }.freeze
27
+
28
+ attr_reader :token, :user_id, :bot_name, :servers, :prefix, :bot_owner_id, :bot_discriminator, :bot_discoverable, :bot_creation_date, :webhooks
14
29
  attr_accessor :websocket_url, :api_url, :cdn_url
15
30
  # Initializes the bot with the provided token, API endpoints, and configuration.
16
31
  def initialize(token, api_url: 'https://api.revolt.chat', websocket_url: 'wss://app.revolt.chat/events', cdn_url: 'https://cdn.revoltusercontent.com', prefix: nil, debuglogs: false, selfbot: false)
@@ -33,6 +48,7 @@ module Revoltrb
33
48
  @ready_event_received = false
34
49
  @logger = Revoltrb::DebugLogger.new(debuglogs)
35
50
  @request_queue = Revoltrb::RequestQueue.new(500)
51
+ @webhooks = Revoltrb::Webhooks.new(api_url, @logger, token, selfbot)
36
52
 
37
53
  @prefix = "!"
38
54
  @prefix = prefix if prefix
@@ -46,8 +62,11 @@ module Revoltrb
46
62
  'bot_name' => @bot_name,
47
63
  'bot_discriminator' => @bot_discriminator,
48
64
  'bot_ownerid' => @bot_owner_id,
49
- 'bot_discoverable' => @bot_discoverable,
50
65
  'bot_creationdate' => @bot_creation_date,
66
+ 'bot_flags' => @bot_flags,
67
+ 'bot_discoverable' => @bot_discoverable,
68
+ 'bot_public' => @bot_public,
69
+ 'bot_analytics' => @bot_analytics,
51
70
  'bot_prefix' => @prefix,
52
71
  'bot_token' => @token
53
72
  }
@@ -72,7 +91,10 @@ module Revoltrb
72
91
  @bot_discriminator = bot_user_data&.[]('discriminator')
73
92
  bot_specific_info = bot_user_data&.[]('bot')
74
93
  @bot_owner_id = bot_specific_info&.[]('owner')
94
+ @bot_flags = bot_specific_info&.[]('flags')
75
95
  @bot_discoverable = nil
96
+ @bot_public = nil
97
+ @bot_analytics = nil
76
98
  @bot_creation_date = Time.at(bot_user_data&.[]('created_at').to_i / 1000) rescue nil
77
99
  if @user_id.nil? || @bot_name.nil?
78
100
  @logger.debug "Error: Essential properties (_id, username) missing or nil in API response from /users/@me."
@@ -283,7 +305,6 @@ module Revoltrb
283
305
  @logger.debug "User #{user_id} is not the bot owner. Requires permissions: #{required_permissions.inspect}. (Full permission check for non-owner, server-specific permissions not implemented)."
284
306
  return false
285
307
  end
286
-
287
308
  @logger.debug "Permission Check: All checks passed. Allowing command."
288
309
  true
289
310
  end
@@ -346,7 +367,7 @@ module Revoltrb
346
367
 
347
368
  if embeds && !embeds.empty?
348
369
  filtered_embeds = embeds.map do |embed|
349
- supported_keys = ['title', 'description', 'color', 'url', 'image']
370
+ supported_keys = ['title', 'description', 'colour', 'url', 'icon_url', 'media']
350
371
  embed.select { |k, v| supported_keys.include?(k.to_s) }
351
372
  end
352
373
  payload[:embeds] = filtered_embeds
@@ -381,40 +402,70 @@ module Revoltrb
381
402
  @logger.debug e.backtrace.join("\n")
382
403
  end
383
404
 
405
+ def find_unicode_emoji(shortcode)
406
+ EMOJI_MAP[shortcode]
407
+ end
384
408
 
385
409
  def add_reaction(channel_id, message_id, emoji_id)
410
+ if emoji_id.start_with?(':') && emoji_id.end_with?(':')
411
+ @logger.debug "AN ERROR HAS OCCURED: Cannot add reaction with shortcode '#{emoji_id}'. Please use the actual Unicode emoji or a custom emoji ID."
412
+ return
413
+ end
414
+
386
415
  @request_queue.enqueue do
387
416
  @logger.debug "Attempting to add reaction '#{emoji_id}' to message '#{message_id}' in channel '#{channel_id}' via queue."
388
- uri = URI("#{@api_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}")
389
- req = Net::HTTP::Put.new(uri) # Reactions use PUT
390
- req['x-bot-token'] = @token
391
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
392
- http.request(req)
417
+ encoded_emoji_id = CGI.escape(emoji_id)
418
+
419
+ uri = URI("#{@api_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{encoded_emoji_id}")
420
+ req = Net::HTTP::Put.new(uri)
421
+ _add_auth_header(req)
422
+
423
+ res = nil
424
+ begin
425
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https', read_timeout: 10, open_timeout: 10) do |http|
426
+ http.request(req)
427
+ end
428
+ rescue Net::ReadTimeout => e
429
+ @logger.debug "AN ERROR HAS OCCURED: The network request timed out while waiting for a response after 10 seconds. Error: #{e.message}"
430
+ return
431
+ rescue Net::OpenTimeout => e
432
+ @logger.debug "AN ERROR HAS OCCURED: The network request timed out while trying to open a connection after 10 seconds. Error: #{e.message}"
433
+ return
434
+ rescue => e
435
+ @logger.debug "AN ERROR HAS OCCURED: An unexpected error occurred during the network request. Error: #{e.message}"
436
+ return
437
+ ensure
438
+ @logger.debug "Network request to add reaction finished."
393
439
  end
440
+
441
+ @logger.debug "Received response with code: #{res.code}"
394
442
  if res.is_a?(Net::HTTPSuccess) || res.code == '204'
395
- @logger.debug "Reaction has been added successfully!"
443
+ @logger.debug "Reaction added successfully!"
396
444
  else
397
445
  @logger.debug "AN ERROR HAS OCCURED: Failed to add reaction: #{res.message} (Code: #{res.code})"
398
446
  @logger.debug "Response Body: #{res.body}"
447
+ if res.code == '403'
448
+ @logger.debug "HINT: The bot may be missing the 'AddReactions' permission in this channel or server."
449
+ end
399
450
  end
400
451
  end
401
- rescue => e
402
- @logger.debug "AN ERROR HAS OCCURED: Error enqueuing add reaction: #{e.message}"
403
- @logger.debug e.backtrace.join("\n")
404
452
  end
405
453
 
406
454
  def remove_reaction(channel_id, message_id, emoji_id, user_id: nil)
407
- target_user_id = user_id || @user_id # Default to bot's own ID
455
+ target_user_id = user_id || @user_id
408
456
  @request_queue.enqueue do
409
457
  @logger.debug "Attempting to remove reaction '#{emoji_id}' from message '#{message_id}' by user '#{target_user_id}' in channel '#{channel_id}' via queue."
410
- uri = URI("#{@api_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}?user_id=#{target_user_id}")
458
+
459
+ encoded_emoji_id = CGI.escape(emoji_id)
460
+
461
+ uri = URI("#{@api_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{encoded_emoji_id}?user_id=#{target_user_id}")
411
462
  req = Net::HTTP::Delete.new(uri)
412
- req['x-bot-token'] = @token
463
+ _add_auth_header(req)
413
464
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
414
465
  http.request(req)
415
466
  end
416
467
  if res.is_a?(Net::HTTPSuccess) || res.code == '204'
417
- @logger.debug "Reaction has been removed successfully!"
468
+ @logger.debug "Reaction removed successfully!"
418
469
  else
419
470
  @logger.debug "AN ERROR HAS OCCURED: Failed to remove reaction: #{res.message} (Code: #{res.code})"
420
471
  @logger.debug "Response Body: #{res.body}"
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Revoltrb
4
4
  # The current version of revoltrb
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.3"
6
6
  end
@@ -0,0 +1,108 @@
1
+ # lib/revoltrb/webhooks.rb
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Revoltrb
7
+ class Webhooks
8
+ attr_reader :api_url, :logger, :token, :selfbot
9
+ def initialize(api_url, logger, token, selfbot)
10
+ @api_url = api_url
11
+ @logger = logger
12
+ @token = token
13
+ @selfbot = selfbot
14
+ end
15
+
16
+ def create_webhook(channel_id, name, avatar_url: nil)
17
+ @logger.debug "Attempting to create a webhook for channel '#{channel_id}' with name '#{name}'."
18
+
19
+ uri = URI("#{@api_url}/channels/#{channel_id}/webhooks")
20
+ req = Net::HTTP::Post.new(uri)
21
+ if @selfbot
22
+ req['Authorization'] = @token
23
+ else
24
+ req['x-bot-token'] = @token
25
+ end
26
+ req['Content-Type'] = 'application/json'
27
+
28
+ payload = {
29
+ name: name
30
+ }
31
+ payload[:avatar] = avatar_url unless avatar_url.nil?
32
+ req.body = payload.to_json
33
+
34
+ res = nil
35
+ begin
36
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https', read_timeout: 10, open_timeout: 10) do |http|
37
+ http.request(req)
38
+ end
39
+ rescue Net::ReadTimeout => e
40
+ @logger.debug "AN ERROR HAS OCCURED: Network request timed out while reading: #{e.message}"
41
+ return nil
42
+ rescue Net::OpenTimeout => e
43
+ @logger.debug "AN ERROR HAS OCCURED: Network request timed out while connecting: #{e.message}"
44
+ return nil
45
+ rescue => e
46
+ @logger.debug "AN ERROR HAS OCCURED: An unexpected error occurred while creating the webhook: #{e.message}"
47
+ return nil
48
+ end
49
+
50
+ if res.is_a?(Net::HTTPSuccess)
51
+ webhook_data = JSON.parse(res.body)
52
+ @logger.debug "Webhook created successfully! Details: #{res.body}"
53
+ @logger.debug "--- Webhook Created ---"
54
+ @logger.debug "Name: #{webhook_data['name']} | ID: #{webhook_data['id']}"
55
+ @logger.debug "Channel ID: #{webhook_data['channel_id']} | Permissions: #{webhook_data['permissions']}"
56
+ @logger.debug "Token: #{webhook_data['token'].inspect}"
57
+ @logger.debug "--- End Webhook Details ---"
58
+ return webhook_data
59
+ else
60
+ @logger.debug "AN ERROR HAS OCCURED: Failed to create webhook. (Code: #{res.code}) Response: #{res.message}"
61
+ @logger.debug "Response Body: #{res.body}"
62
+ return nil
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ module RevoltWebhooks
69
+ class Webhook
70
+ DEFAULT_API_URL = 'https://beta.revolt.chat/api'.freeze
71
+ def initialize(webhook_id, webhook_token, api_url: DEFAULT_API_URL)
72
+ @webhook_id = webhook_id
73
+ @webhook_token = webhook_token
74
+ @api_url = api_url
75
+ @uri = URI("#{@api_url}/webhooks/#{@webhook_id}/#{@webhook_token}")
76
+ end
77
+
78
+ def send_message(content:, embeds: nil)
79
+ payload = {
80
+ content: content
81
+ }
82
+
83
+ if embeds
84
+ payload[:embeds] = Array(embeds)
85
+ end
86
+
87
+ http = Net::HTTP.new(@uri.host, @uri.port)
88
+ http.use_ssl = true
89
+ request = Net::HTTP::Post.new(@uri.path, 'Content-Type' => 'application/json')
90
+ request.body = payload.to_json
91
+
92
+ begin
93
+ response = http.request(request)
94
+
95
+ if response.code.to_i == 204 || response.code.to_i == 200
96
+ return true
97
+ else
98
+ puts "AN ERROR HAS OCCURED: Status code: #{response.code}"
99
+ puts "Response body: #{response.body}"
100
+ return false
101
+ end
102
+ rescue StandardError => e
103
+ puts "AN ERROR HAS OCCURED: #{e.message}"
104
+ return false
105
+ end
106
+ end
107
+ end
108
+ end
data/lib/revoltrb.rb CHANGED
@@ -1,8 +1,14 @@
1
+ # lib/revoltrb.rb
1
2
  require_relative 'revoltrb/bot'
2
3
  require_relative 'revoltrb/version'
3
4
  require_relative 'revoltrb/debuglogger'
4
5
  require_relative 'revoltrb/request_queue'
6
+ require_relative 'revoltrb/webhooks'
5
7
 
6
8
  module Revoltrb
7
9
  # This module can serve as a namespace for the gem's classes. For now, it's a direct require.
10
+ end
11
+
12
+ module RevoltWebhooks
13
+ # This module can serve as a namespace for the gem's classes. For now, it's a direct require.
8
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revoltrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roxanne Studios
@@ -13,30 +13,30 @@ dependencies:
13
13
  name: json
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - ">="
16
+ - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '0'
18
+ version: 2.13.2
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
- - - ">="
23
+ - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '0'
25
+ version: 2.13.2
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: net-http
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - ">="
30
+ - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: 0.6.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: 0.6.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: thread
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -79,6 +79,7 @@ files:
79
79
  - lib/revoltrb/debuglogger.rb
80
80
  - lib/revoltrb/request_queue.rb
81
81
  - lib/revoltrb/version.rb
82
+ - lib/revoltrb/webhooks.rb
82
83
  homepage: https://gitlab.com/roxannewolf/revoltrb
83
84
  licenses:
84
85
  - MIT