Skip to content

Commit 508760c

Browse files
committed
Add Discord webhook for server status updates
1 parent 602f35f commit 508760c

File tree

3 files changed

+103
-11
lines changed

3 files changed

+103
-11
lines changed

etc/config.sample.json

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"ignore_event_types": [3,5,7,8,9,10,11],
5757
"webhook": null
5858
},
59+
"forward_server_updates": {
60+
"enabled": false,
61+
"ignore_server_ids": [],
62+
"webhook": null
63+
},
5964
"invite_code": "u87WVeu",
6065
"server_id": 405789880749260820
6166
},

src/controllers/Server/UpdateJob.php

+77-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace BNETDocs\Controllers\Server;
44

5+
use \BNETDocs\Libraries\Discord\Embed as DiscordEmbed;
6+
use \BNETDocs\Libraries\Discord\EmbedField as DiscordEmbedField;
7+
use \BNETDocs\Libraries\Discord\Webhook as DiscordWebhook;
58
use \BNETDocs\Libraries\Exceptions\ServerNotFoundException;
69
use \BNETDocs\Libraries\Server;
710

@@ -16,6 +19,11 @@
1619
use \DateTimeZone;
1720

1821
class UpdateJob extends Controller {
22+
23+
const S_DISABLED = ':no_entry: Disabled';
24+
const S_ONLINE = ':white_check_mark: Online';
25+
const S_OFFLINE = ':x: Offline';
26+
1927
public function &run( Router &$router, ViewLib &$view, array &$args ) {
2028
$model = new UpdateJobModel();
2129
$action = $router->getRequestMethod();
@@ -36,7 +44,7 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
3644
);
3745

3846
$status = (
39-
isset( $q[ 'status' ]) ? $q[ 'status' ] : null
47+
isset( $q[ 'status' ]) ? (int) $q[ 'status' ] : null
4048
);
4149

4250
if ( !is_null( $server_id )) $server_id = (int) $server_id;
@@ -47,13 +55,9 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
4755
);
4856

4957
if ( !( is_int( $server_id ) && is_int( $status ))) {
50-
5158
$model->_responseCode = 400;
52-
5359
} else if ( !$authenticated ) {
54-
5560
$model->_responseCode = 403;
56-
5761
} else {
5862

5963
try {
@@ -63,24 +67,88 @@ public function &run( Router &$router, ViewLib &$view, array &$args ) {
6367
}
6468

6569
if ( !$model->server ) {
66-
6770
$model->_responseCode = 404;
68-
6971
} else {
7072

7173
$model->old_status_bitmask = $model->server->getStatusBitmask();
72-
7374
$model->server->setStatusBitmask( $status );
7475

7576
if ( $model->server->save() ) {
77+
$discord = Common::$config->discord->forward_server_updates;
78+
if ( $discord && $discord->enabled
79+
&& !in_array( $server_id, $discord->ignore_server_ids )) {
80+
self::dispatchDiscord(
81+
$model->server, $discord->webhook,
82+
$model->old_status_bitmask, $status
83+
);
84+
}
85+
7686
$model->_responseCode = 200;
7787
}
78-
7988
}
8089
}
8190
}
8291

8392
$view->render( $model );
8493
return $model;
8594
}
95+
96+
protected static function dispatchDiscord( $server, $webhook, $old, $new ) {
97+
if ($old === $new) return;
98+
99+
if ($old & Server::STATUS_DISABLED) {
100+
$old_status = self::S_DISABLED;
101+
} else if ($old & Server::STATUS_ONLINE) {
102+
$old_status = self::S_ONLINE;
103+
} else if (!($old & Server::STATUS_ONLINE)) {
104+
$old_status = self::S_OFFLINE;
105+
} else {
106+
$old_status = sprintf('Unknown (%d)', $old);
107+
}
108+
109+
if ($new & Server::STATUS_DISABLED) {
110+
$title = 'Server Disabled';
111+
$new_status = self::S_DISABLED;
112+
} else if ($new & Server::STATUS_ONLINE) {
113+
$title = 'Server Online';
114+
$new_status = self::S_ONLINE;
115+
} else if (!($new & Server::STATUS_ONLINE)) {
116+
$title = 'Server Offline';
117+
$new_status = self::S_OFFLINE;
118+
} else {
119+
$title = 'Generic Status Change';
120+
$new_status = sprintf('Unknown (%d)', $new);
121+
}
122+
123+
$label = $server->getLabel();
124+
125+
if (!empty($label)) {
126+
$title .= ': ' . $label;
127+
}
128+
129+
$webhook = new DiscordWebhook($webhook);
130+
$embed = new DiscordEmbed();
131+
132+
$embed->setUrl($server->getURI());
133+
$embed->setTitle($title);
134+
$embed->setTimestamp(new DateTime('now', new DateTimeZone('Etc/UTC')));
135+
136+
$data = array();
137+
$data['Type'] = $server->getType()->getLabel();
138+
139+
if (!empty($label)) {
140+
$data['Label'] = $label;
141+
}
142+
143+
$data['Server'] = $server->getAddress() . ':' . $server->getPort();
144+
$data['Status'] = $old_status . '' . $new_status;
145+
146+
foreach ($data as $key => $value) {
147+
$field = new DiscordEmbedField($key, $value, true);
148+
$embed->addField($field);
149+
}
150+
151+
$webhook->addEmbed($embed);
152+
$webhook->send();
153+
}
86154
}

src/libraries/Server.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace BNETDocs\Libraries;
44

5+
use \BNETDocs\Libraries\Exceptions\QueryException;
6+
use \BNETDocs\Libraries\Exceptions\ServerNotFoundException;
7+
use \BNETDocs\Libraries\ServerType;
8+
59
use \CarlBennett\MVC\Libraries\Common;
610
use \CarlBennett\MVC\Libraries\Database;
711
use \CarlBennett\MVC\Libraries\DatabaseDriver;
8-
use \BNETDocs\Libraries\Exceptions\QueryException;
9-
use \BNETDocs\Libraries\Exceptions\ServerNotFoundException;
12+
1013
use \DateTime;
1114
use \DateTimeZone;
1215
use \InvalidArgumentException;
@@ -192,6 +195,10 @@ public function getStatusBitmask() {
192195
return $this->status_bitmask;
193196
}
194197

198+
public function getType() {
199+
return new ServerType($this->type_id);
200+
}
201+
195202
public function getTypeId() {
196203
return $this->type_id;
197204
}
@@ -225,6 +232,18 @@ public function getUserId() {
225232
return $this->user_id;
226233
}
227234

235+
public function isDisabled() {
236+
return ($this->status_bitmask & self::STATUS_DISABLED);
237+
}
238+
239+
public function isOffline() {
240+
return (!$this->isOnline());
241+
}
242+
243+
public function isOnline() {
244+
return ($this->status_bitmask & self::STATUS_ONLINE);
245+
}
246+
228247
public function jsonSerialize() {
229248
$created_datetime = $this->getCreatedDateTime();
230249
if (!is_null($created_datetime)) $created_datetime = [

0 commit comments

Comments
 (0)