1
1
<?php
2
2
3
- namespace App \Console \Commands ;
3
+ namespace App \Console \Commands \ Users ;
4
4
5
+ use App \Jobs \Users \UnsyncDiscord ;
5
6
use App \Services \DiscordService ;
6
- use App \Models \User ;
7
7
use Carbon \Carbon ;
8
8
use App \Models \UserApp ;
9
+ use GuzzleHttp \Exception \ClientException ;
9
10
use Illuminate \Console \Command ;
10
- use Illuminate \Support \Facades \ Log ;
11
+ use Illuminate \Support \Str ;
11
12
12
13
class RegenerateDiscordToken extends Command
13
14
{
14
15
/**
15
16
* The name and signature of the console command.
16
- *
17
- * @var string
18
17
*/
19
18
protected $ signature = 'users:renew-discord-tokens ' ;
20
19
21
20
/**
22
21
* The console command description.
23
- *
24
- * @var string
25
22
*/
26
23
protected $ description = 'Renew a user \'s discord api token. ' ;
27
24
@@ -34,7 +31,6 @@ class RegenerateDiscordToken extends Command
34
31
*/
35
32
public function handle ()
36
33
{
37
- //$userID = $this->argument('user');
38
34
$ this ->service = app ()->make (DiscordService::class);
39
35
40
36
$ tokens = UserApp::select (['id ' , 'user_id ' , 'access_token ' , 'refresh_token ' , 'expires_at ' , 'updated_at ' , 'settings ' ])
@@ -51,8 +47,13 @@ public function handle()
51
47
$ count = 0 ;
52
48
foreach ($ tokens as $ token ) {
53
49
try {
54
- $ this ->service ->user ($ token-> user )->refresh ();
50
+ $ this ->service ->app ($ token )->refresh ();
55
51
$ count ++;
52
+ } catch (ClientException $ e ) {
53
+ if (Str::contains ($ e ->getResponse ()->getBody ()->getContents (), 'invalid_grant ' )) {
54
+ UnsyncDiscord::dispatch ($ token );
55
+ }
56
+ throw $ e ;
56
57
} catch (\Exception $ e ) {
57
58
// Silence errors and ignore
58
59
}
0 commit comments