GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SetupCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 128
ccs 44
cts 44
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSocket() 0 3 1
A getGateway() 0 13 2
A handle() 0 52 5
1
<?php
2
3
namespace NotificationChannels\Discord\Commands;
4
5
use Exception;
6
use GuzzleHttp\Client as HttpClient;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Arr;
9
use WebSocket\Client;
10
11
class SetupCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $signature = 'discord:setup';
17
18
    /**
19
     * @var string
20
     */
21
    protected $description = "Add the bot to your server(s) and identify it with Discord's gateway.";
22
23
    /**
24
     * @var \GuzzleHttp\Client
25
     */
26
    protected $guzzle;
27
28
    /**
29
     * @var string
30
     */
31
    protected $token = null;
32
33
    /**
34
     * @var string
35
     */
36
    protected $gateway = 'wss://gateway.discord.gg';
37
38
    /**
39
     * @param \GuzzleHttp\Client $guzzle
40
     * @param string|null $token
41
     */
42 7
    public function __construct(HttpClient $guzzle, $token = null)
43
    {
44 7
        parent::__construct();
45
46 7
        $this->guzzle = $guzzle;
47 7
        $this->token = $token;
48
    }
49
50
    /**
51
     * Attempt to connect and identify a bot with the Discord websocket gateway.
52
     *
53
     * @return int|void
54
     */
55 4
    public function handle()
56
    {
57 4
        if (! $this->token) {
58 1
            $this->error('You must paste your Discord token (App Bot User token) into your `services.php` config file.');
59 1
            $this->error('View the README for more info: https://github.com/laravel-notification-channels/discord#installation');
60
61 1
            return -1;
62
        }
63
64 3
        if (! $this->confirm('Is the bot already added to your server?')) {
65 2
            $clientId = $this->ask('What is your Discord app client ID?');
66
67 2
            $this->warn('Add the bot to your server by visiting this link: https://discord.com/oauth2/authorize?&client_id='.$clientId.'&scope=bot&permissions=0');
68
69 2
            if (! $this->confirm('Continue?', true)) {
70 1
                return -1;
71
            }
72
        }
73
74 2
        $this->warn("Attempting to identify the bot with Discord's websocket gateway...");
75
76 2
        $this->gateway = $this->getGateway();
77
78 2
        $this->warn("Connecting to '$this->gateway'...");
79
80 2
        $client = $this->getSocket($this->gateway);
81
82
        // Discord requires all bots to connect via a websocket connection and
83
        // identify at least once before any API requests over HTTP are allowed.
84
        // https://discord.com/developers/docs/topics/gateway#gateway-identify
85 2
        $client->send(json_encode([
86 2
            'op' => 2,
87 2
            'd' => [
88 2
                'token' => $this->token,
89 2
                'properties' => [
90 2
                    '$os' => PHP_OS,
91 2
                    '$browser' => 'laravel-notification-channels-discord',
92 2
                    '$device' => 'laravel-notification-channels-discord',
93 2
                ],
94 2
            ],
95 2
        ]));
96
97 2
        $response = $client->receive();
98 2
        $identified = Arr::get(json_decode($response, true), 'op') === 10;
99
100 2
        if (! $identified) {
101 1
            $this->error("Discord responded with an error while trying to identify the bot: $response");
102
103 1
            return -1;
104
        }
105
106 1
        $this->info('Your bot has been identified by Discord and can now send API requests!');
107
    }
108
109
    /**
110
     * Get a websocket client for the given gateway.
111
     *
112
     * @param string $gateway
113
     *
114
     * @return \WebSocket\Client
115
     */
116 1
    public function getSocket($gateway)
117
    {
118 1
        return new Client($gateway);
119
    }
120
121
    /**
122
     * Get the URL of the gateway that the socket should connect to.
123
     *
124
     * @return string
125
     */
126 2
    public function getGateway()
127
    {
128 2
        $gateway = $this->gateway;
129
130
        try {
131 2
            $response = $this->guzzle->get('https://discord.com/api/gateway');
132
133 1
            $gateway = Arr::get(json_decode($response->getBody(), true), 'url', $gateway);
134 1
        } catch (Exception $e) {
135 1
            $this->warn("Could not get a websocket gateway address, defaulting to '{$gateway}'.");
136
        }
137
138 2
        return $gateway;
139
    }
140
}
141