Skip to content

Banning and unbanning users

Kyle2142 edited this page Jun 15, 2018 · 1 revision

Banning

To ban a user, simply:

$bot->api->kickChatMember([
    'chat_id' => '@my_awesome_channel',
    'user_id' => 12345678
]);

If you want them to be automatically unbanned after a period of time (NB: this will not automatically add them back), you can use until_date (unix time):

$until = new DateTime(); //sets current time
$until->add(new DateInterval("PT5H")); // adds 5 hours to current time, see php.net/manual/en/dateinterval.construct.php
$bot->api->kickChatMember([
    'chat_id' => '@my_awesome_channel',
    'user_id' => 12345678,
    'until_date' => $until->getTimestamp() //convert back to unix
]);

If you do not want to use DateInterval, you can calculate it yourself:

$time = new DateTime()->getTimestamp(); //unix time as int
$time += 60*60*5; // add 60s*60m*5, which is 5 hours

$bot->api ...//rest of request here
    'until_date' => $time
...

Unbanning

You can wait for until_date to expire (maybe never), or you can unban manually:

$bot->api->unbanChatMember([
    'chat_id' => '@my_awesome_channel',
    'user_id' => 12345678
]);