Skip to content

Commit

Permalink
Support for passing bot name and token
Browse files Browse the repository at this point in the history
  • Loading branch information
marky291 authored Nov 5, 2024
1 parent a394c27 commit 624c999
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Console/Commands/BootCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laracord\Console\Commands;

use Illuminate\Support\Str;
use Illuminate\Console\Command;

class BootCommand extends Command
{
Expand All @@ -12,7 +13,9 @@ class BootCommand extends Command
* @var string
*/
protected $signature = 'bot:boot
{--no-migrate : Boot without running database migrations}';
{--no-migrate : Boot without running database migrations}
{--bot_name= : The name of the bot}
{--bot_token= : The token of the bot}';

/**
* The description of the command.
Expand All @@ -28,12 +31,31 @@ class BootCommand extends Command
*/
public function handle()
{
// Run migrations unless --no-migrate is specified
if (! $this->option('no-migrate')) {
$this->callSilent('migrate', ['--force' => true]);
}

$this->app->singleton('bot', fn () => $this->getClass()::make($this));
// Retrieve bot name and token from options or environment
$botName = $this->option('bot_name') ?? config('discord.description');
$botToken = $this->option('bot_token') ?? config('discord.token');

// Check if bot token is provided
if (empty($botToken)) {
$this->error('Bot token is required.');
return;
}

// Set the bot name and token in the environment dynamically
config(['discord.description' => $botName, 'discord.token' => $botToken]);

// Create the bot instance using singleton with the bot name and token
$this->app->singleton('bot', function () {
$botClass = $this->getClass();
return $botClass::make($this, config('discord.description'), config('discord.token'));
});

// Boot the bot
$this->app->make('bot')->boot();
}

Expand All @@ -42,7 +64,7 @@ public function handle()
*/
protected function getClass(): string
{
$class = Str::start($this->app->getNamespace(), '\\').'Bot';
$class = Str::start($this->app->getNamespace(), '\\') . 'Bot';

return class_exists($class) ? $class : 'Laracord';
}
Expand Down

0 comments on commit 624c999

Please sign in to comment.