disrb 0.1.0

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