Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Push — main ( 30fe2e...c58695 )
by Dan
04:45
created

src/lib/Smr/Discord/Command.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Smr\Discord;
4
5
use Discord\CommandClient\Command as DiscordCommand;
6
use Discord\DiscordCommandClient;
7
use Discord\Parts\Channel\Message;
8
use Smr\Exceptions\UserError;
9
use Throwable;
10
11
abstract class Command {
12
13
	protected Message $message;
14
15
	/**
16
	 * Name to invoke this command on Discord.
17
	 */
18
	abstract public function name(): string;
19
20
	/**
21
	 * Help-text description of what the command does.
22
	 */
23
	abstract public function description(): string;
24
25
	/**
26
	 * Help-text options that can be passed to this command, if any.
27
	 */
28
	public function usage(): ?string {
29
		return null;
30
	}
31
32
	/**
33
	 * Constructs a textual response to a Command invocation.
34
	 *
35
	 * @return array<string>
36
	 */
37
	abstract public function response(string ...$args): array;
38
39
	protected function logException(Throwable $err): void {
40
		// Isolate this global function call so it can be mocked during testing.
41
		// A better solution is probably to switch to Monolog for logging.
42
		logException($err);
43
	}
44
45
	/**
46
	 * Wrapper to properly handle a Command response.
47
	 *
48
	 * @param array<string> $args Arguments passed to the command.
49
	 */
50
	final public function callback(Message $message, array $args): void {
51
		$this->message = $message;
52
		try {
53
			$lines = $this->response(...$args);
54
		} catch (UserError $err) {
55
			$lines = [$err->getMessage()];
56
		} catch (Throwable $err) {
57
			$this->logException($err);
58
			$lines = ['I encountered an error. Please report this to an admin!'];
59
		}
60
		if ($lines) {
61
			$message->reply(implode(EOL, $lines))->done(null, 'logException');
62
		}
63
	}
64
65
	/**
66
	 * Register a Command class as a command (by passing in the main Discord
67
	 * client) or a subcommand (by passing in the output of this method from
68
	 * its parent command).
69
	 */
70
	public function register(DiscordCommand|DiscordCommandClient $parent): DiscordCommand {
71
		// Check if we are registering a command or a subcommand
72
		if ($parent instanceof DiscordCommandClient) {
73
			$registrar = $parent->registerCommand(...);
0 ignored issues
show
A parse error occurred: Syntax error, unexpected ')' on line 73 at column 44
Loading history...
74
		} else {
75
			$registrar = $parent->registerSubCommand(...);
76
		}
77
78
		// Construct a usage string from the command name and any options
79
		$usage = DISCORD_COMMAND_PREFIX . $this->name();
80
		if ($this->usage() !== null) {
81
			$usage .= ' ' . $this->usage();
82
		}
83
84
		return $registrar(
85
			$this->name(),
86
			$this->callback(...),
87
			[
88
				'description' => $this->description(),
89
				'usage' => $usage,
90
			]
91
		);
92
	}
93
94
}
95