|
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
|
5 |
|
public function __construct(HttpClient $guzzle, $token = null) |
|
43
|
|
|
{ |
|
44
|
5 |
|
parent::__construct(); |
|
45
|
|
|
|
|
46
|
5 |
|
$this->guzzle = $guzzle; |
|
47
|
5 |
|
$this->token = $token; |
|
48
|
5 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Attempt to connect and identify a bot with the Discord websocket gateway. |
|
52
|
|
|
* |
|
53
|
|
|
* @return int|void |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function handle() |
|
56
|
|
|
{ |
|
57
|
2 |
|
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
|
1 |
|
if (! $this->confirm('Is the bot already added to your server?')) { |
|
65
|
1 |
|
$clientId = $this->ask('What is your Discord app client ID?'); |
|
66
|
|
|
|
|
67
|
1 |
|
$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
|
|
|
if (! $this->confirm('Continue?', true)) { |
|
70
|
|
|
return -1; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->warn("Attempting to identify the bot with Discord's websocket gateway..."); |
|
75
|
|
|
|
|
76
|
|
|
$this->gateway = $this->getGateway(); |
|
77
|
|
|
|
|
78
|
|
|
$this->warn("Connecting to '$this->gateway'..."); |
|
79
|
|
|
|
|
80
|
|
|
$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
|
|
|
$client->send(json_encode([ |
|
86
|
|
|
'op' => 2, |
|
87
|
|
|
'd' => [ |
|
88
|
|
|
'token' => $this->token, |
|
89
|
|
|
'properties' => [ |
|
90
|
|
|
'$os' => PHP_OS, |
|
91
|
|
|
'$browser' => 'laravel-notification-channels-discord', |
|
92
|
|
|
'$device' => 'laravel-notification-channels-discord', |
|
93
|
|
|
], |
|
94
|
|
|
], |
|
95
|
|
|
])); |
|
96
|
|
|
|
|
97
|
|
|
$response = $client->receive(); |
|
98
|
|
|
$identified = Arr::get(json_decode($response, true), 'op') === 10; |
|
99
|
|
|
|
|
100
|
|
|
if (! $identified) { |
|
101
|
|
|
$this->error("Discord responded with an error while trying to identify the bot: $response"); |
|
102
|
|
|
|
|
103
|
|
|
return -1; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$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
|
|
|
$gateway = Arr::get(json_decode($response->getBody(), true), 'url', $gateway); |
|
134
|
2 |
|
} catch (Exception $e) { |
|
135
|
2 |
|
$this->warn("Could not get a websocket gateway address, defaulting to '{$gateway}'."); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
return $gateway; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|