Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Admin & Ban Rights

layer-client provides typed builders for promoting administrators and restricting users.


Promoting an admin — AdminRightsBuilder

#![allow(unused)]
fn main() {
use layer_client::participants::AdminRightsBuilder;

client.set_admin_rights(
    peer,
    user_id,
    AdminRightsBuilder::new()
        .post_messages(true)
        .edit_messages(true)
        .delete_messages(true)
        .invite_users(true)
        .pin_messages(true)
        .rank("Editor"),  // optional custom title
).await?;
}

All AdminRightsBuilder methods

MethodDefaultDescription
.change_info(v)falseEdit channel title, description, photo
.post_messages(v)falsePost in the channel
.edit_messages(v)falseEdit any message
.delete_messages(v)falseDelete any message
.ban_users(v)falseRestrict other members
.invite_users(v)falseAdd members
.pin_messages(v)falsePin and unpin messages
.add_admins(v)falsePromote other admins (requires self to have this right)
.anonymous(v)falsePost as the channel (anonymous)
.manage_call(v)falseStart/manage video chats
.manage_topics(v)falseCreate/edit/delete forum topics
.rank(r)Custom admin title shown in the member list
AdminRightsBuilder::full_admin()All rights enabled

Full admin shorthand

#![allow(unused)]
fn main() {
client.set_admin_rights(
    peer,
    user_id,
    AdminRightsBuilder::full_admin(),
).await?;
}

Remove admin rights

Pass an empty builder to revoke all rights:

#![allow(unused)]
fn main() {
client.set_admin_rights(
    peer,
    user_id,
    AdminRightsBuilder::new(),  // all false = remove admin
).await?;
}

Restricting a member — BanRightsBuilder

#![allow(unused)]
fn main() {
use layer_client::participants::BanRightsBuilder;

// Mute a user (disable text, stickers, GIFs, inline)
client.set_banned_rights(
    peer,
    user_id,
    BanRightsBuilder::new()
        .send_messages(false)
        .send_stickers(false)
        .send_gifs(false)
        .send_inline(false),
).await?;
}

All BanRightsBuilder methods

MethodDefaultDescription
.view_messages(v)trueCan see the chat at all
.send_messages(v)trueCan send text messages
.send_media(v)trueCan send photos, videos, etc.
.send_stickers(v)trueCan send stickers
.send_gifs(v)trueCan send GIFs
.send_games(v)trueCan send games
.send_inline(v)trueCan use inline bots
.embed_links(v)trueCan include link previews
.send_polls(v)trueCan create polls
.change_info(v)trueCan edit group info
.invite_users(v)trueCan add members
.pin_messages(v)trueCan pin messages
.until_date(ts)0 (permanent)Restriction expires at this unix timestamp
BanRightsBuilder::full_ban()All rights false (full kick/ban)

Temporary restriction (mute for 24 hours)

#![allow(unused)]
fn main() {
let expires = chrono::Utc::now().timestamp() as i32 + 86_400; // 24h

client.set_banned_rights(
    peer,
    user_id,
    BanRightsBuilder::new()
        .send_messages(false)
        .send_media(false)
        .until_date(expires),
).await?;
}

Full ban (kick + block from re-joining)

#![allow(unused)]
fn main() {
client.set_banned_rights(
    peer,
    user_id,
    BanRightsBuilder::full_ban(),
).await?;
}

Lift all restrictions (unban)

Pass a builder with all defaults (everything true, no until_date):

#![allow(unused)]
fn main() {
client.set_banned_rights(
    peer,
    user_id,
    BanRightsBuilder::new(),  // all true = no restrictions
).await?;
}

Check effective permissions

#![allow(unused)]
fn main() {
let perms = client.get_permissions(peer, user_id).await?;
println!("can send messages: {}", perms.can_send_messages());
println!("is admin: {}", perms.is_admin());
println!("is creator: {}", perms.is_creator());
}

Kick from basic group

For legacy basic groups (not supergroups/channels), use the kick method:

#![allow(unused)]
fn main() {
client.kick_participant(chat_id, user_id).await?;
}

This removes the user from the group. They can be added back by any member.


Participant status

get_participants returns Participant structs with a status field:

#![allow(unused)]
fn main() {
let members = client.get_participants(peer, 0).await?;

for member in members {
    match member.status {
        ParticipantStatus::Creator  => println!("{} is the creator", member.user.first_name.as_deref().unwrap_or("?")),
        ParticipantStatus::Admin    => println!("{} is an admin", member.user.first_name.as_deref().unwrap_or("?")),
        ParticipantStatus::Member   => {}
        ParticipantStatus::Banned   => println!("{} is banned", member.user.first_name.as_deref().unwrap_or("?")),
        ParticipantStatus::Restricted => println!("{} is restricted", member.user.first_name.as_deref().unwrap_or("?")),
        ParticipantStatus::Left     => println!("{} has left", member.user.first_name.as_deref().unwrap_or("?")),
    }
}
}