! Published.
This commit is contained in:
32
src/Bootstrap/Commands.php
Normal file
32
src/Bootstrap/Commands.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Core\Commands\Command;
|
||||
use Core\Commands\CommandQueue;
|
||||
use Core\Commands\QueuedCommand;
|
||||
use Core\Disabled;
|
||||
|
||||
use function Core\debug;
|
||||
use function Core\discord;
|
||||
use function Core\doesClassHaveAttribute;
|
||||
use function Core\error;
|
||||
use function Core\loopClasses;
|
||||
|
||||
$commandQueue = new CommandQueue();
|
||||
$discord = discord();
|
||||
loopClasses(BOT_ROOT . '/Commands', static function (string $className) use ($commandQueue) {
|
||||
debug('Loading Command: ' . $className);
|
||||
|
||||
$attribute = doesClassHaveAttribute($className, Command::class);
|
||||
$disabled = doesClassHaveAttribute($className, Disabled::class);
|
||||
|
||||
if (!$attribute || $disabled !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$commandQueue->appendCommand(new QueuedCommand(
|
||||
$attribute->newInstance(),
|
||||
new $className()
|
||||
));
|
||||
});
|
||||
|
||||
$commandQueue->runQueue(registerCommands: Config::AUTO_REGISTER_COMMANDS)->otherwise(static fn (Throwable $e) => error($e->getMessage()));
|
||||
7
src/Bootstrap/Config.php
Normal file
7
src/Bootstrap/Config.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
class Config
|
||||
{
|
||||
public const AUTO_REGISTER_COMMANDS = true;
|
||||
public const AUTO_DELETE_COMMANDS = true;
|
||||
}
|
||||
27
src/Bootstrap/Discord.php
Normal file
27
src/Bootstrap/Discord.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Core\Env;
|
||||
use Discord\Discord;
|
||||
use Discord\WebSockets\Intents;
|
||||
use Bot\DiscordBot;
|
||||
use Services\ReminderService;
|
||||
|
||||
|
||||
use function Core\debug;
|
||||
use function Core\discord as d;
|
||||
|
||||
Env::get()->remainderService = new ReminderService();
|
||||
|
||||
Env::get()->bot = DiscordBot::getInstance();
|
||||
|
||||
Env::get()->discord = new Discord([
|
||||
'token' => Env::get()->TOKEN,
|
||||
'intents' => Intents::getAllIntents(),
|
||||
]);
|
||||
|
||||
require_once BOT_ROOT . '/Bootstrap/Events.php';
|
||||
|
||||
d()->on('init', static function (Discord $discord) {
|
||||
debug('Bootstrapping Commands...');
|
||||
require_once BOT_ROOT . '/Bootstrap/Commands.php';
|
||||
});
|
||||
15
src/Bootstrap/Environment.php
Normal file
15
src/Bootstrap/Environment.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use Core\Env;
|
||||
|
||||
//NOTE: remove comment lines from the .env file
|
||||
$rawEnvFileContents = file_get_contents(BOT_ROOT . '/.env');
|
||||
$filteredEnvFileContents = preg_replace('/^#.*$/m', '', $rawEnvFileContents);
|
||||
|
||||
$env = Env::createFromString($filteredEnvFileContents);
|
||||
|
||||
|
||||
|
||||
if (!isset($env->TOKEN)) {
|
||||
throw new RuntimeException('No token supplied to environment!');
|
||||
}
|
||||
44
src/Bootstrap/Events.php
Normal file
44
src/Bootstrap/Events.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use Core\Disabled;
|
||||
use Core\Events\Event;
|
||||
|
||||
use function Core\discord;
|
||||
use function Core\doesClassHaveAttribute;
|
||||
use function Core\loopClasses;
|
||||
|
||||
$events = [];
|
||||
$discord = discord();
|
||||
|
||||
loopClasses(BOT_ROOT . '/Core/Events', static function (string $className) use (&$events) {
|
||||
if (!interface_exists($className) || $className === Event::class) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attribute = doesClassHaveAttribute($className, Event::class);
|
||||
|
||||
if (!$attribute) {
|
||||
return;
|
||||
}
|
||||
|
||||
$events[$className] = $attribute->newInstance()->name;
|
||||
});
|
||||
|
||||
loopClasses(BOT_ROOT . '/Events', static function (string $className) use ($events, $discord) {
|
||||
if (doesClassHaveAttribute($className, Disabled::class) !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = new $className();
|
||||
$reflection = new ReflectionClass($event);
|
||||
|
||||
foreach ($reflection->getInterfaceNames() as $interface) {
|
||||
$eventName = $events['\\' . $interface] ?? null;
|
||||
|
||||
if ($eventName === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$discord->on($eventName, $event->handle(...));
|
||||
}
|
||||
});
|
||||
7
src/Bootstrap/Requires.php
Normal file
7
src/Bootstrap/Requires.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once BOT_ROOT . '/vendor/autoload.php';
|
||||
require_once BOT_ROOT . '/Bootstrap/Config.php';
|
||||
require_once BOT_ROOT . '/Bootstrap/Environment.php';
|
||||
require_once BOT_ROOT . '/Services/ReminderService.php';
|
||||
require_once BOT_ROOT . '/Bootstrap/Discord.php';
|
||||
Reference in New Issue
Block a user