feat(core): permissions query, finish bots impl

This commit is contained in:
Paul Makles
2023-08-05 16:14:47 +01:00
parent 9f3c1036d0
commit a681df04bd
20 changed files with 481 additions and 173 deletions

View File

@@ -1,5 +1,5 @@
use once_cell::sync::Lazy;
use std::ops::Add;
use std::{fmt, ops::Add};
/// Abstract channel type
pub enum ChannelType {
@@ -102,6 +102,12 @@ pub enum ChannelPermission {
GrantAll = u64::MAX,
}
impl fmt::Display for ChannelPermission {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u64 { *a as u64 | *b as u64 });
impl_op_ex_commutative!(+ |a: &u64, b: &ChannelPermission| -> u64 { *a | *b as u64 });
@@ -136,4 +142,9 @@ pub static DEFAULT_PERMISSION_SERVER: Lazy<u64> = Lazy::new(|| {
)
});
pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| ChannelPermission::SendMessage + ChannelPermission::SendEmbeds + ChannelPermission::Masquerade + ChannelPermission::React);
pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| {
ChannelPermission::SendMessage
+ ChannelPermission::SendEmbeds
+ ChannelPermission::Masquerade
+ ChannelPermission::React
});

View File

@@ -3,6 +3,7 @@ mod server;
mod user;
pub use channel::*;
use revolt_result::{create_error, Result};
pub use server::*;
pub use user::*;
@@ -31,6 +32,30 @@ impl PermissionValue {
pub fn restrict(&mut self, v: u64) {
self.0 &= v;
}
/// Check whether certain a permission has been granted
pub fn has(&mut self, v: u64) -> bool {
(self.0 & v) == v
}
/// Check whether certain a channel permission has been granted
pub fn has_channel_permission(&mut self, permission: ChannelPermission) -> bool {
self.has(permission as u64)
}
/// Throw if missing channel permission
pub fn throw_if_lacking_channel_permission(
&mut self,
permission: ChannelPermission,
) -> Result<()> {
if self.has_channel_permission(permission) {
Ok(())
} else {
Err(create_error!(MissingPermission {
permission: permission.to_string()
}))
}
}
}
impl From<i64> for PermissionValue {

View File

@@ -1,3 +1,5 @@
use std::fmt;
/// User's relationship with another user (or themselves)
pub enum RelationshipStatus {
None,
@@ -21,5 +23,11 @@ pub enum UserPermission {
Invite = 1 << 3,
}
impl fmt::Display for UserPermission {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &UserPermission| -> u32 { *a | *b as u32 });