revoltrb 0.0.2 → 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.
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Revoltrb
4
4
  # The current version of revoltrb
5
- VERSION = "0.0.2"
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roxanne Studios
@@ -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