99use Ourted \Interfaces \User ;
1010use Ourted \Model \Message \Embed ;
1111use Ourted \Utils \API ;
12+ use Ourted \Utils \Settings ;
1213use Ratchet \Client \Connector ;
1314use Ratchet \Client \WebSocket ;
1415use Ratchet \RFC6455 \Messaging \MessageInterface ;
1516use React \EventLoop \Factory ;
16- use Ourted \Utils \Settings ;
1717
1818class 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 /**
0 commit comments