mij-discord 1.0.9 → 1.0.10

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.
@@ -1,297 +1,295 @@
1
- # frozen_string_literal: true
2
-
3
- module MijDiscord::Data
4
- class Game
5
- PLAYING_TYPE = [
6
- :playing,
7
- :streaming,
8
- :listening,
9
- :watching,
10
- ].freeze
11
-
12
- attr_reader :name
13
-
14
- attr_reader :url
15
-
16
- attr_reader :details
17
-
18
- attr_reader :state
19
-
20
- attr_reader :start_time
21
-
22
- attr_reader :end_time
23
-
24
- attr_reader :application
25
-
26
- attr_reader :large_image
27
-
28
- attr_reader :large_text
29
-
30
- attr_reader :small_image
31
-
32
- attr_reader :small_text
33
-
34
- def type
35
- PLAYING_TYPE[@type]
36
- end
37
-
38
- def initialize(data)
39
- @type = data['type']
40
- @name = data['name']
41
- @url = data['url']
42
- @details = data['details']
43
- @state = data['state']
44
- @application = data['application_id']
45
-
46
- if (start_time = data.dig('timestamps', 'start'))
47
- @start_time = Time.at(start_time).utc
48
- end
49
- if (end_time = data.dig('timestamps', 'end'))
50
- @end_time = Time.at(end_time).utc
51
- end
52
-
53
- if (assets = data['assets'])
54
- @large_image = assets['large_image']
55
- @large_text = assets['large_text']
56
- @small_image = assets['small_image']
57
- @small_text = assets['small_text']
58
- end
59
- end
60
-
61
- def to_hash
62
- self.class.construct({
63
- start_time: @start_time,
64
- end_time: @end_time,
65
-
66
- large_image: @large_image,
67
- large_text: @large_text,
68
- small_image: @small_image,
69
- small_text: @small_text,
70
-
71
- type: @type,
72
- name: @name,
73
- url: @url,
74
- details: @details,
75
- state: @state,
76
- application: @application,
77
- })
78
- end
79
-
80
- def inspect
81
- MijDiscord.make_inspect(self,
82
- :type, :name, :url, :details, :state, :start_time, :end_time,
83
- :application, :large_image, :large_text, :small_image, :small_text)
84
- end
85
-
86
- def self.construct(data)
87
- data = {name: data} if data.is_a?(String)
88
-
89
- times = {
90
- start: data.try_keys(:start_time, 'start_time')&.to_i,
91
- end: data.try_keys(:end_time, 'end_time')&.to_i,
92
- }.delete_if {|_,v| v.nil? }
93
-
94
- assets = {
95
- large_image: data.try_keys(:large_image, 'large_image'),
96
- large_text: data.try_keys(:large_text, 'large_text'),
97
- small_image: data.try_keys(:small_image, 'small_image'),
98
- small_text: data.try_keys(:small_text, 'small_text'),
99
- }.delete_if {|_,v| v.nil? }
100
-
101
- type = PLAYING_TYPE.index(data.try_keys(:type, 'type'))
102
-
103
- game = {
104
- type: type || 0,
105
- name: data.try_keys(:name, 'name'),
106
- url: data.try_keys(:url, 'url'),
107
- details: data.try_keys(:details, 'details'),
108
- state: data.try_keys(:state, 'state'),
109
- application_id: data.try_keys(:application, 'application'),
110
-
111
- timestamps: times.empty? ? nil : times,
112
- assets: assets.empty? ? nil : assets,
113
- }.delete_if {|_,v| v.nil? }
114
-
115
- game
116
- end
117
- end
118
-
119
- class User
120
- include IDObject
121
-
122
- attr_reader :bot
123
-
124
- attr_reader :username
125
- alias_method :name, :username
126
-
127
- attr_reader :discriminator
128
- alias_method :tag, :discriminator
129
-
130
- attr_reader :bot_account
131
- alias_method :bot_account?, :bot_account
132
-
133
- attr_reader :avatar_id
134
-
135
- attr_reader :status
136
-
137
- attr_reader :game
138
-
139
- attr_reader :extra
140
-
141
- def initialize(data, bot)
142
- @bot = bot
143
-
144
- # Kludge for User::resolve2 API call
145
- data = data['user'] if data['user'].is_a?(Hash)
146
-
147
- @id = data['id'].to_i
148
- @bot_account = !!data['bot']
149
- update_data(data)
150
-
151
- @status, @game = :offline, nil
152
-
153
- @roles = {}
154
- end
155
-
156
- def update_data(data)
157
- @username = data.fetch('username', @username)
158
- @discriminator = data.fetch('discriminator', @discriminator)
159
- @avatar_id = data.fetch('avatar', @avatar_id)
160
- end
161
-
162
- def update_presence(presence)
163
- @status = presence['status'].to_sym
164
-
165
- if (game = presence['game'])
166
- @game = Game.new(game)
167
- else
168
- @game = nil
169
- end
170
- end
171
-
172
- def mention
173
- "<@#{@id}>"
174
- end
175
-
176
- alias_method :to_s, :mention
177
-
178
- def distinct
179
- "#{@username}##{@discriminator}"
180
- end
181
-
182
- def pm(text: nil, embed: nil)
183
- if text || embed
184
- pm.send_message(text: text || '', embed: embed)
185
- else
186
- @bot.pm_channel(@id)
187
- end
188
- end
189
-
190
- alias_method :dm, :pm
191
-
192
- def send_file(file, caption: nil)
193
- pm.send_file(file, caption: caption)
194
- end
195
-
196
- def on(server)
197
- id = server.to_id
198
- @bot.server(id).member(@id)
199
- end
200
-
201
- def webhook?
202
- @discriminator == '0000'
203
- end
204
-
205
- def current_bot?
206
- @bot.profile == self
207
- end
208
-
209
- def online?
210
- @status == :online?
211
- end
212
-
213
- def idle?
214
- @status == :idle
215
- end
216
-
217
- alias_method :away?, :idle?
218
-
219
- def dnd?
220
- @status == :dnd
221
- end
222
-
223
- alias_method :busy?, :dnd?
224
-
225
- def invisible?
226
- @status == :invisible
227
- end
228
-
229
- alias_method :hidden?, :invisible?
230
-
231
- def offline?
232
- @status == :offline
233
- end
234
-
235
- def member?
236
- false
237
- end
238
-
239
- def avatar_url(format = nil)
240
- return MijDiscord::Core::API::User.default_avatar(@discriminator) unless @avatar_id
241
- MijDiscord::Core::API::User.avatar_url(@id, @avatar_id, format)
242
- end
243
-
244
- alias_method :avatar, :avatar_url
245
-
246
- def inspect
247
- MijDiscord.make_inspect(self,
248
- :id, :username, :discriminator, :avatar_id, :bot_account)
249
- end
250
-
251
- class << self
252
- def process_avatar(data, format = :png, empty = false)
253
- if data.is_a?(String)
254
- "data:image/#{format};base64,#{data}"
255
- elsif data.respond_to?(:read)
256
- data.binmode if data.respond_to?(:binmode)
257
- data = Base64.strict_encode64(data.read)
258
- "data:image/#{format};base64,#{data}"
259
- elsif empty && %i[none empty].include?(data)
260
- nil
261
- else
262
- raise ArgumentError, 'Invalid avatar data provided'
263
- end
264
- end
265
- end
266
- end
267
-
268
- class Profile < User
269
- attr_reader :mfa_enabled
270
- alias_method :mfa_enabled?, :mfa_enabled
271
-
272
- def update_data(data)
273
- super(data)
274
-
275
- @mfa_enabled = !!data['mfa_enabled']
276
- end
277
-
278
- def set_username(name)
279
- response = MijDiscord::Core::API::User.update_profile(@bot.auth, name, nil)
280
- update_data(JSON.parse(response))
281
- nil
282
- end
283
-
284
- alias_method :username=, :set_username
285
- alias_method :set_name, :set_username
286
- alias_method :name=, :set_username
287
-
288
- def set_avatar(data, format = :png)
289
- data = User.process_avatar(data, format, false)
290
- response = MijDiscord::Core::API::User.update_profile(@bot.auth, nil, data)
291
- update_data(JSON.parse(response))
292
- nil
293
- end
294
-
295
- alias_method :avatar=, :set_avatar
296
- end
1
+ # frozen_string_literal: true
2
+
3
+ module MijDiscord::Data
4
+ class Game
5
+ PLAYING_TYPE = [
6
+ :playing,
7
+ :streaming,
8
+ :listening,
9
+ :watching,
10
+ ].freeze
11
+
12
+ attr_reader :name
13
+
14
+ attr_reader :url
15
+
16
+ attr_reader :details
17
+
18
+ attr_reader :state
19
+
20
+ attr_reader :start_time
21
+
22
+ attr_reader :end_time
23
+
24
+ attr_reader :application
25
+
26
+ attr_reader :large_image
27
+
28
+ attr_reader :large_text
29
+
30
+ attr_reader :small_image
31
+
32
+ attr_reader :small_text
33
+
34
+ def type
35
+ PLAYING_TYPE[@type]
36
+ end
37
+
38
+ def initialize(data)
39
+ @type = data['type']
40
+ @name = data['name']
41
+ @url = data['url']
42
+ @details = data['details']
43
+ @state = data['state']
44
+ @application = data['application_id']
45
+
46
+ if (start_time = data.dig('timestamps', 'start'))
47
+ @start_time = Time.at(start_time).utc
48
+ end
49
+ if (end_time = data.dig('timestamps', 'end'))
50
+ @end_time = Time.at(end_time).utc
51
+ end
52
+
53
+ if (assets = data['assets'])
54
+ @large_image = assets['large_image']
55
+ @large_text = assets['large_text']
56
+ @small_image = assets['small_image']
57
+ @small_text = assets['small_text']
58
+ end
59
+ end
60
+
61
+ def to_hash
62
+ self.class.construct({
63
+ start_time: @start_time,
64
+ end_time: @end_time,
65
+
66
+ large_image: @large_image,
67
+ large_text: @large_text,
68
+ small_image: @small_image,
69
+ small_text: @small_text,
70
+
71
+ type: @type,
72
+ name: @name,
73
+ url: @url,
74
+ details: @details,
75
+ state: @state,
76
+ application: @application,
77
+ })
78
+ end
79
+
80
+ def inspect
81
+ MijDiscord.make_inspect(self,
82
+ :type, :name, :url, :details, :state, :start_time, :end_time,
83
+ :application, :large_image, :large_text, :small_image, :small_text)
84
+ end
85
+
86
+ def self.construct(data)
87
+ data = {name: data} if data.is_a?(String)
88
+
89
+ times = {
90
+ 'start' => data.try_keys(:start_time, 'start_time')&.to_i,
91
+ 'end' => data.try_keys(:end_time, 'end_time')&.to_i,
92
+ }.delete_if {|_,v| v.nil? }
93
+
94
+ assets = {
95
+ 'large_image' => data.try_keys(:large_image, 'large_image')&.to_s,
96
+ 'large_text' => data.try_keys(:large_text, 'large_text')&.to_s,
97
+ 'small_image' => data.try_keys(:small_image, 'small_image')&.to_s,
98
+ 'small_text' => data.try_keys(:small_text, 'small_text')&.to_s,
99
+ }.delete_if {|_,v| v.nil? }
100
+
101
+ game = {
102
+ 'type' => PLAYING_TYPE.index(data.try_keys(:type, 'type')).to_i,
103
+ 'name' => data.try_keys(:name, 'name')&.to_s,
104
+ 'url' => data.try_keys(:url, 'url')&.to_s,
105
+ 'details' => data.try_keys(:details, 'details')&.to_s,
106
+ 'state' => data.try_keys(:state, 'state')&.to_s,
107
+ 'application_id' => data.try_keys(:application, 'application')&.to_s,
108
+
109
+ 'timestamps' => times.empty? ? nil : times,
110
+ 'assets' => assets.empty? ? nil : assets,
111
+ }.delete_if {|_,v| v.nil? }
112
+
113
+ game
114
+ end
115
+ end
116
+
117
+ class User
118
+ include IDObject
119
+
120
+ attr_reader :bot
121
+
122
+ attr_reader :username
123
+ alias_method :name, :username
124
+
125
+ attr_reader :discriminator
126
+ alias_method :tag, :discriminator
127
+
128
+ attr_reader :bot_account
129
+ alias_method :bot_account?, :bot_account
130
+
131
+ attr_reader :avatar_id
132
+
133
+ attr_reader :status
134
+
135
+ attr_reader :game
136
+
137
+ attr_reader :extra
138
+
139
+ def initialize(data, bot)
140
+ @bot = bot
141
+
142
+ # Kludge for User::resolve2 API call
143
+ data = data['user'] if data['user'].is_a?(Hash)
144
+
145
+ @id = data['id'].to_i
146
+ @bot_account = !!data['bot']
147
+ update_data(data)
148
+
149
+ @status, @game = :offline, nil
150
+
151
+ @roles = {}
152
+ end
153
+
154
+ def update_data(data)
155
+ @username = data.fetch('username', @username)
156
+ @discriminator = data.fetch('discriminator', @discriminator)
157
+ @avatar_id = data.fetch('avatar', @avatar_id)
158
+ end
159
+
160
+ def update_presence(presence)
161
+ @status = presence['status'].to_sym
162
+
163
+ if (game = presence['game'])
164
+ @game = Game.new(game)
165
+ else
166
+ @game = nil
167
+ end
168
+ end
169
+
170
+ def mention
171
+ "<@#{@id}>"
172
+ end
173
+
174
+ alias_method :to_s, :mention
175
+
176
+ def distinct
177
+ "#{@username}##{@discriminator}"
178
+ end
179
+
180
+ def pm(text: nil, embed: nil)
181
+ if text || embed
182
+ pm.send_message(text: text || '', embed: embed)
183
+ else
184
+ @bot.pm_channel(@id)
185
+ end
186
+ end
187
+
188
+ alias_method :dm, :pm
189
+
190
+ def send_file(file, caption: nil)
191
+ pm.send_file(file, caption: caption)
192
+ end
193
+
194
+ def on(server)
195
+ id = server.to_id
196
+ @bot.server(id).member(@id)
197
+ end
198
+
199
+ def webhook?
200
+ @discriminator == '0000'
201
+ end
202
+
203
+ def current_bot?
204
+ @bot.profile == self
205
+ end
206
+
207
+ def online?
208
+ @status == :online?
209
+ end
210
+
211
+ def idle?
212
+ @status == :idle
213
+ end
214
+
215
+ alias_method :away?, :idle?
216
+
217
+ def dnd?
218
+ @status == :dnd
219
+ end
220
+
221
+ alias_method :busy?, :dnd?
222
+
223
+ def invisible?
224
+ @status == :invisible
225
+ end
226
+
227
+ alias_method :hidden?, :invisible?
228
+
229
+ def offline?
230
+ @status == :offline
231
+ end
232
+
233
+ def member?
234
+ false
235
+ end
236
+
237
+ def avatar_url(format = nil)
238
+ return MijDiscord::Core::API::User.default_avatar(@discriminator) unless @avatar_id
239
+ MijDiscord::Core::API::User.avatar_url(@id, @avatar_id, format)
240
+ end
241
+
242
+ alias_method :avatar, :avatar_url
243
+
244
+ def inspect
245
+ MijDiscord.make_inspect(self,
246
+ :id, :username, :discriminator, :avatar_id, :bot_account)
247
+ end
248
+
249
+ class << self
250
+ def process_avatar(data, format = :png, empty = false)
251
+ if data.is_a?(String)
252
+ "data:image/#{format};base64,#{data}"
253
+ elsif data.respond_to?(:read)
254
+ data.binmode if data.respond_to?(:binmode)
255
+ data = Base64.strict_encode64(data.read)
256
+ "data:image/#{format};base64,#{data}"
257
+ elsif empty && %i[none empty].include?(data)
258
+ nil
259
+ else
260
+ raise ArgumentError, 'Invalid avatar data provided'
261
+ end
262
+ end
263
+ end
264
+ end
265
+
266
+ class Profile < User
267
+ attr_reader :mfa_enabled
268
+ alias_method :mfa_enabled?, :mfa_enabled
269
+
270
+ def update_data(data)
271
+ super(data)
272
+
273
+ @mfa_enabled = !!data['mfa_enabled']
274
+ end
275
+
276
+ def set_username(name)
277
+ response = MijDiscord::Core::API::User.update_profile(@bot.auth, name, nil)
278
+ update_data(JSON.parse(response))
279
+ nil
280
+ end
281
+
282
+ alias_method :username=, :set_username
283
+ alias_method :set_name, :set_username
284
+ alias_method :name=, :set_username
285
+
286
+ def set_avatar(data, format = :png)
287
+ data = User.process_avatar(data, format, false)
288
+ response = MijDiscord::Core::API::User.update_profile(@bot.auth, nil, data)
289
+ update_data(JSON.parse(response))
290
+ nil
291
+ end
292
+
293
+ alias_method :avatar=, :set_avatar
294
+ end
297
295
  end