PortlandLabs /
slackbot
| 1 | <?php |
||
| 2 | namespace PortlandLabs\Slackbot\Command; |
||
| 3 | |||
| 4 | use League\CLImate\Argument\Filter; |
||
| 5 | use League\CLImate\Argument\Summary; |
||
| 6 | use PortlandLabs\Slackbot\Bot; |
||
| 7 | use PortlandLabs\Slackbot\Command\Argument\Manager as ArgumentManager; |
||
| 8 | use PortlandLabs\Slackbot\Permission\Checker; |
||
| 9 | use PortlandLabs\Slackbot\Slack\Rtm\Event\Message; |
||
| 10 | use Psr\Container\ContainerInterface; |
||
| 11 | |||
| 12 | class HelpCommand extends SimpleCommand |
||
| 13 | { |
||
| 14 | |||
| 15 | protected $description = 'Get help'; |
||
| 16 | |||
| 17 | protected $signature = 'help {command? : The command to get help with}'; |
||
| 18 | |||
| 19 | protected $container; |
||
| 20 | |||
| 21 | public function __construct(Bot $bot, ArgumentManager $argumentManager, Checker $checker, ContainerInterface $container) |
||
| 22 | { |
||
| 23 | $this->container = $container; |
||
| 24 | parent::__construct($bot, $argumentManager, $checker); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Handle a message |
||
| 29 | * |
||
| 30 | * @param Message $message |
||
| 31 | * @param Manager $manager |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function run(Message $message, ArgumentManager $manager) |
||
| 36 | { |
||
| 37 | $output = []; |
||
| 38 | |||
| 39 | $filter = new Filter(); |
||
| 40 | $summary = new Summary(); |
||
| 41 | |||
| 42 | $userRole = $this->checker->getRole($message); |
||
| 43 | |||
| 44 | $commandManager = $this->bot->commands(); |
||
| 45 | $commandName = $manager->get('command'); |
||
| 46 | |||
| 47 | $output[] = 'You can use the following commands:'; |
||
| 48 | |||
| 49 | foreach ($commandManager->all() as $command) { |
||
| 50 | if (!$command instanceof ConsoleStyleCommand) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | $arguments = new ArgumentManager(); |
||
| 55 | $arguments = $command->configure($arguments); |
||
| 56 | $commandRole = $arguments->getRole(); |
||
| 57 | |||
| 58 | if (!$userRole instanceof $commandRole) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($commandName && $arguments->getCommand() === $commandName) { |
||
| 63 | $this->outputUsage($message, $arguments, $command); |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | $filter->setArguments($arguments->all()); |
||
| 68 | |||
| 69 | // Output the simple usage statement |
||
| 70 | $orderedArguments = array_merge($filter->withPrefix(), $filter->withoutPrefix()); |
||
| 71 | $short = $summary->short($orderedArguments); |
||
| 72 | $output[] = "• `{$arguments->getCommand()}" . ($short ? " {$short}`" : '`') . ' ' . $command->getDescription(); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->bot->feignTyping($message->getChannel(), implode("\n", $output)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 76 | } |
||
| 77 | } |