16
16
17
17
namespace App \Bots ;
18
18
19
- use App \Events \Chatter ;
20
- use App \Http \Resources \UserAudibleResource ;
21
- use App \Http \Resources \UserEchoResource ;
19
+ use App \Events \MessageCreated ;
22
20
use App \Models \Ban ;
23
21
use App \Models \Bot ;
22
+ use App \Models \Message ;
24
23
use App \Models \Peer ;
25
24
use App \Models \Torrent ;
26
25
use App \Models \User ;
27
- use App \Models \UserAudible ;
28
- use App \Models \UserEcho ;
29
26
use App \Models \Warning ;
30
- use App \Repositories \ChatRepository ;
31
27
use Illuminate \Support \Carbon ;
32
28
33
29
class NerdBot
34
30
{
35
31
private Bot $ bot ;
36
32
37
- private User $ target ;
38
-
39
- private string $ type ;
40
-
41
- private string $ message ;
42
-
43
- private string $ log ;
44
-
45
33
private Carbon $ expiresAt ;
46
34
47
35
private Carbon $ current ;
48
36
49
37
private string $ site ;
50
38
51
- public function __construct (private readonly ChatRepository $ chatRepository )
39
+ public function __construct ()
52
40
{
53
41
$ this ->bot = Bot::findOrFail (2 );
54
42
$ this ->expiresAt = Carbon::now ()->addMinutes (60 );
@@ -219,44 +207,21 @@ public function getKing(): string
219
207
}
220
208
221
209
/**
222
- * Process Message .
210
+ * Handle command .
223
211
*/
224
- public function process (string $ type , User $ user , string $ message = '' , int $ targeted = 0 ): true | \ Illuminate \ Http \ Response | \ Illuminate \ Contracts \ Routing \ ResponseFactory
212
+ public function handle (string $ message , ? int $ roomId = null , ? int $ receiverId = null ): void
225
213
{
226
- $ this ->target = $ user ;
227
-
228
- if ($ type === 'message ' ) {
229
- $ x = 0 ;
230
- $ y = 1 ;
231
- } else {
232
- $ x = 1 ;
233
- $ y = 2 ;
234
- }
214
+ [, $ command ] = preg_split ('/ +/ ' , $ message , 2 ) + [null , null ];
235
215
236
- if ($ message === '' ) {
237
- $ log = '' ;
238
- } else {
239
- $ log = 'All ' .$ this ->bot ->name .' commands must be a private message or begin with / ' .$ this ->bot ->command .' or ! ' .$ this ->bot ->command .'. Need help? Type / ' .$ this ->bot ->command .' help and you shall be helped. ' ;
240
- }
241
-
242
- $ command = @explode (' ' , $ message );
243
-
244
- $ params = $ command [$ y ] ?? null ;
245
-
246
- if ($ params ) {
247
- $ clone = $ command ;
248
- array_shift ($ clone );
249
- array_shift ($ clone );
250
- array_shift ($ clone );
251
- }
252
-
253
- if (\array_key_exists ($ x , $ command )) {
254
- $ log = match ($ command [$ x ]) {
216
+ MessageCreated::dispatch (Message::create ([
217
+ 'user_id ' => User::SYSTEM_USER_ID ,
218
+ 'chatroom_id ' => $ roomId ,
219
+ 'receiver_id ' => $ receiverId ,
220
+ 'message ' => match ($ command ) {
255
221
'banker ' => $ this ->getBanker (),
256
222
'bans ' => $ this ->getBans (),
257
223
'doubleupload ' => $ this ->getDoubleUpload (),
258
224
'freeleech ' => $ this ->getFreeleech (),
259
- 'help ' => $ this ->getHelp (),
260
225
'king ' => $ this ->getKing (),
261
226
'logins ' => $ this ->getLogins (),
262
227
'peers ' => $ this ->getPeers (),
@@ -266,97 +231,8 @@ public function process(string $type, User $user, string $message = '', int $tar
266
231
'seeded ' => $ this ->getSeeded (),
267
232
'leeched ' => $ this ->getLeeched (),
268
233
'snatched ' => $ this ->getSnatched (),
269
- default => '' ,
270
- };
271
- }
272
-
273
- $ this ->type = $ type ;
274
- $ this ->message = $ message ;
275
- $ this ->log = $ log ;
276
-
277
- return $ this ->pm ();
278
- }
279
-
280
- /**
281
- * Output Message.
282
- */
283
- public function pm (): true |\Illuminate \Http \Response |\Illuminate \Contracts \Routing \ResponseFactory
284
- {
285
- $ type = $ this ->type ;
286
- $ target = $ this ->target ;
287
- $ txt = $ this ->log ;
288
- $ message = $ this ->message ;
289
-
290
- if ($ type === 'message ' || $ type === 'private ' ) {
291
- // Create echo for user if missing
292
- $ echoes = cache ()->remember (
293
- 'user-echoes ' .$ target ->id ,
294
- 3600 ,
295
- fn () => UserEcho::with (['room ' , 'target ' , 'bot ' ])->where ('user_id ' , '= ' , $ target ->id )->get ()
296
- );
297
-
298
- if ($ echoes ->doesntContain (fn ($ echo ) => $ echo ->bot_id == $ this ->bot ->id )) {
299
- UserEcho::create ([
300
- 'user_id ' => $ target ->id ,
301
- 'target_id ' => $ this ->bot ->id ,
302
- ]);
303
-
304
- $ echoes = UserEcho::with (['room ' , 'target ' , 'bot ' ])->where ('user_id ' , '= ' , $ target ->id )->get ();
305
-
306
- cache ()->put ('user-echoes ' .$ target ->id , $ echoes , 3600 );
307
-
308
- Chatter::dispatch ('echo ' , $ target ->id , UserEchoResource::collection ($ echoes ));
234
+ default => $ this ->getHelp (),
309
235
}
310
-
311
- // Create audible for user if missing
312
- $ audibles = cache ()->remember (
313
- 'user-audibles ' .$ target ->id ,
314
- 3600 ,
315
- fn () => UserAudible::with (['room ' , 'target ' , 'bot ' ])->where ('user_id ' , '= ' , $ target ->id )->get ()
316
- );
317
-
318
- if ($ audibles ->doesntContain (fn ($ audible ) => $ audible ->bot_id == $ this ->bot ->id )) {
319
- UserAudible::create ([
320
- 'user_id ' => $ target ->id ,
321
- 'target_id ' => $ this ->bot ->id ,
322
- 'status ' => false ,
323
- ]);
324
-
325
- $ audibles = UserAudible::with (['room ' , 'target ' , 'bot ' ])->where ('user_id ' , '= ' , $ target ->id )->get ();
326
-
327
- cache ()->put ('user-audibles ' .$ target ->id , $ audibles , 3600 );
328
-
329
- Chatter::dispatch ('audible ' , $ target ->id , UserAudibleResource::collection ($ audibles ));
330
- }
331
-
332
- // Create message
333
- if ($ txt !== '' ) {
334
- $ roomId = 0 ;
335
- $ this ->chatRepository ->privateMessage ($ target ->id , $ roomId , $ message , 1 , $ this ->bot ->id );
336
- $ this ->chatRepository ->privateMessage (1 , $ roomId , $ txt , $ target ->id , $ this ->bot ->id );
337
- }
338
-
339
- return response ('success ' );
340
- }
341
-
342
- if ($ type === 'echo ' ) {
343
- if ($ txt !== '' ) {
344
- $ roomId = 0 ;
345
- $ this ->chatRepository ->botMessage ($ this ->bot ->id , $ roomId , $ txt , $ target ->id );
346
- }
347
-
348
- return response ('success ' );
349
- }
350
-
351
- if ($ type === 'public ' ) {
352
- if ($ txt !== '' ) {
353
- $ this ->chatRepository ->message ($ target ->id , $ target ->chatroom ->id , $ message , null , null );
354
- $ this ->chatRepository ->message (1 , $ target ->chatroom ->id , $ txt , null , $ this ->bot ->id );
355
- }
356
-
357
- return response ('success ' );
358
- }
359
-
360
- return true ;
236
+ ]), User::SYSTEM_USER_ID , $ roomId , $ receiverId );
361
237
}
362
238
}
0 commit comments