|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace PortlandLabs\Slackbot\Command; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use PortlandLabs\Slackbot\Bot; |
|
9
|
|
|
use PortlandLabs\Slackbot\Command\Argument\Manager; |
|
|
|
|
|
|
10
|
|
|
use PortlandLabs\Slackbot\Permission\Admin; |
|
11
|
|
|
use PortlandLabs\Slackbot\Permission\Checker; |
|
12
|
|
|
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message; |
|
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
14
|
|
|
use Psr\SimpleCache\CacheException; |
|
15
|
|
|
|
|
16
|
|
|
class RememberCommand extends SimpleCommand |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $description = 'Remember something interesting to be `recall`ed later'; |
|
20
|
|
|
|
|
21
|
|
|
protected $signature = 'remember {thing} {value}'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var CacheInterface */ |
|
24
|
|
|
protected $cache; |
|
25
|
|
|
|
|
26
|
|
|
protected $role = Admin::class; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(Bot $bot, Manager $argumentManager, Checker $checker, CacheInterface $cache) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($bot, $argumentManager, $checker); |
|
31
|
|
|
|
|
32
|
|
|
$this->cache = $cache; |
|
33
|
|
|
$this->checker = $checker; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Message $message |
|
38
|
|
|
* @param Manager $manager |
|
39
|
|
|
*/ |
|
40
|
|
|
public function run(Message $message, Manager $manager) |
|
41
|
|
|
{ |
|
42
|
|
|
$thing = $manager->get('thing'); |
|
43
|
|
|
$text = $message->getText(); |
|
44
|
|
|
$text = substr($text, strpos($text, '>')); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$value = trim(substr($text, strpos($text, $thing) + strlen($thing))); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
// trim off quotes |
|
49
|
|
|
$value = ltrim($value, '"\''); |
|
50
|
|
|
|
|
51
|
|
|
$key = Str::snake('remember ' . $thing); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$this->cache->set($key, $value); |
|
55
|
|
|
$keys = $this->cache->get('rememberindex', []); |
|
56
|
|
|
$keys[$key] = $thing; |
|
57
|
|
|
$this->cache->set('rememberindex', $keys); |
|
58
|
|
|
} catch (CacheException $e) { |
|
59
|
|
|
$this->bot->logger()->error('[CMD.RMB] Failed to remember: ' . $e->getMessage()); |
|
|
|
|
|
|
60
|
|
|
$this->bot->feignTyping($message->getChannel(), 'Hmm.. I wasn\'t able to remember that... What was it again?'); |
|
|
|
|
|
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->bot->feignTyping($message->getChannel(), 'Okay, I\'ve got that remembered! Use the `recall` command to recall.'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: