refactor: cleanup workaround for creating systembot echoes/audibles

When the user changes the bot in the front-end (e.g. clicks the systembot button), it calls the bot pm method with an empty string and the pm method creates audibles/echoes but doesn't create the message if it's an empty string. Feels like an awfully hacky method, so I'm making the logic more explicit.
This commit is contained in:
Roardom
2025-08-22 05:08:33 +00:00
parent 94b68d5097
commit 28c99c9736
3 changed files with 70 additions and 60 deletions
+26 -35
View File
@@ -232,7 +232,7 @@ class NerdBot
/**
* Process Message.
*/
public function process(string $type, User $user, string $message = ''): true|\Illuminate\Http\Response
public function process(string $type, User $user, string $message): true|\Illuminate\Http\Response
{
$this->target = $user;
@@ -242,30 +242,27 @@ class NerdBot
[, $command,] = mb_split(' +', trim($message), 3) + [null, null, null];
}
if ($command !== null && $message !== '') {
$log = match($command) {
'banker' => $this->getBanker(),
'bans' => $this->getBans(),
'unbans' => $this->getUnbans(),
'doubleupload' => $this->getDoubleUpload(),
'freeleech' => $this->getFreeleech(),
'help' => $this->getHelp(),
'king' => $this->getKing(),
'logins' => $this->getLogins(),
'peers' => $this->getPeers(),
'registrations' => $this->getRegistrations(),
'uploads' => $this->getUploads(),
'warnings' => $this->getWarnings(),
'seeded' => $this->getSeeded(),
'leeched' => $this->getLeeched(),
'snatched' => $this->getSnatched(),
default => '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.',
};
}
$this->log = match($command) {
'banker' => $this->getBanker(),
'bans' => $this->getBans(),
'unbans' => $this->getUnbans(),
'doubleupload' => $this->getDoubleUpload(),
'freeleech' => $this->getFreeleech(),
'help' => $this->getHelp(),
'king' => $this->getKing(),
'logins' => $this->getLogins(),
'peers' => $this->getPeers(),
'registrations' => $this->getRegistrations(),
'uploads' => $this->getUploads(),
'warnings' => $this->getWarnings(),
'seeded' => $this->getSeeded(),
'leeched' => $this->getLeeched(),
'snatched' => $this->getSnatched(),
default => '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.',
};
$this->type = $type;
$this->message = $message;
$this->log = $log;
return $this->pm();
}
@@ -319,29 +316,23 @@ class NerdBot
}
// 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);
}
$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);
}
$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);
}
$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');
}
+9 -19
View File
@@ -122,7 +122,7 @@ class SystemBot
/**
* Process Message.
*/
public function process(string $type, User $user, string $message = ''): \Illuminate\Http\Response|bool
public function process(string $type, User $user, string $message): \Illuminate\Http\Response|bool
{
$this->target = $user;
@@ -136,11 +136,7 @@ class SystemBot
$z = 3;
}
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.';
}
$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);
@@ -215,29 +211,23 @@ class SystemBot
}
// 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);
}
$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);
}
$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);
}
$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');
}
+35 -6
View File
@@ -126,16 +126,45 @@ class ChatController extends Controller
/* MESSAGES */
public function botMessages(Request $request, int $botId): \Illuminate\Http\Resources\Json\AnonymousResourceCollection
{
$runbot = null;
$bot = Bot::findOrFail($botId);
$user = $request->user();
if ($bot->is_systembot) {
$runbot = new SystemBot($this->chatRepository);
} elseif ($bot->is_nerdbot) {
$runbot = new NerdBot($this->chatRepository);
// Create echo for user if missing
$echoes = cache()->remember(
'user-echoes'.$user->id,
3600,
fn () => UserEcho::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user->id)->get()
);
if ($echoes->doesntContain(fn ($echo) => $echo->bot_id == $bot->id)) {
$echoes->push(UserEcho::create([
'user_id' => $user->id,
'bot_id' => $bot->id,
]));
cache()->put('user-echoes'.$user->id, $echoes, 3600);
Chatter::dispatch('echo', $user->id, UserEchoResource::collection($echoes));
}
$runbot->process('message', $request->user(), '');
// Create audible for user if missing
$audibles = cache()->remember(
'user-audibles'.$user->id,
3600,
fn () => UserAudible::with(['user', 'room', 'target', 'bot'])->where('user_id', '=', $user->id)->get()
);
if ($audibles->doesntContain(fn ($audible) => $audible->bot_id == $bot->id)) {
$audibles->push(UserAudible::create([
'user_id' => $user->id,
'bot_id' => $bot->id,
'status' => 0,
]));
cache()->put('user-audibles'.$user->id, $audibles, 3600);
Chatter::dispatch('audible', $user->id, UserAudibleResource::collection($audibles));
}
return ChatMessageResource::collection($this->chatRepository->botMessages($request->user()->id, $bot->id));
}