Skip to content

Commit

Permalink
feat: self-timeout transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Jan 17, 2024
1 parent 534abf9 commit 3d42267
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use crate::utils::actix_utils::ActixError;
use crate::utils::actix::ActixError;
use log::info;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn to_vec() -> Vec<
presence::presence(),
say::say(),
self_timeout::self_timeout(),
self_timeout::transparency(),
shiggy::shiggy(),
sysinfo::sysinfo(),
translate::translate(),
Expand Down
87 changes: 71 additions & 16 deletions src/commands/self_timeout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre::Result;
use color_eyre::eyre::{eyre, Result};
use poise::serenity_prelude as serenity;

use crate::Context;
use crate::{utils::serenity::unique_username, Context};

/// Time yourself out for a specific duration
#[poise::command(rename = "self-timeout", slash_command, guild_only)]
Expand All @@ -22,20 +22,35 @@ pub async fn self_timeout(
.disable_communication_until_datetime(&ctx, end_serenity)
.await?;

ctx.send(
poise::CreateReply::default().embed(
serenity::CreateEmbed::default()
.title("Self-timeout in effect")
.field("Start", format!("<t:{0}:F>", start.timestamp()), false)
.field(
"End",
format!("<t:{0}:F> (<t:{0}:R>)", end.timestamp()),
false,
)
.color(0x4ade80),
),
)
.await?;
let resp_embed = serenity::CreateEmbed::default()
.title("Self-timeout in effect")
.field("Start", format!("<t:{0}:F>", start.timestamp()), false)
.field(
"End",
format!("<t:{0}:F> (<t:{0}:R>)", end.timestamp()),
false,
)
.color(0x4ade80);

ctx.send(poise::CreateReply::default().embed(resp_embed.clone()))
.await?;

if let Some(storage) = &ctx.data().storage {
if storage
.get_self_timeout_transparency(&ctx.author().id.to_string())
.await?
.unwrap_or(false)
{
let resp_embed = resp_embed.author(
serenity::CreateEmbedAuthor::new(unique_username(ctx.author()))
.icon_url(ctx.author().face()),
);

ctx.channel_id()
.send_message(&ctx, serenity::CreateMessage::default().embed(resp_embed))
.await?;
};
}
} else {
ctx.say("Error: Member unavailable!").await?;
};
Expand All @@ -45,3 +60,43 @@ pub async fn self_timeout(

Ok(())
}

/// Configure self-timeout transparency
#[poise::command(
rename = "self-timeout-transparency",
slash_command,
guild_only,
ephemeral
)]
pub async fn transparency(
ctx: Context<'_>,
#[description = "Whether transparency is on or off"] status: bool,
) -> Result<()> {
let storage = ctx
.data()
.storage
.as_ref()
.ok_or_else(|| eyre!("storage is not available for the transparency feature"))?;

storage
.set_self_timeout_transparency(&ctx.author().id.to_string(), &status)
.await?;

let desc = if status {
"Your self-timeouts will now be publicly logged to the channel that you ran the self-timeout in."
} else {
"Your self-timeouts will no longer be publicly logged."
};

ctx.send(
poise::CreateReply::default().embed(
serenity::CreateEmbed::default()
.title("Self-timeout transparency updated!")
.description(desc)
.color(0x4ade80),
),
)
.await?;

Ok(())
}
4 changes: 3 additions & 1 deletion src/starboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use poise::serenity_prelude as serenity;
use color_eyre::eyre::{OptionExt, Result};
use log::debug;

use crate::utils::serenity::unique_username;

fn channel_from_env(key: &str) -> Option<serenity::ChannelId> {
env::var(key)
.ok()
Expand Down Expand Up @@ -104,7 +106,7 @@ async fn make_message_embed(
content
})
.author(
serenity::CreateEmbedAuthor::new(message.author.name.to_string())
serenity::CreateEmbedAuthor::new(unique_username(&message.author))
.icon_url(message.author.face()),
)
.timestamp(message.timestamp);
Expand Down
1 change: 1 addition & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@ macro_rules! impl_storage {
impl Storage {
impl_storage!(presence, "presence-v1", presence::PresenceData);
impl_storage!(starboard, "starboard-v1:{}", String, ttl = 2629746, message_id: &str);
impl_storage!(self_timeout_transparency, "stt-v1:{}", bool, user_id: &str);
}
File renamed without changes.
5 changes: 3 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod actix_utils;
pub mod actix;
pub mod error_handling;
pub mod pluralize;
pub mod serenity;

mod pluralize;
pub use pluralize::Pluralize;
10 changes: 10 additions & 0 deletions src/utils/serenity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use poise::serenity_prelude::User;

pub fn unique_username(user: &User) -> String {
let mut ret = user.name.clone();
if let Some(discrim) = user.discriminator {
ret.push_str(&format!("#{discrim}"));
}

ret
}

0 comments on commit 3d42267

Please sign in to comment.