Skip to content

Commit 08510e2

Browse files
committed
add: livewire/alpinejs chatbox
1 parent fe3490d commit 08510e2

34 files changed

+1251
-2919
lines changed

app/Bots/NerdBot.php

+13-137
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,27 @@
1616

1717
namespace App\Bots;
1818

19-
use App\Events\Chatter;
20-
use App\Http\Resources\UserAudibleResource;
21-
use App\Http\Resources\UserEchoResource;
19+
use App\Events\MessageCreated;
2220
use App\Models\Ban;
2321
use App\Models\Bot;
22+
use App\Models\Message;
2423
use App\Models\Peer;
2524
use App\Models\Torrent;
2625
use App\Models\User;
27-
use App\Models\UserAudible;
28-
use App\Models\UserEcho;
2926
use App\Models\Warning;
30-
use App\Repositories\ChatRepository;
3127
use Illuminate\Support\Carbon;
3228

3329
class NerdBot
3430
{
3531
private Bot $bot;
3632

37-
private User $target;
38-
39-
private string $type;
40-
41-
private string $message;
42-
43-
private string $log;
44-
4533
private Carbon $expiresAt;
4634

4735
private Carbon $current;
4836

4937
private string $site;
5038

51-
public function __construct(private readonly ChatRepository $chatRepository)
39+
public function __construct()
5240
{
5341
$this->bot = Bot::findOrFail(2);
5442
$this->expiresAt = Carbon::now()->addMinutes(60);
@@ -219,44 +207,21 @@ public function getKing(): string
219207
}
220208

221209
/**
222-
* Process Message.
210+
* Handle command.
223211
*/
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
225213
{
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];
235215

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) {
255221
'banker' => $this->getBanker(),
256222
'bans' => $this->getBans(),
257223
'doubleupload' => $this->getDoubleUpload(),
258224
'freeleech' => $this->getFreeleech(),
259-
'help' => $this->getHelp(),
260225
'king' => $this->getKing(),
261226
'logins' => $this->getLogins(),
262227
'peers' => $this->getPeers(),
@@ -266,97 +231,8 @@ public function process(string $type, User $user, string $message = '', int $tar
266231
'seeded' => $this->getSeeded(),
267232
'leeched' => $this->getLeeched(),
268233
'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(),
309235
}
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);
361237
}
362238
}

0 commit comments

Comments
 (0)