99use Ourted \Interfaces \Invite ;
1010use Ourted \Interfaces \Member ;
1111use Ourted \Interfaces \User ;
12- use Ourted \Model \Message \Embed ;
12+ use Ourted \Interfaces \Webhook ;
13+ use Ourted \Model \Channel \Embed ;
1314use Ourted \Utils \API ;
1415use Ourted \Utils \Settings ;
1516use Ratchet \Client \Connector ;
2021class Bot
2122{
2223
24+
25+ public $ CREATE_INSTANT_INVITE = 0x00000001 ;
26+ public $ KICK_MEMBERS = 0x00000002 ;
27+ public $ BAN_MEMBERS = 0x00000004 ;
28+ public $ ADMINISTRATOR = 0x00000008 ;
29+ public $ MANAGE_CHANNELS = 0x00000010 ;
30+ public $ MANAGE_GUILD = 0x00000020 ;
31+ public $ ADD_REACTIONS = 0x00000040 ;
32+ public $ VIEW_AUDIT_LOG = 0x00000080 ;
33+ public $ PRIORITY_SPEAKER = 0x00000100 ;
34+ public $ STREAM = 0x00000200 ;
35+ public $ VIEW_CHANNEL = 0x00000400 ;
36+ public $ SEND_MESSAGES = 0x00000800 ;
37+ public $ SEND_TTS_MESSAGES = 0x00001000 ;
38+ public $ MANAGE_MESSAGES = 0x00002000 ;
39+ public $ EMBED_LINKS = 0x00004000 ;
40+ public $ ATTACH_FILES = 0x00008000 ;
41+ public $ READ_MESSAGE_HISTORY = 0x00010000 ;
42+ public $ MENTION_EVERYONE = 0x00020000 ;
43+ public $ USE_EXTERNAL_EMOJIS = 0x00040000 ;
44+ public $ VIEW_GUILD_INSIGHTS = 0x00080000 ;
45+ public $ CONNECT = 0x00100000 ;
46+ public $ SPEAK = 0x00200000 ;
47+ public $ MUTE_MEMBERS = 0x00400000 ;
48+ public $ DEAFEN_MEMBERS = 0x00800000 ;
49+ public $ MOVE_MEMBERS = 0x01000000 ;
50+ public $ USE_VAD = 0x02000000 ;
51+ public $ CHANGE_NICKNAME = 0x04000000 ;
52+ public $ MANAGE_NICKNAMES = 0x08000000 ;
53+ public $ MANAGE_ROLES = 0x10000000 ;
54+ public $ MANAGE_WEBHOOKS = 0x20000000 ;
55+ public $ MANAGE_EMOJIS = 0x40000000 ;
56+
57+ public $ GUILD_TEXT = 0 ;
58+ public $ DM = 1 ;
59+ public $ GUILD_VOICE = 2 ;
60+ public $ GROUP_DM = 3 ;
61+ public $ GUILD_NEWS = 4 ;
62+ public $ GUILD_STORE = 5 ;
63+
64+ public $ GAME_LISTEN = 2 ;
65+ public $ GAME_WATCHING = 1 ;
66+ public $ GAME_PLAYING = 0 ;
67+
68+
2369 /**
2470 * State
2571 * @var State
@@ -56,6 +102,12 @@ class Bot
56102 public $ member ;
57103 /** @var Invite */
58104 public $ invite ;
105+ /** @var Webhook */
106+ public $ webhook ;
107+ /**
108+ * @var mixed
109+ */
110+ public $ session_id ;
59111 /**
60112 * Default WSS URL (from the Discord API docs)
61113 * @var string
@@ -66,7 +118,6 @@ class Bot
66118 * @var WebSocket Instance
67119 */
68120 protected $ connection ;
69-
70121 /**
71122 * @var bool
72123 */
@@ -112,6 +163,7 @@ public function __construct($botToken, $botPrefix, $wssUrl = null)
112163 $ this ->prefix = $ botPrefix ;
113164 $ this ->token = $ botToken ;
114165 $ this ->settings = new Settings ($ this );
166+ $ this ->webhook = new Webhook ($ this );
115167 $ this ->channel = new Channel ($ this );
116168 $ this ->member = new Member ($ this );
117169 $ this ->invite = new Invite ($ this );
@@ -138,17 +190,26 @@ public function init()
138190
139191 $ conn ->on ('message ' , function (MessageInterface $ msg ) use ($ conn , $ state ) {
140192 $ json = json_decode ($ msg );
193+ if (isset ($ json ->d ->session_id )) {
194+ $ this ->session_id = $ json ->d ->session_id ;
195+ }
141196 $ state ->action ($ json , $ this ->loop );
142197 });
143198
144199
145- $ conn ->on ('close ' , function ($ code = null , $ reason = null ) {
200+ $ conn ->on ('close ' , function ($ code = null , $ reason = null ) use ( $ conn ) {
146201 echo "\nConnection closed ( {$ code } - {$ reason }) \n" ;
147- if (!$ this ->reconnect ) {
202+ if (!$ this ->reconnect ) {
148203 die ();
149- }else {
150- $ this ->stop ();
151- $ this ->run ();
204+ } else {
205+ $ conn ->send (json_encode ([
206+ "op " => 6 ,
207+ "d " => [
208+ "token " => $ this ->getBot ()->token ,
209+ "session_id " => $ this ->getBot ()->session_id ,
210+ "seq " => 1337
211+ ]
212+ ]));
152213 }
153214 });
154215
@@ -161,16 +222,27 @@ public function init()
161222 return null ;
162223 }
163224
225+ public function getBot ()
226+ {
227+ return $ this ;
228+ }
229+
164230 public function stop ()
165231 {
166232 $ this ->loop ->stop ();
167233 }
168234
235+ public function run ()
236+ {
237+
238+ $ this ->loop ->run ();
239+ echo "Connection Started! " ;
240+ }
241+
169242 /**
170243 * Add a new dispatch handler
171244 *
172245 * @param string $type Dispatch type
173- * @param string $callback_name Dispatch name
174246 * @param string|Callable $callback Callback to execute when dispatching action
175247 */
176248 public function addDispatch ($ type , $ callback )
@@ -196,10 +268,17 @@ public function addCommand($command_name, $function)
196268 $ function ($ this , $ command_name );
197269 }
198270
271+ public function getImageData ($ image_path ){
272+ if (!file_exists ($ image_path ) || str_ends_with ($ image_path , ("png " || "jpg " || "jpeg " | "PNG " || "JPG " || "JPEG " ))) return "Fail " ;
273+ $ imageData = base64_encode (file_get_contents ($ image_path ));
274+ return 'data: ' .mime_content_type ($ image_path ).';base64, ' .$ imageData ;
275+ }
276+
199277 /**
200278 * @param $title
201- * @param \Ourted\ Model\Channel\Channel $channel
279+ * @param Model\Channel\Channel $channel
202280 * @param string $description
281+ * @return Embed
203282 */
204283 public function createEmbed ($ title , $ channel , $ description = "" )
205284 {
@@ -219,13 +298,6 @@ public function addListeners(...$listener)
219298 }
220299 }
221300
222- public function run ()
223- {
224-
225- $ this ->loop ->run ();
226- echo "Connection Started! " ;
227- }
228-
229301 /**
230302 * Get Connection
231303 *
@@ -241,10 +313,5 @@ public function getToken()
241313 return $ this ->token ;
242314 }
243315
244- public function getBot ()
245- {
246- return $ this ;
247- }
248-
249316
250317}
0 commit comments