Skip to content

Commit

Permalink
0.1.7 - setters and new permission
Browse files Browse the repository at this point in the history
  • Loading branch information
toastxc committed Jul 8, 2023
1 parent 7474c08 commit bc1d4ea
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 50 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reywen"
version = "0.1.16"
version = "0.1.17"
edition = "2021"
description = "A fast and simple framework for creating bots for revolt.chat"
license = "GPL-3.0-only"
Expand All @@ -10,14 +10,14 @@ documentation = "https://docs.rs/reywen/latest/reywen/"
[dependencies]

# revolt compat
iso8601-timestamp = "0.2.11"
validator = "0.16.1"
num_enum = "0.6.1"
iso8601-timestamp = "0.2.10"
validator = "0.16.0"
num_enum = "0.6.0"
indexmap = { version = "2.0.0", features = ["serde"] }

# serde
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.99"
serde_json = "1.0.94"

# misc
rand = "0.8.5"
Expand Down
87 changes: 77 additions & 10 deletions src/client/methods/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use reywen_http::{driver::Method, results::DeltaError, utils::struct_to_url};
use serde::{Deserialize, Serialize};

use crate::{
client::Client,
json,
client::{methods::opt_vec_add, Client},
json, opt_str, ref_str,
structures::channels::message::{
BulkMessageResponse2, Interactions, Masquerade, Message, MessageSort, Reply, SendableEmbed,
},
};

use super::origin;

impl Client {
pub async fn message_ack(&self, channel: &str, message: &str) -> Result<(), DeltaError> {
self.http
Expand Down Expand Up @@ -169,10 +171,10 @@ impl DataBulkDelete {
self.ids.push(String::from(message));
self.to_owned()
}
pub fn set_messages(&mut self, messages: Vec<&str>) -> Self {
for x in messages {
self.ids.push(String::from(x))
}
pub fn set_messages(&mut self, mut messages: Vec<&str>) -> Self {
messages
.iter_mut()
.for_each(|item| self.ids.push(item.to_string()));
self.to_owned()
}
}
Expand All @@ -189,7 +191,7 @@ impl DataEditMessage {
Default::default()
}

pub fn content(&mut self, content: &str) -> Self {
pub fn set_content(&mut self, content: &str) -> Self {
self.content = Some(String::from(content));
self.to_owned()
}
Expand Down Expand Up @@ -340,9 +342,75 @@ impl DataMessageSend {
self.content = Some(String::from(content));
self.to_owned()
}
pub fn set_attachments(&mut self, attachments: Vec<String>) -> Self {
self.attachments = origin(&self.attachments, attachments);
self.to_owned()
}
pub fn add_attachment(&mut self, attachment: &str) -> Self {
self.attachments = opt_vec_add(&self.attachments, ref_str!(attachment));
self.to_owned()
}

pub fn set_replies(&mut self, replies: Vec<Reply>) -> Self {
self.replies = origin(&self.replies, replies);
self.to_owned()
}
pub fn add_reply(&mut self, reply: &Reply) -> Self {
self.replies = opt_vec_add(&self.replies, reply);
self.to_owned()
}
pub fn set_reply_str(&mut self, replies: Vec<String>) {
self.replies = Some(
replies
.into_iter()
.map(|id| Reply {
id,
..Default::default()
})
.collect(),
)
}
pub fn add_reply_str(&mut self, reply: &str) -> Self {
self.add_reply(&Reply {
id: String::from(reply),
..Default::default()
});
self.to_owned()
}

pub fn set_embeds(&mut self, embeds: Vec<SendableEmbed>) -> Self {
let embeds = origin(&self.embeds, embeds);
self.embeds = embeds;
self.to_owned()
}
pub fn add_embed(&mut self, embed: &SendableEmbed) -> Self {
self.embeds = opt_vec_add(&self.embeds, embed);
self.to_owned()
}

pub fn set_masquerade(&mut self, masquerade: &Masquerade) -> Self {
self.masquerade = Some(masquerade.clone());
self.masquerade = Some(masquerade.to_owned());
self.to_owned()
}

pub fn set_interactions(&mut self, interactions: Interactions) -> Self {
self.interactions = Some(interactions);
self.to_owned()
}
pub fn add_reaction(&mut self, reaction: &str) -> Self {
self.set_interactions(
self.interactions
.as_ref()
.map_or(Interactions::new(), |origin| origin.to_owned())
.add_reaction(reaction),
)
}
pub fn set_reactions(&mut self, reactions: Vec<String>) -> Self {
let mut interactions = self.interactions.clone().unwrap_or_default();
reactions.into_iter().for_each(|item| {
interactions.add_reaction(&item);
});

self.to_owned()
}
}
Expand All @@ -355,13 +423,12 @@ pub struct DataUnreact {
/// Remove all reactions
pub remove_all: Option<bool>,
}

impl DataUnreact {
pub fn new() -> Self {
Default::default()
}
pub fn set_user_id(&mut self, user_id: &str) -> Self {
self.user_id = Some(String::from(user_id));
self.user_id = opt_str!(user_id);
self.to_owned()
}
pub fn set_remove_all(&mut self, remove_all: bool) -> Self {
Expand Down
31 changes: 31 additions & 0 deletions src/client/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,40 @@ pub mod permissions;
pub mod server;
pub mod user;

/// macro for creating jsons
#[macro_export]
macro_rules! json {
($data:expr) => {
Some(&serde_json::to_string(&$data).unwrap_or_default())
};
}

#[macro_export]
macro_rules! ref_str {
($data:expr) => {
&String::from($data)
};
}

#[macro_export]
macro_rules! opt_str {
($data:expr) => {
Some(String::from($data))
};
}

/// autovec is a helper for adding an entry to an optional vector, since this code is quite big and is repeated many times it has
/// been given its own function
pub fn opt_vec_add<T: Clone>(input: &Option<Vec<T>>, new: &T) -> Option<Vec<T>> {
Some(input.clone().map_or(vec![new.to_owned()], |mut origin| {
origin.push(new.to_owned());
origin
}))
}

// grabs original value and appends it to an optional vector
pub fn origin<T: Clone + Default>(input: &Option<Vec<T>>, new: Vec<T>) -> Option<Vec<T>> {
let mut a = input.clone().unwrap_or_default();
a.extend(new);
Some(a)
}
9 changes: 9 additions & 0 deletions src/client/methods/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ impl DataRoleCreate {
..Default::default()
}
}
pub fn set_name(&mut self, name: &str) -> Self {
self.name = String::from(name);
self.to_owned()
}

pub fn set_rank(&mut self, rank: u32) -> Self {
self.rank = Some(rank);
self.to_owned()
}
}

/// # New Role Response
Expand Down
61 changes: 56 additions & 5 deletions src/client/methods/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::{
},
};

use super::opt_vec_add;

impl Client {
pub async fn server_ack(&self, server: &str) -> Result<(), DeltaError> {
self.http
Expand Down Expand Up @@ -120,10 +122,25 @@ impl DataChannelCreate {
pub fn new(name: &str) -> Self {
Self {
name: String::from(name),
r#type: Some(ChannelType::Text),
..Default::default()
}
}
pub fn set_type(&mut self, r#type: ChannelType) -> Self {
self.r#type = Some(r#type);
self.to_owned()
}
pub fn set_name(&mut self, name: &str) -> Self {
self.name = String::from(name);
self.to_owned()
}
pub fn set_description(&mut self, description: &str) -> Self {
self.description = Some(String::from(description));
self.to_owned()
}
pub fn set_nsfw(&mut self, nsfw: bool) -> Self {
self.nsfw = Some(nsfw);
self.to_owned()
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -174,6 +191,18 @@ impl DataCreateServer {
..Default::default()
}
}
pub fn set_name(&mut self, name: &str) -> Self {
self.name = String::from(name);
self.to_owned()
}
pub fn set_description(&mut self, description: &str) -> Self {
self.description = Some(String::from(description));
self.to_owned()
}
pub fn set_nsfw(&mut self, nsfw: bool) -> Self {
self.nsfw = Some(nsfw);
self.to_owned()
}
}

/// # Create Server Response
Expand All @@ -186,7 +215,7 @@ pub struct CreateServerResponse {
}

/// # Server Data
#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct DataEditServer {
/// Server name
pub name: Option<String>,
Expand Down Expand Up @@ -220,9 +249,31 @@ pub struct DataEditServer {

impl DataEditServer {
pub fn new() -> Self {
Self {
..Default::default()
}
Default::default()
}

pub fn set_name(&mut self, name: &str) -> Self {
self.name = Some(String::from(name));
self.to_owned()
}
pub fn set_description(&mut self, description: &str) -> Self {
self.name = Some(String::from(description));
self.to_owned()
}
pub fn set_icon(&mut self, icon: &str) -> Self {
self.icon = Some(String::from(icon));
self.to_owned()
}
pub fn set_banner(&mut self, banner: &str) -> Self {
self.banner = Some(String::from(banner));
self.to_owned()
}
pub fn add_category(&mut self, category: Category) {
self.categories = opt_vec_add(&self.categories, &category)
}
pub fn set_categories(&mut self, categories: Vec<Category>) -> Self {
self.categories = Some(categories);
self.to_owned()
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/structures/channels/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ impl Interactions {
pub fn is_default(&self) -> bool {
self.reactions.is_none()
}
pub fn add_reaction(&mut self, reaction: &str) -> Self {
let mut origin = match self.reactions.clone() {
Some(original) => original,
None => IndexSet::new(),
};
origin.insert(String::from(reaction));
self.reactions = Some(origin);
self.to_owned()
}

pub fn set_reactions(&mut self, reactions: Vec<String>) -> Self {
let mut index = self.reactions.to_owned().unwrap_or_default();
reactions.into_iter().for_each(|item| {
index.insert(item);
});
self.reactions = Some(index);
self.to_owned()
}
pub fn new() -> Self {
Default::default()
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
36 changes: 13 additions & 23 deletions src/structures/permissions/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub struct Permissions {
}

impl Permissions {
// converts from readable to bitwise

pub fn add_allow(&mut self, permission: Permission) -> Self {
self.allow.push(permission);
self.clone()
Expand All @@ -28,31 +26,23 @@ impl Permissions {
Default::default()
}

pub fn export(&self) -> PermissionData {
// define channel
let mut channel = Override::new();

for x in self.allow.clone() {
channel.allow += x as u64;
}
for x in self.deny.clone() {
channel.deny += x as u64;
}

// define group

let mut group = 0;

for x in self.allow.clone() {
group += x as u64;
}
pub fn convert(input: Vec<Permission>) -> u64 {
input
.into_iter()
.map(|item| item as u64)
.collect::<Vec<u64>>()
.into_iter()
.sum()
}

// deny is simply dropped from scope
pub fn export(&self) -> PermissionData {
let allow = Self::convert(self.allow.to_owned());
let deny = Self::convert(self.deny.to_owned());

PermissionData {
value: Value { permissions: group },
value: Value { permissions: allow },
field: Field {
permissions: channel,
permissions: Override { allow, deny },
},
}
}
Expand Down
Loading

0 comments on commit bc1d4ea

Please sign in to comment.