disrb 0.1.0 → 0.1.1.2

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.
data/lib/disrb/logger.rb CHANGED
@@ -7,16 +7,24 @@ class Logger2
7
7
  @verbosity_level = verbosity_level
8
8
  end
9
9
 
10
- def error(message)
10
+ def fatal_error(message)
11
11
  return unless @verbosity_level >= 1
12
12
 
13
- puts("\033[1;38;2;255;255;255;48;2;192;57;43m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
13
+ puts("\033[1;38;2;255;255;255;48;2;192;57;43m | FATAL ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
14
14
  " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;192;57;43m \033[0m " \
15
15
  "\e[38;2;192;57;43m#{message}\e[0m")
16
16
  end
17
17
 
18
+ def error(message)
19
+ return unless @verbosity_level >= 2
20
+
21
+ puts("\033[1;38;2;255;255;255;48;2;243;156;18m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
22
+ " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
23
+ "\e[38;2;243;156;18m#{message}\e[0m")
24
+ end
25
+
18
26
  def debug(message)
19
- return unless @verbosity_level == 4
27
+ return unless @verbosity_level == 5
20
28
 
21
29
  puts("\033[1;38;2;255;255;255;48;2;155;89;182m | DEBUG \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
22
30
  " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;155;89;182m \033[0m " \
@@ -24,27 +32,33 @@ class Logger2
24
32
  end
25
33
 
26
34
  def warn(message)
27
- return unless @verbosity_level >= 2
35
+ return unless @verbosity_level >= 3
28
36
 
29
- puts("\033[1;38;2;255;255;255;48;2;243;156;18m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
30
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
31
- "\e[38;2;243;156;18m#{message}\e[0m")
37
+ puts("\033[1;38;2;255;255;255;48;2;241;196;15m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
38
+ " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;241;196;15m \033[0m " \
39
+ "\e[38;2;241;196;15m#{message}\e[0m")
32
40
  end
33
41
 
34
42
  def info(message)
35
- return unless @verbosity_level >= 3
43
+ return unless @verbosity_level >= 4
36
44
 
37
45
  puts("\033[1;38;2;255;255;255;48;2;76;175;80m | INFORMATION \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
38
46
  " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;76;175;80m \033[0m " \
39
47
  "\e[38;2;76;175;80m#{message}\e[0m")
40
48
  end
41
49
 
42
- def self.s_error(message)
43
- puts("\033[1;38;2;255;255;255;48;2;192;57;43m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
50
+ def self.s_fatal_error(message)
51
+ puts("\033[1;38;2;255;255;255;48;2;192;57;43m | FATAL ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
44
52
  " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;192;57;43m \033[0m " \
45
53
  "\e[38;2;192;57;43m#{message}\e[0m")
46
54
  end
47
55
 
56
+ def self.s_error(message)
57
+ puts("\033[1;38;2;255;255;255;48;2;243;156;18m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
58
+ " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
59
+ "\e[38;2;243;156;18m#{message}\e[0m")
60
+ end
61
+
48
62
  def self.s_debug(message)
49
63
  puts("\033[1;38;2;255;255;255;48;2;155;89;182m | DEBUG \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
50
64
  " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;155;89;182m \033[0m " \
@@ -52,9 +66,9 @@ class Logger2
52
66
  end
53
67
 
54
68
  def self.s_warn(message)
55
- puts("\033[1;38;2;255;255;255;48;2;243;156;18m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
56
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
57
- "\e[38;2;243;156;18m#{message}\e[0m")
69
+ puts("\033[1;38;2;255;255;255;48;2;241;196;15m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
70
+ " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;241;196;15m \033[0m " \
71
+ "\e[38;2;241;196;15m#{message}\e[0m")
58
72
  end
59
73
 
60
74
  def self.s_info(message)
data/lib/disrb/message.rb CHANGED
@@ -11,24 +11,24 @@ class DiscordApi
11
11
  return
12
12
  end
13
13
  output = {}
14
- output[:content] = content if content
15
- output[:nonce] = nonce if nonce
16
- output[:tts] = tts if tts
17
- output[:embeds] = embeds if embeds
18
- output[:allowed_mentions] = allowed_mentions if allowed_mentions
19
- output[:message_reference] = message_reference if message_reference
20
- output[:components] = components if components
21
- output[:sticker_ids] = sticker_ids if sticker_ids
22
- output[:files] = files if files
23
- output[:attachments] = attachments if attachments
24
- output[:flags] = flags if flags
25
- output[:enforce_nonce] = enforce_nonce if enforce_nonce
26
- output[:poll] = poll if poll
27
- url = URI("#{@base_url}/channels/#{channel_id}/messages")
14
+ output[:content] = content unless content.nil?
15
+ output[:nonce] = nonce unless nonce.nil?
16
+ output[:tts] = tts unless tts.nil?
17
+ output[:embeds] = embeds unless embeds.nil?
18
+ output[:allowed_mentions] = allowed_mentions unless allowed_mentions.nil?
19
+ output[:message_reference] = message_reference unless message_reference.nil?
20
+ output[:components] = components unless components.nil?
21
+ output[:sticker_ids] = sticker_ids unless sticker_ids.nil?
22
+ output[:files] = files unless files.nil?
23
+ output[:attachments] = attachments unless attachments.nil?
24
+ output[:flags] = flags unless flags.nil?
25
+ output[:enforce_nonce] = enforce_nonce unless enforce_nonce.nil?
26
+ output[:poll] = poll unless poll.nil?
27
+ url = "#{@base_url}/channels/#{channel_id}/messages"
28
28
  data = JSON.generate(output)
29
29
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
30
- response = Net::HTTP.post(url, data, headers)
31
- return response unless response.code != '200'
30
+ response = DiscordApi.post(url, data, headers)
31
+ return response unless response.status != 200
32
32
 
33
33
  @logger.error("Failed to create message in channel #{channel_id}. Response: #{response.body}")
34
34
  response
data/lib/disrb/user.rb CHANGED
@@ -6,41 +6,43 @@
6
6
  # The class that contains everything that interacts with the Discord API.
7
7
  class DiscordApi
8
8
  def get_current_user
9
- url = URI("#{@base_url}/users/@me")
9
+ url = "#{@base_url}/users/@me"
10
10
  headers = { 'Authorization': @authorization_header }
11
- response = Net::HTTP.get(url, headers)
12
- return response unless response.code != '200'
11
+ response = DiscordApi.get(url, headers)
12
+ return response unless response.status != 200
13
13
 
14
14
  @logger.error("Failed to get current user. Response: #{response.body}")
15
15
  response
16
16
  end
17
17
 
18
- # TODO: I should consistenly check that the variables are set correctly
19
18
  def get_user(user_id)
20
- url = URI("#{@base_url}/users/#{user_id}")
19
+ url = "#{@base_url}/users/#{user_id}"
21
20
  headers = { 'Authorization': @authorization_header }
22
- response = Net::HTTP.get(url, headers)
23
- return response unless response.code != '200'
21
+ response = DiscordApi.get(url, headers)
22
+ return response unless response.status != 200
24
23
 
25
24
  @logger.error("Failed to get user with ID #{user_id}. Response: #{response.body}")
26
25
  response
27
26
  end
28
27
 
29
28
  def modify_current_user(username: nil, avatar: nil, banner: nil)
29
+ if args.all?(&:nil?)
30
+ @logger.warn('No modifications provided for current user. Skipping function.')
31
+ return nil
32
+ end
30
33
  output = {}
31
- output[:username] = username if username
32
- output[:avatar] = avatar if avatar
33
- output[:banner] = banner if banner
34
- # TODO: where all parameters are optional, i should consistently check if the parameters are empty, then skip if yes
34
+ output[:username] = username unless username.nil?
35
+ output[:avatar] = avatar unless avatar.nil?
36
+ output[:banner] = banner unless banner.nil?
35
37
  if output.empty?
36
38
  @logger.warn('No current user modifications provided. Skipping function.')
37
39
  return
38
40
  end
39
- url = URI("#{@base_url}/users/@me")
41
+ url = "#{@base_url}/users/@me"
40
42
  data = JSON.generate(output)
41
43
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
42
- response = Net::HTTP.patch(url, data, headers)
43
- return response unless response.code != '200'
44
+ response = DiscordApi.patch(url, data, headers)
45
+ return response unless response.status != 200
44
46
 
45
47
  @logger.error("Failed to modify current user. Response: #{response.body}")
46
48
  response
@@ -48,41 +50,41 @@ class DiscordApi
48
50
 
49
51
  def get_current_user_guilds(before: nil, after: nil, limit: nil, with_counts: nil)
50
52
  query_string_hash = {}
51
- query_string_hash[:before] = before
52
- query_string_hash[:after] = after
53
- query_string_hash[:limit] = limit
54
- query_string_hash[:with_counts] = with_counts
53
+ query_string_hash[:before] = before unless before.nil?
54
+ query_string_hash[:after] = after unless after.nil?
55
+ query_string_hash[:limit] = limit unless limit.nil?
56
+ query_string_hash[:with_counts] = with_counts unless with_counts.nil?
55
57
  query_string = DiscordApi.handle_query_strings(query_string_hash)
56
- url = URI("#{@base_url}/users/@me/guilds#{query_string}")
58
+ url = "#{@base_url}/users/@me/guilds#{query_string}"
57
59
  headers = { 'Authorization': @authorization_header }
58
- response = Net::HTTP.get(url, headers)
60
+ response = DiscordApi.get(url, headers)
59
61
  if @authorization_token_type == 'Bot' && response.body.count == 200
60
62
  @logger.warn('A bot can be in more than 200 guilds, however 200 guilds were returned.' \
61
63
  'Discord API doesn\'t allow you to fetch more than 200 guilds. Some guilds might not be listed.')
62
64
  return response
63
65
  end
64
- return response unless response.code != '200'
66
+ return response unless response.status != 200
65
67
 
66
68
  @logger.error("Failed to get current user's guilds. Response: #{response.body}")
67
69
  response
68
70
  end
69
71
 
70
72
  def leave_guild(guild_id)
71
- url = URI("#{@base_url}/users/@me/guilds/#{guild_id}")
73
+ url = "#{@base_url}/users/@me/guilds/#{guild_id}"
72
74
  headers = { 'Authorization': @authorization_header }
73
- response = Net::HTTP.delete(url, headers)
74
- return response unless response.code != '204'
75
+ response = DiscordApi.delete(url, headers)
76
+ return response unless response.status != 204
75
77
 
76
78
  @logger.error("Failed to leave guild with ID #{guild_id}. Response: #{response.body}")
77
79
  response
78
80
  end
79
81
 
80
82
  def create_dm(recipient_id)
81
- url = URI("#{@base_url}/users/@me/channels")
83
+ url = "#{@base_url}/users/@me/channels"
82
84
  data = JSON.generate({ recipient_id: recipient_id })
83
85
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
84
- response = Net::HTTP.post(url, data, headers)
85
- return response unless response.code != '200'
86
+ response = DiscordApi.post(url, data, headers)
87
+ return response unless response.status != 200
86
88
 
87
89
  @logger.error("Failed to create DM with recipient ID #{recipient_id}. Response: #{response.body}")
88
90
  response
@@ -92,31 +94,31 @@ class DiscordApi
92
94
  output = {}
93
95
  output[:access_tokens] = access_tokens
94
96
  output[:nicks] = nicks
95
- url = URI("#{@base_url}/users/@me/channels")
97
+ url = "#{@base_url}/users/@me/channels"
96
98
  data = JSON.generate(output)
97
99
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
98
- response = Net::HTTP.post(url, data, headers)
99
- return response unless response.code != '200'
100
+ response = DiscordApi.post(url, data, headers)
101
+ return response unless response.status != 200
100
102
 
101
103
  @logger.error("Failed to create group DM. Response: #{response.body}")
102
104
  response
103
105
  end
104
106
 
105
107
  def get_current_user_connections
106
- url = URI("#{@base_url}/users/@me/connections")
108
+ url = "#{@base_url}/users/@me/connections"
107
109
  headers = { 'Authorization': @authorization_header }
108
- response = Net::HTTP.get(url, headers)
109
- return response unless response.code != '200'
110
+ response = DiscordApi.get(url, headers)
111
+ return response unless response.status != 200
110
112
 
111
113
  @logger.error("Failed to get current user's connections. Response: #{response.body}")
112
114
  response
113
115
  end
114
116
 
115
117
  def get_current_user_application_role_connection(application_id)
116
- url = URI("#{@base_url}/users/@me/applications/#{application_id}/role-connection")
118
+ url = "#{@base_url}/users/@me/applications/#{application_id}/role-connection"
117
119
  headers = { 'Authorization': @authorization_header }
118
- response = Net::HTTP.get(url, headers)
119
- return response unless response.code != '200'
120
+ response = DiscordApi.get(url, headers)
121
+ return response unless response.status != 200
120
122
 
121
123
  @logger.error("Failed to get current user's application role connection for application ID #{application_id}. " \
122
124
  "Response: #{response.body}")
@@ -133,11 +135,11 @@ class DiscordApi
133
135
  @logger.warn('No current user application role connection modifications provided. Skipping function.')
134
136
  return
135
137
  end
136
- url = URI("#{@base_url}/users/@me/applications/#{application_id}/role-connection")
138
+ url = "#{@base_url}/users/@me/applications/#{application_id}/role-connection"
137
139
  data = JSON.generate(output)
138
140
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
139
- response = Net::HTTP.put(url, data, headers)
140
- return response unless response.code != '200'
141
+ response = DiscordApi.put(url, data, headers)
142
+ return response unless response.status != 200
141
143
 
142
144
  @logger.error("Failed to update current user's application role connection for application ID #{application_id}. " \
143
145
  "Response: #{response.body}")