Coder Social home page Coder Social logo

permission-calculator's Introduction

discord badge github badge license badge rust badge

project logo

twilight-permission-calculator

twilight-permission-calculator is a permission calculator for the Discord twilight-rs library.

Installation

Add the following to your Cargo.toml:

[dependencies]
twilight-permission-calculator = { branch = "trunk", git = "https://github.com/twilight-rs/permission-calculator" }

Features

The tracing dependency is optional and can be disabled if you don't want logging. To do this, use this in your Cargo.toml:

[dependencies]
twilight-permission-calculator = { branch = "trunk", default-features = false, git = "https://github.com/twilight-rs/permission-calculator" }

Examples

Calculating member permissions in a channel

Take a scenario where a member has two roles: the @everyone role (with the same ID as the guild) that grants the View Channel permission across the whole guild, and a second role that grants the Send Messages permission across the whole guild. This means that, across the server, the member will have the View Channel and Send Messages permissions, unless denied or expanded by channel-specific permission overwrites.

In a given channel, there are two permission overwrites: one for the @everyone role and one for the member itself. These overwrites look like:

  • @everyone role is allowed the Embed Links and Add Reactions permissions;
  • Member is denied the Send Messages permission.

Taking into account the guild root-level permissions and the permission overwrites, the end result is that in the specified channel the user has the View Channel, Embed Links, and Add Reactions permission, but is denied the Send Messages permission that their second role was granted on a root level.

Let's see that in code:

use twilight_permission_calculator::Calculator;
use twilight_model::{
    channel::{
        permission_overwrite::{PermissionOverwriteType, PermissionOverwrite},
        ChannelType
    },
    guild::Permissions,
    id::{GuildId, RoleId, UserId},
};

let guild_id = GuildId(1);
let user_id = UserId(3);
let member_roles = &[
    // Guild-level @everyone role that, by default, allows everyone to view
    // channels.
    (RoleId(1), Permissions::VIEW_CHANNEL),
    // Guild-level permission that grants members with the role the Send
    // Messages permission.
    (RoleId(2), Permissions::SEND_MESSAGES),
];

let channel_overwrites = &[
    // All members are given the Add Reactions and Embed Links members via
    // the `@everyone` role.
    PermissionOverwrite {
        allow: Permissions::ADD_REACTIONS | Permissions::EMBED_LINKS,
        deny: Permissions::empty(),
        kind: PermissionOverwriteType::Role(RoleId(1)),
    },
    // Member is denied the Send Messages permission.
    PermissionOverwrite {
        allow: Permissions::empty(),
        deny: Permissions::SEND_MESSAGES,
        kind: PermissionOverwriteType::Member(user_id),
    },
];

let calculated_permissions = Calculator::new(guild_id, user_id, member_roles)
    .in_channel(ChannelType::GuildText, channel_overwrites)?;

// Now that we've got the member's permissions in the channel, we can
// check that they have the server-wide View Channel permission and
// the Add Reactions permission granted, but their guild-wide Send Messages
// permission was denied. Additionally, since the user can't send messages,
// their Embed Links permission was removed.

let expected = Permissions::ADD_REACTIONS | Permissions::VIEW_CHANNEL;
assert!(!calculated_permissions.contains(Permissions::EMBED_LINKS));
assert!(!calculated_permissions.contains(Permissions::SEND_MESSAGES));
assert_eq!(expected, calculated_permissions);

permission-calculator's People

Contributors

7596ff avatar dusterthefirst avatar vilgotf avatar zeylahellyer avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.