Block/allow DM reminders

Only affects slash commands but this is sort of a non-issue post September
This commit is contained in:
jude
2022-07-29 19:22:15 +01:00
parent 2781f2923e
commit 7b6e967a5d
10 changed files with 102 additions and 111 deletions

View File

@ -49,6 +49,7 @@ __Todo Commands__
__Setup Commands__
`/timezone` - Set your timezone (necessary for `/remind` to work properly)
`/dm allow/block` - Change your DM settings for reminders.
__Advanced Commands__
`/macro` - Record and replay command sequences

View File

@ -124,6 +124,52 @@ You may want to use one of the popular timezones below, otherwise click [here](h
Ok(())
}
/// Configure whether other users can set reminders to your direct messages
#[poise::command(slash_command, rename = "dm", identifying_name = "allowed_dm")]
pub async fn allowed_dm(_ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}
/// Allow other users to set reminders in your direct messages
#[poise::command(slash_command, rename = "allow", identifying_name = "allowed_dm")]
pub async fn set_allowed_dm(ctx: Context<'_>) -> Result<(), Error> {
let mut user_data = ctx.author_data().await?;
user_data.allowed_dm = true;
user_data.commit_changes(&ctx.data().database).await;
ctx.send(|r| {
r.ephemeral(true).embed(|e| {
e.title("DMs permitted")
.description("You will receive a message if a user sets a DM reminder for you.")
.color(*THEME_COLOR)
})
})
.await?;
Ok(())
}
/// Block other users from setting reminders in your direct messages
#[poise::command(slash_command, rename = "block", identifying_name = "allowed_dm")]
pub async fn unset_allowed_dm(ctx: Context<'_>) -> Result<(), Error> {
let mut user_data = ctx.author_data().await?;
user_data.allowed_dm = false;
user_data.commit_changes(&ctx.data().database).await;
ctx.send(|r| {
r.ephemeral(true).embed(|e| {
e.title("DMs blocked")
.description(
"You can still set DM reminders for yourself or for users with DMs enabled.",
)
.color(*THEME_COLOR)
})
})
.await?;
Ok(())
}
/// View the webhook being used to send reminders to this channel
#[poise::command(
slash_command,

View File

@ -694,6 +694,7 @@ pub async fn remind(
}
}
}
None => {
ctx.say("Time could not be processed").await?;
}

View File

@ -101,6 +101,13 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
info_cmds::clock_context_menu(),
info_cmds::dashboard(),
moderation_cmds::timezone(),
poise::Command {
subcommands: vec![
moderation_cmds::set_allowed_dm(),
moderation_cmds::unset_allowed_dm(),
],
..moderation_cmds::allowed_dm()
},
moderation_cmds::webhook(),
poise::Command {
subcommands: vec![

View File

@ -233,6 +233,10 @@ impl<'a> MultiReminderBuilder<'a> {
if let Some(guild_id) = self.guild_id {
if guild_id.member(&self.ctx.discord(), user).await.is_err() {
Err(ReminderError::InvalidTag)
} else if self.set_by.map_or(true, |i| i != user_data.id)
&& !user_data.allowed_dm
{
Err(ReminderError::UserBlockedDm)
} else {
Ok(user_data.dm_channel)
}

View File

@ -7,6 +7,7 @@ pub enum ReminderError {
PastTime,
ShortInterval,
InvalidTag,
UserBlockedDm,
DiscordError(String),
}
@ -30,6 +31,9 @@ impl ToString for ReminderError {
ReminderError::InvalidTag => {
"Couldn't find a location by your tag. Your tag must be either a channel or a user (not a role)".to_string()
}
ReminderError::UserBlockedDm => {
"User has DM reminders disabled".to_string()
}
ReminderError::DiscordError(s) => format!("A Discord error occurred: **{}**", s),
}
}

View File

@ -10,6 +10,7 @@ pub struct UserData {
pub user: u64,
pub dm_channel: u32,
pub timezone: String,
pub allowed_dm: bool,
}
impl UserData {
@ -46,7 +47,7 @@ SELECT timezone FROM users WHERE user = ?
match sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, dm_channel, IF(timezone IS NULL, ?, timezone) AS timezone FROM users WHERE user = ?
SELECT id, user, dm_channel, IF(timezone IS NULL, ?, timezone) AS timezone, allowed_dm FROM users WHERE user = ?
",
*LOCAL_TIMEZONE,
user_id.0
@ -83,7 +84,7 @@ INSERT INTO users (name, user, dm_channel, timezone) VALUES ('', ?, (SELECT id F
Ok(sqlx::query_as_unchecked!(
Self,
"
SELECT id, user, dm_channel, timezone FROM users WHERE user = ?
SELECT id, user, dm_channel, timezone, allowed_dm FROM users WHERE user = ?
",
user_id.0
)
@ -102,9 +103,10 @@ SELECT id, user, dm_channel, timezone FROM users WHERE user = ?
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE users SET timezone = ? WHERE id = ?
UPDATE users SET timezone = ?, allowed_dm = ? WHERE id = ?
",
self.timezone,
self.allowed_dm,
self.id
)
.execute(pool)