Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Create a HasLaracord trait for accessing the Bot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Feb 8, 2024
1 parent f1e4d27 commit f260e1f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
55 changes: 55 additions & 0 deletions src/HasLaracord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Laracord;

use Discord\DiscordCommandClient as Discord;
use Laracord\Console\Commands\Command as ConsoleCommand;
use Laracord\Discord\Message;

trait HasLaracord
{
/**
* The bot instance.
*/
protected ?Laracord $bot;

/**
* Retrieve the bot instance.
*/
public function bot(): Laracord
{
if ($this->bot) {
return $this->bot;
}

return $this->bot = app('bot');
}

/**
* Retrieve the Discord instance.
*/
public function discord(): Discord
{
return $this->bot()->discord();
}

/**
* Retrieve the console instance.
*/
public function console(): ConsoleCommand
{
return $this->bot()->console();
}

/**
* Build an embed for use in a Discord message.
*
* @param string $content
* @return \Laracord\Discord\Message
*/
public function message($content = '')
{
return Message::make($this)
->content($content);
}
}
37 changes: 2 additions & 35 deletions src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,10 @@

namespace Laracord\Http\Controllers;

use Discord\DiscordCommandClient as Discord;
use Illuminate\Routing\Controller as BaseController;
use Laracord\Console\Commands\Command as ConsoleCommand;
use Laracord\Laracord;
use Laracord\HasLaracord;

class Controller extends BaseController
{
/**
* The bot instance.
*/
protected ?Laracord $bot;

/**
* Retrieve the bot instance.
*/
public function bot(): Laracord
{
if ($this->bot) {
return $this->bot;
}

return $this->bot = app('bot');
}

/**
* Retrieve the Discord instance.
*/
public function discord(): Discord
{
return $this->bot()->discord();
}

/**
* Retrieve the console instance.
*/
public function console(): ConsoleCommand
{
return $this->bot()->console();
}
use HasLaracord;
}

0 comments on commit f260e1f

Please sign in to comment.