-
app/Console/Commands/AutoBan.php
Open in GitHubuse App\Mail\BanUser; use App\Models\Ban; use App\Models\Group; use App\Models\Warning; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Mail; class AutoBan extends Command { protected $signature = 'auto:ban'; public function handle() { $bannedGroup = \cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id')); $bans = Warning::with('warneduser')->select(DB::raw('user_id, count(*) as value'))->where('active', '=', 1)->groupBy('user_id')->having('value', '>=', \config('hitrun.max_warnings'))->get(); foreach ($bans as $ban) { if ($ban->warneduser->group_id != $bannedGroup[0] && ! $ban->warneduser->group->is_immune) { $ban->warneduser->group_id = $bannedGroup[0]; $ban->warneduser->can_upload = 0; $ban->warneduser->can_download = 0; $ban->warneduser->can_comment = 0; $ban->warneduser->can_invite = 0; $ban->warneduser->can_request = 0; $ban->warneduser->can_chat = 0; $ban->warneduser->save(); $logban = new Ban(); $logban->owned_by = $ban->warneduser->id; $logban->created_by = 1; $logban->ban_reason = 'Warning Limit Reached, has '.$ban->value.' warnings.'; $logban->unban_reason = ''; $logban->save(); Mail::to($ban->warneduser->email)->send(new BanUser($ban->warneduser->email, $logban)); } } $this->comment('Automated User Banning Command Complete'); } }