mirror of
https://github.com/HDInnovations/UNIT3D-Community-Edition.git
synced 2026-04-25 12:38:46 -05:00
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:
+26
-35
@@ -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
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user