zaun_community_protocol/permissions/lockdown/
restrictions.rs1use bitflags::bitflags;
4
5use crate::bits::Permissions;
6
7bitflags! {
8 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
10 pub struct LockdownRestrictions: u16 {
11 const PAUSE_JOINS = 1 << 0;
13 const DISABLE_INVITE_REDEEM = 1 << 1;
15 const DISABLE_INVITE_CREATE = 1 << 2;
17 const REQUIRE_APPROVAL = 1 << 3;
19 const RESTRICT_NEWCOMERS = 1 << 4;
21 const DISABLE_ATTACHMENTS = 1 << 5;
23 const DISABLE_LINKS = 1 << 6;
25 const DISABLE_MASS_MENTIONS = 1 << 7;
27 const RESTRICT_VOICE = 1 << 8;
29 const RESTRICT_EXPENSIVE = 1 << 9;
34 }
35}
36
37const WIRE: [(&str, LockdownRestrictions); 10] = [
39 ("pause_joins", LockdownRestrictions::PAUSE_JOINS),
40 (
41 "disable_invite_redeem",
42 LockdownRestrictions::DISABLE_INVITE_REDEEM,
43 ),
44 (
45 "disable_invite_create",
46 LockdownRestrictions::DISABLE_INVITE_CREATE,
47 ),
48 ("require_approval", LockdownRestrictions::REQUIRE_APPROVAL),
49 (
50 "restrict_newcomers",
51 LockdownRestrictions::RESTRICT_NEWCOMERS,
52 ),
53 (
54 "disable_attachments",
55 LockdownRestrictions::DISABLE_ATTACHMENTS,
56 ),
57 ("disable_links", LockdownRestrictions::DISABLE_LINKS),
58 (
59 "disable_mass_mentions",
60 LockdownRestrictions::DISABLE_MASS_MENTIONS,
61 ),
62 ("restrict_voice", LockdownRestrictions::RESTRICT_VOICE),
63 (
64 "restrict_expensive",
65 LockdownRestrictions::RESTRICT_EXPENSIVE,
66 ),
67];
68
69impl LockdownRestrictions {
70 #[must_use]
72 pub fn from_wire(name: &str) -> Option<Self> {
73 WIRE.iter().find(|(n, _)| *n == name).map(|(_, r)| *r)
74 }
75
76 #[must_use]
79 pub fn permission_mask(self) -> Permissions {
80 let mut mask = Permissions::empty();
81 let pairs = [
82 (Self::DISABLE_INVITE_CREATE, Permissions::CREATE_INVITE),
83 (Self::DISABLE_ATTACHMENTS, Permissions::ATTACH_FILES),
84 (Self::DISABLE_LINKS, Permissions::EMBED_LINKS),
87 (Self::DISABLE_MASS_MENTIONS, Permissions::MENTION_EVERYONE),
88 (Self::RESTRICT_VOICE, Permissions::CONNECT),
89 ];
90 for (flag, bits) in pairs {
91 if self.contains(flag) {
92 mask |= bits;
93 }
94 }
95 mask
96 }
97}