22
33namespace Laracord \Services ;
44
5- use Discord \DiscordCommandClient as Discord ;
6- use Laracord \Console \Commands \BootCommand as Console ;
7- use Laracord \Laracord ;
5+ use Laracord \Concerns \HasHandler ;
6+ use Laracord \HasLaracord ;
87use Laracord \Services \Contracts \Service as ServiceContract ;
98use Laracord \Services \Exceptions \InvalidServiceInterval ;
109
1110abstract class Service implements ServiceContract
1211{
13- /**
14- * The bot instance.
15- *
16- * @var \Laracord\Laracord
17- */
18- protected $ bot ;
19-
20- /**
21- * The console instance.
22- *
23- * @var \Laracord\Console\Commands\BootCommand
24- */
25- protected $ console ;
26-
27- /**
28- * The Discord instance.
29- *
30- * @var \Discord\DiscordCommandClient;
31- */
32- protected $ discord ;
12+ use HasHandler, HasLaracord;
3313
3414 /**
3515 * The service name.
@@ -42,39 +22,23 @@ abstract class Service implements ServiceContract
4222 protected int $ interval = 5 ;
4323
4424 /**
45- * Determine if the service is enabled.
46- *
47- * @var bool
25+ * Determine if the service handler should execute during boot.
4826 */
49- protected $ enabled = true ;
27+ protected bool $ eager = false ;
5028
5129 /**
52- * Create a new service instance.
53- *
54- * @return void
30+ * Determine if the service is enabled.
5531 */
56- public function __construct (Laracord $ bot )
57- {
58- $ this ->bot = $ bot ;
59- $ this ->console = $ bot ->console ();
60- $ this ->discord = $ bot ->discord ();
61- }
32+ protected bool $ enabled = true ;
6233
6334 /**
6435 * Make a new service instance.
6536 */
66- public static function make (Laracord $ bot ): self
37+ public static function make (): self
6738 {
68- return new static ( $ bot ) ;
39+ return new static ;
6940 }
7041
71- /**
72- * Handle the service.
73- *
74- * @return mixed
75- */
76- abstract public function handle ();
77-
7842 /**
7943 * Boot the service.
8044 */
@@ -84,9 +48,13 @@ public function boot(): self
8448 throw new InvalidServiceInterval ($ this ->getName ());
8549 }
8650
51+ if ($ this ->eager ) {
52+ $ this ->resolveHandler ();
53+ }
54+
8755 $ this ->bot ->getLoop ()->addPeriodicTimer (
8856 $ this ->getInterval (),
89- fn () => $ this ->bot -> handleSafe ( $ this -> getName (), fn () => $ this -> handle () )
57+ fn () => $ this ->resolveHandler ( )
9058 );
9159
9260 return $ this ;
@@ -123,7 +91,7 @@ public function interval(int $interval): self
12391 */
12492 public function getName (): string
12593 {
126- if ($ this ->name ) {
94+ if (filled ( $ this ->name ) ) {
12795 return $ this ->name ;
12896 }
12997
@@ -137,39 +105,4 @@ public function isEnabled(): bool
137105 {
138106 return $ this ->enabled ;
139107 }
140-
141- /**
142- * Get the Discord client.
143- */
144- public function discord (): Discord
145- {
146- return $ this ->discord ;
147- }
148-
149- /**
150- * Get the bot instance.
151- */
152- public function bot (): Laracord
153- {
154- return $ this ->bot ;
155- }
156-
157- /**
158- * Get the console instance.
159- */
160- public function console (): Console
161- {
162- return $ this ->console ;
163- }
164-
165- /**
166- * Build an embed for use in a Discord message.
167- *
168- * @param string $content
169- * @return \Laracord\Discord\Message
170- */
171- public function message ($ content = '' )
172- {
173- return $ this ->bot ()->message ($ content );
174- }
175108}
0 commit comments