@@ -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,24 @@ public function preExecute()
145152 return $ this ->executeNoDb ();
146153 }
147154
155+ if ($ this ->isPrivateOnly () && $ this ->removeNonPrivateMessage ()) {
156+ $ message = $ this ->getMessage ();
157+
158+ if ($ user = $ message ->getFrom ()) {
159+ return Request::sendMessage ([
160+ 'chat_id ' => $ user ->getId (),
161+ 'parse_mode ' => 'Markdown ' ,
162+ 'text ' => sprintf (
163+ "/%s command is only available in a private chat. \n(`%s`) " ,
164+ $ this ->getName (),
165+ $ message ->getText ()
166+ ),
167+ ]);
168+ }
169+
170+ return Request::emptyResponse ();
171+ }
172+
148173 return $ this ->execute ();
149174 }
150175
@@ -296,6 +321,16 @@ public function isEnabled()
296321 return $ this ->enabled ;
297322 }
298323
324+ /**
325+ * If this command is intended for private chats only.
326+ *
327+ * @return bool
328+ */
329+ public function isPrivateOnly ()
330+ {
331+ return $ this ->private_only ;
332+ }
333+
299334 /**
300335 * If this is a SystemCommand
301336 *
@@ -325,4 +360,27 @@ public function isUserCommand()
325360 {
326361 return ($ this instanceof UserCommand);
327362 }
363+
364+ /**
365+ * Delete the current message if it has been called in a non-private chat.
366+ *
367+ * @return bool
368+ */
369+ protected function removeNonPrivateMessage ()
370+ {
371+ $ message = $ this ->getMessage ();
372+ $ chat = $ message ->getChat ();
373+
374+ if (!$ chat ->isPrivateChat ()) {
375+ // Delete the falsely called command message.
376+ Request::deleteMessage ([
377+ 'chat_id ' => $ chat ->getId (),
378+ 'message_id ' => $ message ->getMessageId (),
379+ ]);
380+
381+ return true ;
382+ }
383+
384+ return false ;
385+ }
328386}
0 commit comments