@@ -97,6 +97,13 @@ abstract class Command
97
97
*/
98
98
protected $ need_mysql = false ;
99
99
100
+ /*
101
+ * Make sure this command only executes on a private chat.
102
+ *
103
+ * @var bool
104
+ */
105
+ protected $ private_only = false ;
106
+
100
107
/**
101
108
* Command config
102
109
*
@@ -145,6 +152,24 @@ public function preExecute()
145
152
return $ this ->executeNoDb ();
146
153
}
147
154
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
+
148
173
return $ this ->execute ();
149
174
}
150
175
@@ -296,6 +321,16 @@ public function isEnabled()
296
321
return $ this ->enabled ;
297
322
}
298
323
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
+
299
334
/**
300
335
* If this is a SystemCommand
301
336
*
@@ -325,4 +360,27 @@ public function isUserCommand()
325
360
{
326
361
return ($ this instanceof UserCommand);
327
362
}
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
+ }
328
386
}
0 commit comments