Skip to content

Commit 25b5dea

Browse files
committed
Ourted - 5.1.4 Beta Version
1 parent ced787a commit 25b5dea

File tree

10 files changed

+147
-119
lines changed

10 files changed

+147
-119
lines changed

.idea/php.xml

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Bot.php

+98-92
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,157 @@
99
use Ourted\Interfaces\User;
1010
use Ourted\Model\Message\Embed;
1111
use Ourted\Utils\API;
12+
use Ourted\Utils\Settings;
1213
use Ratchet\Client\Connector;
1314
use Ratchet\Client\WebSocket;
1415
use Ratchet\RFC6455\Messaging\MessageInterface;
1516
use React\EventLoop\Factory;
16-
use Ourted\Utils\Settings;
1717

1818
class Bot
1919
{
20-
/**
21-
* Default WSS URL (from the Discord API docs)
22-
* @var string
23-
*/
24-
protected $wssUrl = 'wss://gateway.discord.gg/?v=6&encoding=json';
25-
2620
/**
2721
* State
2822
* @var State
2923
*/
3024
public $state;
31-
32-
/**
33-
* Current Connection
34-
* @var WebSocket Instance
35-
*/
36-
protected $connection;
37-
3825
/**
3926
* Functions
4027
* @var Settings Instance
4128
*/
4229
public $settings;
43-
4430
/**
4531
* Current bot token
4632
* @var mixed
4733
*/
4834
public $token;
35+
public $loop;
36+
/**
37+
* @var string
38+
*/
39+
public $prefix;
40+
/**
41+
* @var bool
42+
*/
43+
public $send_log = false;
44+
/** @var API */
45+
public $api;
46+
/** @var Channel */
47+
public $channel;
48+
/** @var Guild */
49+
public $guild;
50+
/** @var User */
51+
public $user;
52+
/**
53+
* Default WSS URL (from the Discord API docs)
54+
* @var string
55+
*/
56+
protected $wssUrl = 'wss://gateway.discord.gg/?v=6&encoding=json';
57+
/**
58+
* Current Connection
59+
* @var WebSocket Instance
60+
*/
61+
protected $connection;
62+
4963

64+
/* Classes */
5065
/**
5166
* Interval
5267
* @var [type]
5368
*/
5469
protected $interval = [];
55-
5670
/**
5771
* Current set of dispatch handlers
5872
* @var [type]
5973
*/
6074
protected $dispatch = [];
61-
6275
/**
6376
* Commands
6477
* @var [type]
6578
*/
6679
protected $commands = [];
67-
6880
/**
6981
* Listeners
7082
*/
7183
protected $listeners = [];
7284

73-
public $loop;
85+
/* Finish Classes */
7486

7587
/**
76-
* @var string
88+
* Set Bot
89+
*
90+
* @param string $botToken Current bot token
91+
* @param string $botPrefix Bot Prefix
92+
* @param string $wssUrl WSS URL [optional]
7793
*/
78-
public $prefix;
94+
95+
public function __construct($botToken, $botPrefix, $wssUrl = null)
96+
{
97+
if ($wssUrl !== null) {
98+
$this->wssUrl = $wssUrl;
99+
}
100+
$this->prefix = $botPrefix;
101+
$this->token = $botToken;
102+
$this->settings = new Settings($this);
103+
$this->channel = new Channel($this);
104+
$this->guild = new Guild($this);
105+
$this->user = new User($this);
106+
$this->api = new API($this);
107+
108+
$this->loop = Factory::create();
109+
$this->init();
110+
}
79111

80112
/**
81-
* @var bool
113+
* Init the bot and set up the loop/actions for the WebSocket
82114
*/
83-
public $send_log = false;
115+
public function init()
116+
{
117+
$connector = new Connector($this->loop, new \React\Socket\Connector($this->loop));
118+
$connector($this->wssUrl)->then(function (WebSocket $conn) {
119+
$this->connection = $conn;
120+
$this->state = $state = new State($conn, $this->loop, $this->token);
121+
$state->addDispatch($this->dispatch);
84122

85123

86-
/* Classes */
87124

88-
/** @var API */
89-
public $api;
90125

91-
/** @var Channel */
92-
public $channel;
93126

94-
/** @var Guild */
95-
public $guild;
96127

97-
/** @var User */
98-
public $user;
128+
$conn->on('message', function (MessageInterface $msg) use ($conn, $state) {
129+
$json = json_decode($msg);
130+
$state->action($json, $this->loop);
131+
});
99132

100-
/* Finish Classes */
133+
134+
135+
136+
$conn->on('close', function ($code = null, $reason = null) {
137+
echo "Connection closed ({$code} - {$reason})\n";
138+
die();
139+
});
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
}, function (Exception $e) {
152+
echo "Could not connect: {$e->getMessage()}\n";
153+
$this->stop();
154+
});
155+
156+
return null;
157+
}
158+
159+
public function stop()
160+
{
161+
$this->loop->stop();
162+
}
101163

102164
/**
103165
* Add a new dispatch handler
@@ -134,33 +196,9 @@ public function addCommand($command_name, $function)
134196
* @param \Ourted\Model\Channel\Channel $channel
135197
* @param string $description
136198
*/
137-
public function createEmbed($title, $channel, $description = ""){
138-
return new Embed($title, $this, $channel, $description);
139-
}
140-
141-
/**
142-
* Set Bot
143-
*
144-
* @param string $botToken Current bot token
145-
* @param string $botPrefix Bot Prefix
146-
* @param string $wssUrl WSS URL [optional]
147-
*/
148-
149-
public function __construct($botToken, $botPrefix, $wssUrl = null)
199+
public function createEmbed($title, $channel, $description = "")
150200
{
151-
if ($wssUrl !== null) {
152-
$this->wssUrl = $wssUrl;
153-
}
154-
$this->prefix = $botPrefix;
155-
$this->token = $botToken;
156-
$this->settings = new Settings($this);
157-
$this->channel = new Channel($this);
158-
$this->guild = new Guild($this);
159-
$this->user = new User($this);
160-
$this->api = new API($this);
161-
162-
$this->loop = Factory::create();
163-
$this->init();
201+
return new Embed($title, $this, $channel, $description);
164202
}
165203

166204
/**
@@ -176,42 +214,10 @@ public function addListeners(...$listener)
176214
}
177215
}
178216

179-
180-
/**
181-
* Init the bot and set up the loop/actions for the WebSocket
182-
*/
183-
public function init()
184-
{
185-
$reactConnector = new \React\Socket\Connector($this->loop);
186-
$connector = new Connector($this->loop, $reactConnector);
187-
$connector($this->wssUrl)->then(function (WebSocket $conn) {
188-
$this->connection = $conn;
189-
$this->state = $state = new State($conn, $this->loop, $this->token);
190-
$state->addDispatch($this->dispatch);
191-
$conn->on('message', function (MessageInterface $msg) use ($conn, $state) {
192-
$json = json_decode($msg);
193-
$state->action($json, $this->loop);
194-
});
195-
196-
$conn->on('close', function ($code = null, $reason = null) {
197-
echo "Connection closed ({$code} - {$reason})\n";
198-
die();
199-
});
200-
}, function (Exception $e) {
201-
echo "Could not connect: {$e->getMessage()}\n";
202-
$this->stop();
203-
});
204-
return null;
205-
}
206-
207217
public function run()
208218
{
209-
$this->loop->run();
210-
}
211219

212-
public function stop()
213-
{
214-
$this->loop->stop();
220+
$this->loop->run();
215221
}
216222

217223
/**

src/Model/Channel/Channel.php

+4
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ public function __construct($bot, $channel_id)
4646
return $this;
4747
}
4848

49+
public function isNull(){
50+
return $this->id == null;
51+
}
52+
4953
}

src/Model/Message/Message.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use Ourted\Bot;
66
use Ourted\Model\Channel\Channel;
7+
use stdClass;
78

8-
class Message extends \stdClass
9+
class Message extends stdClass
910
{
1011
public $id;
1112
public $reactions = [];
@@ -57,4 +58,8 @@ public function __construct($bot, $message_id, $channel)
5758
$this->guild_id = $result->guild_id ?? null;
5859
return $this;
5960
}
61+
62+
public function isNull(){
63+
return $this->id == null;
64+
}
6065
}

src/Model/Role/Role.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Ourted\Model\Role;
44

55
use Ourted\Bot;
6-
use Ourted\Interfaces\Guild;
76

87
class Role
98
{
@@ -44,4 +43,8 @@ public function __construct($bot, $guild, $role_id = 000)
4443
$this->mentionable = $json->mentionable ?? null;
4544
return $this;
4645
}
46+
47+
public function isNull(){
48+
return $this->id == null;
49+
}
4750
}

src/Model/User/User.php

+4
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public function __construct($bot, $user_id)
3737
return $this;
3838
}
3939

40+
public function isNull(){
41+
return $this->id == null;
42+
}
43+
4044
}

src/Ops/Heartbeatack.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Ourted\Ops;
44

5-
class Heartbeatack extends \Ourted\Model\Op\Op
5+
use Ourted\Model\Op\Op;
6+
use Ourted\State;
7+
8+
class Heartbeatack extends Op
69
{
710
public function execute($json)
811
{
912
$this->bot->send_log ?
10-
\Ourted\State::log('Execute: HEARTBEAT-ACK') : null;
11-
// Nothing to see...
13+
State::log('Execute: HEARTBEAT-ACK') : null;
1214
}
1315
}

0 commit comments

Comments
 (0)