9
9
use Ourted \Interfaces \User ;
10
10
use Ourted \Model \Message \Embed ;
11
11
use Ourted \Utils \API ;
12
+ use Ourted \Utils \Settings ;
12
13
use Ratchet \Client \Connector ;
13
14
use Ratchet \Client \WebSocket ;
14
15
use Ratchet \RFC6455 \Messaging \MessageInterface ;
15
16
use React \EventLoop \Factory ;
16
- use Ourted \Utils \Settings ;
17
17
18
18
class Bot
19
19
{
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
-
26
20
/**
27
21
* State
28
22
* @var State
29
23
*/
30
24
public $ state ;
31
-
32
- /**
33
- * Current Connection
34
- * @var WebSocket Instance
35
- */
36
- protected $ connection ;
37
-
38
25
/**
39
26
* Functions
40
27
* @var Settings Instance
41
28
*/
42
29
public $ settings ;
43
-
44
30
/**
45
31
* Current bot token
46
32
* @var mixed
47
33
*/
48
34
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
+
49
63
64
+ /* Classes */
50
65
/**
51
66
* Interval
52
67
* @var [type]
53
68
*/
54
69
protected $ interval = [];
55
-
56
70
/**
57
71
* Current set of dispatch handlers
58
72
* @var [type]
59
73
*/
60
74
protected $ dispatch = [];
61
-
62
75
/**
63
76
* Commands
64
77
* @var [type]
65
78
*/
66
79
protected $ commands = [];
67
-
68
80
/**
69
81
* Listeners
70
82
*/
71
83
protected $ listeners = [];
72
84
73
- public $ loop ;
85
+ /* Finish Classes */
74
86
75
87
/**
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]
77
93
*/
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
+ }
79
111
80
112
/**
81
- * @var bool
113
+ * Init the bot and set up the loop/actions for the WebSocket
82
114
*/
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 );
84
122
85
123
86
- /* Classes */
87
124
88
- /** @var API */
89
- public $ api ;
90
125
91
- /** @var Channel */
92
- public $ channel ;
93
126
94
- /** @var Guild */
95
- public $ guild ;
96
127
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
+ });
99
132
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
+ }
101
163
102
164
/**
103
165
* Add a new dispatch handler
@@ -134,33 +196,9 @@ public function addCommand($command_name, $function)
134
196
* @param \Ourted\Model\Channel\Channel $channel
135
197
* @param string $description
136
198
*/
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 = "" )
150
200
{
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 );
164
202
}
165
203
166
204
/**
@@ -176,42 +214,10 @@ public function addListeners(...$listener)
176
214
}
177
215
}
178
216
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
-
207
217
public function run ()
208
218
{
209
- $ this ->loop ->run ();
210
- }
211
219
212
- public function stop ()
213
- {
214
- $ this ->loop ->stop ();
220
+ $ this ->loop ->run ();
215
221
}
216
222
217
223
/**
0 commit comments