Skip to content

Restricting users

Kyle2142 edited this page Feb 19, 2019 · 3 revisions

To be able to restrict (or ban) a user, your bot must have the can_restrict_members permission.

Be sure to check out Checking permissions and restrictions

If you just want a quick and easy "mute user", look no further:

$user = 12345678;
$chat = '@my_awesome_channel';
$bot->muteUser($user, $chat);

The default is to mute forever, but you can specify the third parameter, $until (unix timestamp) if you want to.

To have more fine-grained control, use $bot->api->restrictChatMember(...):

Example: stop user sending media for 9 days

$time = time(); //current unix time
$time += 60*60*24*9; // add 60s*60m*24h*9 = 9 days to the current time
$bot->api->restrictChatMember([
    'chat_id' => '@my_awesome_channel',
    'user_id' => 12345678,
    'can_send_messages' => true,          //user may still send normal messages
    'can_send_media_messages' => false,   //user may not send pictures/videos/audio
    'can_send_other_messages' => false,   //user may not send stickers or GIFS
    'can_add_web_page_previews' => true,  //user may have link previews
    'until_date' => $time
]);

Note that you do not have to specify permissions that you do not want to change.

Let's say you want to stop a user spamming stickers, but you don't want to accidentally remove his other restrictions (maybe someone else restricted him), you can just:

$bot->api->restrictChatMember([
    'chat_id' => '@my_awesome_channel',
    'user_id' => 12345678,
    'can_send_other_messages' => false   //user may not send stickers or GIFS
]);