Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Refactor) Code Quality and Style #1412

Merged
merged 13 commits into from
Jul 27, 2020
92 changes: 47 additions & 45 deletions app/Bots/CasinoBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class CasinoBot
/**
* NerdBot Constructor.
*
* @param ChatRepository $chat
* @param \App\Repositories\ChatRepository $chatRepository
*/
public function __construct(ChatRepository $chat)
public function __construct(ChatRepository $chatRepository)
{
$bot = Bot::where('id', '=', '3')->firstOrFail();
$this->chat = $chat;
$this->chat = $chatRepository;
$this->bot = $bot;
$this->expiresAt = Carbon::now()->addMinutes(60);
$this->current = Carbon::now();
Expand All @@ -67,15 +67,15 @@ public function __construct(ChatRepository $chat)
*/
public function replaceVars($output)
{
$output = str_replace('{me}', $this->bot->name, $output);
$output = str_replace('{command}', $this->bot->command, $output);
if (strstr($output, '{bots}')) {
$output = \str_replace('{me}', $this->bot->name, $output);
$output = \str_replace('{command}', $this->bot->command, $output);
if (\strstr($output, '{bots}')) {
$bot_help = '';
$bots = Bot::where('active', '=', 1)->where('id', '!=', $this->bot->id)->orderBy('position', 'asc')->get();
foreach ($bots as $bot) {
$bot_help .= '( ! | / | @)'.$bot->command.' help triggers help file for '.$bot->name."\n";
}
$output = str_replace('{bots}', $bot_help, $output);
$output = \str_replace('{bots}', $bot_help, $output);
}

return $output;
Expand All @@ -93,10 +93,10 @@ public function replaceVars($output)
*/
public function putDonate($amount = 0, $note = '')
{
$output = implode(' ', $note);
$v = validator(['bot_id' => $this->bot->id, 'amount'=> $amount, 'note'=> $output], [
$output = \implode(' ', $note);
$v = \validator(['bot_id' => $this->bot->id, 'amount'=> $amount, 'note'=> $output], [
'bot_id' => 'required|exists:bots,id|max:999',
'amount' => sprintf('required|numeric|min:1|max:%s', $this->target->seedbonus),
'amount' => \sprintf('required|numeric|min:1|max:%s', $this->target->seedbonus),
'note' => 'required|string',
]);
if ($v->passes()) {
Expand All @@ -107,17 +107,17 @@ public function putDonate($amount = 0, $note = '')
$this->target->seedbonus -= $value;
$this->target->save();

$transaction = new BotTransaction();
$transaction->type = 'bon';
$transaction->cost = $value;
$transaction->user_id = $this->target->id;
$transaction->bot_id = $this->bot->id;
$transaction->to_bot = 1;
$transaction->comment = $output;
$transaction->save();
$botTransaction = new BotTransaction();
$botTransaction->type = 'bon';
$botTransaction->cost = $value;
$botTransaction->user_id = $this->target->id;
$botTransaction->bot_id = $this->bot->id;
$botTransaction->to_bot = 1;
$botTransaction->comment = $output;
$botTransaction->save();

$donations = BotTransaction::with('user', 'bot')->where('bot_id', '=', $this->bot->id)->where('to_bot', '=', 1)->latest()->limit(10)->get();
cache()->put('casinobot-donations', $donations, $this->expiresAt);
\cache()->put('casinobot-donations', $donations, $this->expiresAt);

return 'Your donation to '.$this->bot->name.' for '.$amount.' BON has been sent!';
}
Expand All @@ -136,10 +136,10 @@ public function putDonate($amount = 0, $note = '')
*/
public function getDonations($duration = 'default')
{
$donations = cache()->get('casinobot-donations');
$donations = \cache()->get('casinobot-donations');
if (! $donations || $donations == null) {
$donations = BotTransaction::with('user', 'bot')->where('bot_id', '=', $this->bot->id)->where('to_bot', '=', 1)->latest()->limit(10)->get();
cache()->put('casinobot-donations', $donations, $this->expiresAt);
\cache()->put('casinobot-donations', $donations, $this->expiresAt);
}
$donation_dump = '';
$i = 1;
Expand All @@ -148,7 +148,7 @@ public function getDonations($duration = 'default')
$i++;
}

return "The Most Recent Donations To Me Are As Follows:\n\n".trim($donation_dump);
return "The Most Recent Donations To Me Are As Follows:\n\n".\trim($donation_dump);
}

/**
Expand All @@ -162,16 +162,18 @@ public function getHelp()
/**
* Process Message.
*
* @param $type
* @param User $target
* @param string $message
* @param int $targeted
* @param $type
* @param \App\Models\User $user
* @param string $message
* @param int $targeted
*
* @throws \Exception
*
* @return bool
*/
public function process($type, User $target, $message = '', $targeted = 0)
public function process($type, User $user, $message = '', $targeted = 0)
{
$this->target = $target;
$this->target = $user;
if ($type == 'message') {
$x = 0;
$y = 1;
Expand All @@ -187,23 +189,23 @@ public function process($type, User $target, $message = '', $targeted = 0)
} 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);
$command = @\explode(' ', $message);

$wildcard = null;
$params = null;
if (array_key_exists($y, $command)) {
if (\array_key_exists($y, $command)) {
$params = $command[$y];
}

if ($params != null) {
$clone = $command;
array_shift($clone);
array_shift($clone);
array_shift($clone);
\array_shift($clone);
\array_shift($clone);
\array_shift($clone);
$wildcard = $clone;
}

if (array_key_exists($x, $command)) {
if (\array_key_exists($x, $command)) {
if ($command[$x] === 'donations') {
$log = $this->getDonations($params);
}
Expand Down Expand Up @@ -235,8 +237,8 @@ public function pm()
}
if ($type == 'message' || $type == 'private') {
$receiver_dirty = 0;
$receiver_echoes = cache()->get('user-echoes'.$target->id);
if (! $receiver_echoes || ! is_array($receiver_echoes) || count($receiver_echoes) < 1) {
$receiver_echoes = \cache()->get('user-echoes'.$target->id);
if (! $receiver_echoes || ! \is_array($receiver_echoes) || \count($receiver_echoes) < 1) {
$receiver_echoes = UserEcho::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$target->id])->get();
}
$receiver_listening = false;
Expand All @@ -255,12 +257,12 @@ public function pm()
}
if ($receiver_dirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-echoes'.$target->id, $receiver_echoes, $expiresAt);
event(new Chatter('echo', $target->id, UserEchoResource::collection($receiver_echoes)));
\cache()->put('user-echoes'.$target->id, $receiver_echoes, $expiresAt);
\event(new Chatter('echo', $target->id, UserEchoResource::collection($receiver_echoes)));
}
$receiver_dirty = 0;
$receiver_audibles = cache()->get('user-audibles'.$target->id);
if (! $receiver_audibles || ! is_array($receiver_audibles) || count($receiver_audibles) < 1) {
$receiver_audibles = \cache()->get('user-audibles'.$target->id);
if (! $receiver_audibles || ! \is_array($receiver_audibles) || \count($receiver_audibles) < 1) {
$receiver_audibles = UserAudible::with(['room', 'target', 'bot'])->whereRaw('user_id = ?', [$target->id])->get();
}
$receiver_listening = false;
Expand All @@ -279,24 +281,24 @@ public function pm()
}
if ($receiver_dirty == 1) {
$expiresAt = Carbon::now()->addMinutes(60);
cache()->put('user-audibles'.$target->id, $receiver_audibles, $expiresAt);
event(new Chatter('audible', $target->id, UserAudibleResource::collection($receiver_audibles)));
\cache()->put('user-audibles'.$target->id, $receiver_audibles, $expiresAt);
\event(new Chatter('audible', $target->id, UserAudibleResource::collection($receiver_audibles)));
}
if ($txt != '') {
$room_id = 0;
$message = $this->chat->privateMessage($target->id, $room_id, $message, 1, $this->bot->id);
$message = $this->chat->privateMessage(1, $room_id, $txt, $target->id, $this->bot->id);
}

return response('success');
return \response('success');
}
if ($type == 'echo') {
if ($txt != '') {
$room_id = 0;
$message = $this->chat->botMessage($this->bot->id, $room_id, $txt, $target->id);
}

return response('success');
return \response('success');
}

if ($type == 'public') {
Expand All @@ -305,7 +307,7 @@ public function pm()
$dumproom = $this->chat->message(1, $target->chatroom->id, $txt, null, $this->bot->id);
}

return response('success');
return \response('success');
}

return true;
Expand Down
42 changes: 21 additions & 21 deletions app/Bots/IRCAnnounceBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,38 @@ class IRCAnnounceBot

public function __construct()
{
$this->username = config('irc-bot.username');
$this->channels = config('irc-bot.channels');
$this->server = config('irc-bot.server');
$this->port = config('irc-bot.port');
$this->hostname = config('irc-bot.hostname');
$this->nickservpass = config('irc-bot.nickservpass');
$this->joinchannels = config('irc-bot.joinchannels');
$this->socket = fsockopen($this->server, $this->port);

$this->send_data(sprintf('NICK %s', $this->username));
$this->send_data(sprintf('USER %s %s %s %s', $this->username, $this->hostname, $this->server, $this->username));
$this->username = \config('irc-bot.username');
$this->channels = \config('irc-bot.channels');
$this->server = \config('irc-bot.server');
$this->port = \config('irc-bot.port');
$this->hostname = \config('irc-bot.hostname');
$this->nickservpass = \config('irc-bot.nickservpass');
$this->joinchannels = \config('irc-bot.joinchannels');
$this->socket = \fsockopen($this->server, $this->port);

$this->send_data(\sprintf('NICK %s', $this->username));
$this->send_data(\sprintf('USER %s %s %s %s', $this->username, $this->hostname, $this->server, $this->username));

$this->connect();
}

public function __destruct()
{
if ($this->socket) {
fclose($this->socket);
\fclose($this->socket);
}
}

private function connect()
{
while ($data = fgets($this->socket)) {
flush();
$ex = explode(' ', $data);
while ($data = \fgets($this->socket)) {
\flush();
$ex = \explode(' ', $data);

if ($ex[0] === 'PING') {
$this->send_data('PONG '.$ex[1]);
if ($this->nickservpass) {
$this->send_data(sprintf('NICKSERV IDENTIFY %s', $this->nickservpass));
$this->send_data(\sprintf('NICKSERV IDENTIFY %s', $this->nickservpass));
}

return;
Expand All @@ -86,24 +86,24 @@ private function connect()

private function send_data($data)
{
fwrite($this->socket, sprintf('%s
\fwrite($this->socket, \sprintf('%s
', $data));
}

private function say($channel, $string)
{
$this->send_data(sprintf('PRIVMSG %s %s', $channel, $string));
$this->send_data(\sprintf('PRIVMSG %s %s', $channel, $string));
}

private function join($channel)
{
$this->send_data(sprintf('JOIN %s', $channel));
$this->send_data(\sprintf('JOIN %s', $channel));
}

public function message($channel, $message)
{
// Messages an specific IRC Channel
if ($this->joinchannels && preg_match('##(\w*[a-zA-Z_0-9]+\w*)#', $channel)) {
if ($this->joinchannels && \preg_match('##(\w*[a-zA-Z_0-9]+\w*)#', $channel)) {
$this->join($channel);
}

Expand All @@ -113,7 +113,7 @@ public function message($channel, $message)
public function broadcast($message, $channels = null)
{
// Broadcast to all IRC Channels in config
$channels = (is_null($channels)) ? $this->channels : $channels;
$channels = (\is_null($channels)) ? $this->channels : $channels;
foreach ($channels as $channel) {
$this->message($channel, $message);
}
Expand Down
Loading