Skip to content

Commit

Permalink
add: livewire/alpinejs chatbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Roardom committed Jun 10, 2024
1 parent fe3490d commit 720f11c
Show file tree
Hide file tree
Showing 34 changed files with 1,251 additions and 2,919 deletions.
150 changes: 13 additions & 137 deletions app/Bots/NerdBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,27 @@

namespace App\Bots;

use App\Events\Chatter;
use App\Http\Resources\UserAudibleResource;
use App\Http\Resources\UserEchoResource;
use App\Events\MessageCreated;
use App\Models\Ban;
use App\Models\Bot;
use App\Models\Message;
use App\Models\Peer;
use App\Models\Torrent;
use App\Models\User;
use App\Models\UserAudible;
use App\Models\UserEcho;
use App\Models\Warning;
use App\Repositories\ChatRepository;
use Illuminate\Support\Carbon;

class NerdBot
{
private Bot $bot;

private User $target;

private string $type;

private string $message;

private string $log;

private Carbon $expiresAt;

private Carbon $current;

private string $site;

public function __construct(private readonly ChatRepository $chatRepository)
public function __construct()
{
$this->bot = Bot::findOrFail(2);
$this->expiresAt = Carbon::now()->addMinutes(60);
Expand Down Expand Up @@ -219,44 +207,21 @@ public function getKing(): string
}

/**
* Process Message.
* Handle command.
*/
public function process(string $type, User $user, string $message = '', int $targeted = 0): true|\Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
public function handle(string $message, ?int $roomId = null, ?int $receiverId = null): void
{
$this->target = $user;

if ($type === 'message') {
$x = 0;
$y = 1;
} else {
$x = 1;
$y = 2;
}
[, $command] = preg_split('/ +/', $message, 2) + [null, null];

if ($message === '') {
$log = '';
} else {
$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.';
}

$command = @explode(' ', $message);

$params = $command[$y] ?? null;

if ($params) {
$clone = $command;
array_shift($clone);
array_shift($clone);
array_shift($clone);
}

if (\array_key_exists($x, $command)) {
$log = match($command[$x]) {
MessageCreated::dispatch(Message::create([
'user_id' => User::SYSTEM_USER_ID,
'chatroom_id' => $roomId,
'receiver_id' => $receiverId,
'message' => match($command) {
'banker' => $this->getBanker(),
'bans' => $this->getBans(),
'doubleupload' => $this->getDoubleUpload(),
'freeleech' => $this->getFreeleech(),
'help' => $this->getHelp(),
'king' => $this->getKing(),
'logins' => $this->getLogins(),
'peers' => $this->getPeers(),
Expand All @@ -266,97 +231,8 @@ public function process(string $type, User $user, string $message = '', int $tar
'seeded' => $this->getSeeded(),
'leeched' => $this->getLeeched(),
'snatched' => $this->getSnatched(),
default => '',
};
}

$this->type = $type;
$this->message = $message;
$this->log = $log;

return $this->pm();
}

/**
* Output Message.
*/
public function pm(): true|\Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
{
$type = $this->type;
$target = $this->target;
$txt = $this->log;
$message = $this->message;

if ($type === 'message' || $type === 'private') {
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$target->id,
3600,
fn () => UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $this->bot->id)) {
UserEcho::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
]);

$echoes = UserEcho::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();

cache()->put('user-echoes'.$target->id, $echoes, 3600);

Chatter::dispatch('echo', $target->id, UserEchoResource::collection($echoes));
default => $this->getHelp(),
}

// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$target->id,
3600,
fn () => UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get()
);

if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $this->bot->id)) {
UserAudible::create([
'user_id' => $target->id,
'target_id' => $this->bot->id,
'status' => false,
]);

$audibles = UserAudible::with(['room', 'target', 'bot'])->where('user_id', '=', $target->id)->get();

cache()->put('user-audibles'.$target->id, $audibles, 3600);

Chatter::dispatch('audible', $target->id, UserAudibleResource::collection($audibles));
}

// Create message
if ($txt !== '') {
$roomId = 0;
$this->chatRepository->privateMessage($target->id, $roomId, $message, 1, $this->bot->id);
$this->chatRepository->privateMessage(1, $roomId, $txt, $target->id, $this->bot->id);
}

return response('success');
}

if ($type === 'echo') {
if ($txt !== '') {
$roomId = 0;
$this->chatRepository->botMessage($this->bot->id, $roomId, $txt, $target->id);
}

return response('success');
}

if ($type === 'public') {
if ($txt !== '') {
$this->chatRepository->message($target->id, $target->chatroom->id, $message, null, null);
$this->chatRepository->message(1, $target->chatroom->id, $txt, null, $this->bot->id);
}

return response('success');
}

return true;
]), User::SYSTEM_USER_ID, $roomId, $receiverId);
}
}
Loading

0 comments on commit 720f11c

Please sign in to comment.