1 | <?php |
||
11 | class SetupCommand extends Command |
||
12 | { |
||
13 | protected $signature = 'discord:setup'; |
||
14 | |||
15 | protected $description = "Add the bot to your server(s) and identify it with Discord's gateway."; |
||
16 | |||
17 | /** |
||
18 | * @var \GuzzleHttp\Client |
||
19 | */ |
||
20 | protected $guzzle; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $token = null; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $gateway = 'wss://gateway.discord.gg'; |
||
31 | |||
32 | 7 | public function __construct(HttpClient $guzzle, $token = null) |
|
39 | |||
40 | 4 | public function handle() |
|
41 | { |
||
42 | 4 | if (! $this->token) { |
|
43 | 1 | $this->error('You must paste your Discord token (App Bot User token) into your `services.php` config file.'); |
|
44 | 1 | $this->error('View the README for more info: https://github.com/laravel-notification-channels/discord#installation'); |
|
45 | |||
46 | 1 | return -1; |
|
47 | } |
||
48 | |||
49 | 3 | if (! $this->confirm('Is the bot already added to your server?')) { |
|
50 | 2 | $clientId = $this->ask('What is your Discord app client ID?'); |
|
51 | |||
52 | 2 | $this->warn('Add the bot to your server by visiting this link: https://discordapp.com/oauth2/authorize?&client_id='.$clientId.'&scope=bot&permissions=0'); |
|
53 | |||
54 | 2 | if (! $this->confirm('Continue?', true)) { |
|
55 | 1 | return -1; |
|
56 | } |
||
57 | 1 | } |
|
58 | |||
59 | 2 | $this->warn("Attempting to identify the bot with Discord's websocket gateway..."); |
|
60 | |||
61 | 2 | $this->gateway = $this->getGateway(); |
|
62 | |||
63 | 2 | $this->warn("Connecting to '$this->gateway'..."); |
|
64 | |||
65 | 2 | $client = $this->getSocket($this->gateway); |
|
66 | |||
67 | // Discord requires all bots to connect via a websocket connection and |
||
68 | // identify at least once before any HTTP API requests are allowed. |
||
69 | // https://discordapp.com/developers/docs/topics/gateway#gateway-identify |
||
70 | 2 | $client->send(json_encode([ |
|
71 | 2 | 'op' => 2, |
|
72 | 'd' => [ |
||
73 | 2 | 'token' => $this->token, |
|
74 | 2 | 'v' => 3, |
|
75 | 2 | 'compress' => false, |
|
76 | 'properties' => [ |
||
77 | 2 | '$os' => PHP_OS, |
|
78 | 2 | '$browser' => 'laravel-notification-channels-discord', |
|
79 | 2 | '$device' => 'laravel-notification-channels-discord', |
|
80 | 2 | '$referrer' => '', |
|
81 | 2 | '$referring_domain' => '', |
|
82 | 2 | ], |
|
83 | 2 | ], |
|
84 | 2 | ])); |
|
85 | |||
86 | 2 | $response = $client->receive(); |
|
87 | 2 | $identified = Arr::get(json_decode($response, true), 't') === 'READY'; |
|
88 | |||
89 | 2 | if (! $identified) { |
|
90 | 1 | $this->error("Discord responded with an error while trying to identify the bot: $response"); |
|
91 | |||
92 | 1 | return -1; |
|
93 | } |
||
94 | |||
95 | 1 | $this->info('Your bot has been identified by Discord and can now send API requests!'); |
|
96 | 1 | } |
|
97 | |||
98 | 1 | public function getSocket($gateway) |
|
99 | { |
||
100 | 1 | return new Client($gateway); |
|
101 | } |
||
102 | |||
103 | 2 | public function getGateway() |
|
121 | } |
||
122 |