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