Skip to content

Commit 5017d82

Browse files
committed
New command parameter to enforce usage in private chats only.
1 parent 499c74c commit 5017d82

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

src/Commands/AdminCommands/CleanupCommand.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class CleanupCommand extends AdminCommand
6262
*/
6363
protected $need_mysql = true;
6464

65+
/**
66+
* @var bool
67+
*/
68+
protected $private_only = true;
69+
6570
/**
6671
* Default tables to clean, cleaning 'chat', 'user' and 'user_chat' by default is bad practice!
6772
*
@@ -351,20 +356,14 @@ public function executeNoDb()
351356
public function execute()
352357
{
353358
$message = $this->getMessage();
354-
$chat_id = $message->getChat()->getId();
359+
$user_id = $message->getFrom()->getId();
355360
$text = $message->getText(true);
356361

357362
$data = [
358-
'chat_id' => $chat_id,
363+
'chat_id' => $user_id,
359364
'parse_mode' => 'Markdown',
360365
];
361366

362-
if (!$message->getChat()->isPrivateChat()) {
363-
$data['text'] = '/cleanup command is only available in a private chat.';
364-
365-
return Request::sendMessage($data);
366-
}
367-
368367
$settings = $this->getSettings($text);
369368
$queries = $this->getQueries($settings);
370369

src/Commands/Command.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ abstract class Command
9797
*/
9898
protected $need_mysql = false;
9999

100+
/**
101+
* Make sure this command only executes on a private chat.
102+
*
103+
* @var bool
104+
*/
105+
protected $private_only = false;
106+
100107
/**
101108
* Command config
102109
*
@@ -145,6 +152,27 @@ public function preExecute()
145152
return $this->executeNoDb();
146153
}
147154

155+
if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) {
156+
$message = $this->getMessage();
157+
$chat = $message->getChat();
158+
$user = $message->getFrom();
159+
160+
// @todo Deleting a message from a channel isn't working yet.
161+
if ($user !== null && !$chat->isChannel()) {
162+
return Request::sendMessage([
163+
'chat_id' => $user->getId(),
164+
'parse_mode' => 'Markdown',
165+
'text' => sprintf(
166+
"/%s command is only available in a private chat.\n(`%s`)",
167+
$this->getName(),
168+
$message->getText()
169+
),
170+
]);
171+
}
172+
173+
return Request::emptyResponse();
174+
}
175+
148176
return $this->execute();
149177
}
150178

@@ -296,6 +324,16 @@ public function isEnabled()
296324
return $this->enabled;
297325
}
298326

327+
/**
328+
* If this command is intended for private chats only.
329+
*
330+
* @return bool
331+
*/
332+
public function isPrivateOnly()
333+
{
334+
return $this->private_only;
335+
}
336+
299337
/**
300338
* If this is a SystemCommand
301339
*
@@ -325,4 +363,27 @@ public function isUserCommand()
325363
{
326364
return ($this instanceof UserCommand);
327365
}
366+
367+
/**
368+
* Delete the current message if it has been called in a non-private chat.
369+
*
370+
* @return bool
371+
*/
372+
protected function removeNonPrivateMessage()
373+
{
374+
$message = $this->getMessage();
375+
$chat = $message->getChat();
376+
377+
if (!$chat->isPrivateChat()) {
378+
// Delete the falsely called command message.
379+
Request::deleteMessage([
380+
'chat_id' => $chat->getId(),
381+
'message_id' => $message->getMessageId(),
382+
]);
383+
384+
return true;
385+
}
386+
387+
return false;
388+
}
328389
}

0 commit comments

Comments
 (0)