Skip to content

Commit

Permalink
feat: make configurations optional
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed May 28, 2023
1 parent 2f983ec commit 580235b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 87 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
authors = ["oSumAtrIX"]
authors = ["ReVanced"]
description = "The official Discord bot assisting the ReVanced Discord server."
homepage = "https://revanced.app"
license = "GPL-3.0"
name = "revanced-discord-bot"
repository = "https://github.com/revanced/revanced-discord-bot"
version = "2.3.1"
version = "2.4.2"
edition = "2021"

[profile.release]
Expand Down
10 changes: 5 additions & 5 deletions src/model/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ pub struct Introduction {

#[derive(Serialize, Deserialize)]
pub struct MessageResponse {
pub includes: Includes,
pub excludes: Excludes,
pub condition: Condition,
pub includes: Option<Includes>,
pub excludes: Option<Excludes>,
pub condition: Option<Condition>,
pub response: Response,
}

Expand Down Expand Up @@ -145,14 +145,14 @@ pub struct Author {

#[derive(Serialize, Deserialize)]
pub struct Includes {
pub channels: Vec<u64>,
pub channels: Option<Vec<u64>>,
#[serde(rename = "match", with = "serde_regex")]
pub match_field: Vec<Regex>,
}

#[derive(Serialize, Deserialize)]
pub struct Excludes {
pub roles: Vec<u64>,
pub roles: Option<Vec<u64>>,
#[serde(rename = "match", with = "serde_regex")]
pub match_field: Vec<Regex>,
}
Expand Down
154 changes: 79 additions & 75 deletions src/utils/autorespond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,96 @@ pub async fn auto_respond(ctx: &serenity::Context, new_message: &serenity::Messa
let message = &new_message.content;

for response in responses {
// check if the message was sent in a channel that is included in the responder
if !response
.includes
.channels
.iter()
.any(|&channel_id| channel_id == new_message.channel_id.0)
{
continue;
}
tracing::log::trace!("d");
// check if the channel is whitelisted
if let Some(includes) = &response.includes {
if let Some(channels) = &includes.channels {
if !channels.contains(&new_message.channel_id.0) {
continue;
}
}

// check if the message was sent by a user that is not excluded from the responder
let excludes = &response.excludes;
let member_roles = &new_message.member.as_ref().unwrap().roles;
if excludes.roles.iter().any(|&role_id| {
member_roles
.iter()
.any(|&member_role| role_id == member_role.0)
}) {
continue;
// check if message matches regex
if !contains_match(&includes.match_field, message) {
tracing::log::trace!("Message does not match regex");
continue;
}
}

// check if the message does not match any of the excludes
if contains_match(&excludes.match_field, message) {
continue;
}
if let Some(excludes) = &response.excludes {
// check if the role is blacklisted
if let Some(roles) = &excludes.roles {
let member_roles = &new_message.member.as_ref().unwrap().roles;
if roles.iter().any(|&role_id| {
member_roles
.iter()
.any(|&member_role| role_id == member_role.0)
}) {
continue;
}
}

// check if the message does match any of the includes
if !(contains_match(&response.includes.match_field, message)) {
continue;
// check if the message is not blacklisted
if contains_match(&excludes.match_field, message) {
continue;
}
}

let min_age = response.condition.user.server_age;
if let Some(condition) = &response.condition {
let min_age = condition.user.server_age;

if min_age != 0 {
let joined_at = ctx
.http
.get_member(new_message.guild_id.unwrap().0, new_message.author.id.0)
.await
.unwrap()
.joined_at
.unwrap()
.unix_timestamp();
if min_age != 0 {
let joined_at = ctx
.http
.get_member(new_message.guild_id.unwrap().0, new_message.author.id.0)
.await
.unwrap()
.joined_at
.unwrap()
.unix_timestamp();

let must_joined_at = DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(joined_at, 0).unwrap(),
Utc,
);
let but_joined_at = Utc::now() - Duration::days(min_age);
let must_joined_at = DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(joined_at, 0).unwrap(),
Utc,
);
let but_joined_at = Utc::now() - Duration::days(min_age);

if must_joined_at <= but_joined_at {
return;
if must_joined_at <= but_joined_at {
continue;
}
}
}

if let Err(err) = new_message
.channel_id
.send_message(&ctx.http, |m| {
m.reference_message(new_message);
match &response.response.embed {
Some(embed) => m.embed(|e| {
e.title(&embed.title)
.description(&embed.description)
.color(embed.color)
.fields(embed.fields.iter().map(|field| {
(field.name.clone(), field.value.clone(), field.inline)
}))
.footer(|f| {
f.text(&embed.footer.text);
f.icon_url(&embed.footer.icon_url)
})
.thumbnail(&embed.thumbnail.url)
.image(&embed.image.url)
.author(|a| {
a.name(&embed.author.name).icon_url(&embed.author.icon_url)
})
}),
None => m.content(response.response.message.as_ref().unwrap()),
}
})
.await
{
error!(
"Failed to reply to the message from {}. Error: {:?}",
new_message.author.tag(),
err
);
}
if let Err(err) = new_message
.channel_id
.send_message(&ctx.http, |m| {
m.reference_message(new_message);
match &response.response.embed {
Some(embed) => m.embed(|e| {
e.title(&embed.title)
.description(&embed.description)
.color(embed.color)
.fields(embed.fields.iter().map(|field| {
(field.name.clone(), field.value.clone(), field.inline)
}))
.footer(|f| {
f.text(&embed.footer.text);
f.icon_url(&embed.footer.icon_url)
})
.thumbnail(&embed.thumbnail.url)
.image(&embed.image.url)
.author(|a| a.name(&embed.author.name).icon_url(&embed.author.icon_url))
}),
None => m.content(response.response.message.as_ref().unwrap()),
}
})
.await
{
error!(
"Failed to reply to the message from {}. Error: {:?}",
new_message.author.tag(),
err
);
}
}
}

0 comments on commit 580235b

Please sign in to comment.