-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathPurgeUnusedInvitesCommand.php
More file actions
83 lines (63 loc) · 2.5 KB
/
Copy pathPurgeUnusedInvitesCommand.php
File metadata and controls
83 lines (63 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace He4rt\IntegrationDiscord\Sync\Console;
use He4rt\IntegrationDiscord\Sync\Actions\PurgeUnusedInvitesAction;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Description('Purge unused infinite Discord guild invites (max_age=0, uses=0)')]
#[Signature('discord:purge-invites {guild_id?} {--dry-run : List invites without deleting} {--include-expiring : Also purge unused invites that have an expiration time}')]
final class PurgeUnusedInvitesCommand extends Command
{
public function handle(PurgeUnusedInvitesAction $action): int
{
$guildId = $this->argument('guild_id') ?? config('he4rt.discord.guild_id');
if ($guildId === null) {
$this->error('No guild ID provided and no default configured.');
return self::FAILURE;
}
$dryRun = (bool) $this->option('dry-run');
$includeExpiring = (bool) $this->option('include-expiring');
$scope = $includeExpiring ? 'unused' : 'unused infinite';
$this->info(sprintf(
'%s %s invites for guild %s...',
$dryRun ? 'Scanning' : 'Purging',
$scope,
$guildId,
));
$result = $action->execute((string) $guildId, $dryRun, $includeExpiring);
if ($result['matched'] === 0) {
$this->info(sprintf('No %s invites found. Nothing to do.', $scope));
return self::SUCCESS;
}
$this->table(
['Code', 'Inviter', 'Channel', 'Created At'],
array_map(
static fn (array $invite): array => [
$invite['code'],
$invite['inviter'],
$invite['channel'],
$invite['created_at'],
],
$result['invites'],
),
);
$this->newLine();
$this->info(sprintf(
'Found %d %s invite(s) out of %d total.',
$result['matched'],
$scope,
$result['total'],
));
if ($dryRun) {
$this->warn('DRY RUN -- no invites were deleted.');
return self::SUCCESS;
}
$this->info(sprintf('Deleted %d invite(s).', $result['deleted']));
if ($result['failed'] > 0) {
$this->warn(sprintf('%d invite(s) failed to delete. Check logs for details.', $result['failed']));
return self::FAILURE;
}
return self::SUCCESS;
}
}