Skip to content

Commit 8de9488

Browse files
committed
Send message to user when they have been banned
1 parent 2c3ebae commit 8de9488

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT"
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
11-
serenity = { version = "0.8.0", features = ["cache", "model"] }
11+
serenity = { version = "0.8.0", features = ["model"] }
1212
diesel = { version = "1.4.0", features = ["postgres", "r2d2"] }
1313
diesel_migrations = { version = "1.4.0", features = ["postgres"] }
1414
reqwest = { version = "0.10", features = ["blocking", "json"] }

src/ban.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
commands::{Args, Result},
44
db::DB,
55
schema::bans,
6+
text::ban_message,
67
};
78
use diesel::prelude::*;
89
use serenity::{model::prelude::*, prelude::*, utils::parse_username};
@@ -47,15 +48,13 @@ pub(crate) fn save_unban(user_id: String, guild_id: String) -> Result<()> {
4748
Ok(())
4849
}
4950

50-
type SendSyncError = Box<dyn std::error::Error + Send + Sync>;
51-
5251
pub(crate) fn start_unban_thread(cx: Context) {
5352
use std::str::FromStr;
5453
if !UNBAN_THREAD_INITIALIZED.load(Ordering::SeqCst) {
5554
UNBAN_THREAD_INITIALIZED.store(true, Ordering::SeqCst);
55+
type SendSyncError = Box<dyn std::error::Error + Send + Sync>;
5656
std::thread::spawn(move || -> std::result::Result<(), SendSyncError> {
5757
loop {
58-
sleep(Duration::new(HOUR, 0));
5958
let conn = DB.get()?;
6059
let to_unban = bans::table
6160
.filter(
@@ -70,6 +69,7 @@ pub(crate) fn start_unban_thread(cx: Context) {
7069
info!("Unbanning user {}", &row.1);
7170
guild_id.unban(&cx, u64::from_str(&row.1)?)?;
7271
}
72+
sleep(Duration::new(HOUR, 0));
7373
}
7474
});
7575
}
@@ -96,9 +96,20 @@ pub(crate) fn temp_ban(args: Args) -> Result<()> {
9696
.ok_or("unable to retrieve hours param")?,
9797
)?;
9898

99+
let reason = args
100+
.params
101+
.get("reason")
102+
.ok_or("unable to retrieve reason param")?;
103+
99104
if let Some(guild) = args.msg.guild(&args.cx) {
100105
info!("Banning user from guild");
101-
guild.read().ban(args.cx, UserId::from(user_id), &"all")?;
106+
let user = UserId::from(user_id);
107+
108+
user.create_dm_channel(args.cx)?
109+
.say(args.cx, ban_message(reason, hours))?;
110+
111+
guild.read().ban(args.cx, &user, &"all")?;
112+
102113
save_ban(
103114
format!("{}", user_id),
104115
format!("{}", guild.read().id),
@@ -110,10 +121,30 @@ pub(crate) fn temp_ban(args: Args) -> Result<()> {
110121
}
111122

112123
pub(crate) fn help(args: Args) -> Result<()> {
113-
let help_string = "ban a user for a temporary amount of time
124+
let hours = 24;
125+
let reason = "violating the code of conduct";
126+
127+
let help_string = format!(
128+
"
129+
Ban a user for a temporary amount of time
130+
```
131+
{command}
132+
```
133+
Example:
134+
```
135+
?ban @someuser {hours} {reason}
114136
```
115-
?ban {user} {hours}
116-
```";
137+
will ban a user for {hours} hours and send them the following message:
138+
```
139+
{user_message}
140+
```
141+
",
142+
command = "?ban {user} {hours} reason...",
143+
user_message = ban_message(reason, hours),
144+
hours = hours,
145+
reason = reason,
146+
);
147+
117148
api::send_reply(&args, &help_string)?;
118149
Ok(())
119150
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod db;
1515
mod schema;
1616
mod state_machine;
1717
mod tags;
18+
mod text;
1819
mod welcome;
1920

2021
use crate::db::DB;
@@ -113,7 +114,7 @@ fn app() -> Result {
113114

114115
// Ban
115116
cmds.add("?ban help", ban::help);
116-
cmds.add("?ban {user} {hours}", ban::temp_ban);
117+
cmds.add("?ban {user} {hours} reason...", ban::temp_ban);
117118

118119
// Post the welcome message to the welcome channel.
119120
cmds.add("?CoC {channel}", welcome::post_message);

src/text.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub(crate) const WELCOME_BILLBOARD: &'static str = "By participating in this community, you agree to follow the Rust Code of Conduct, as linked below. Please click the :white_check_mark: below to acknowledge and gain access to the channels.
2+
3+
https://www.rust-lang.org/policies/code-of-conduct ";
4+
5+
pub(crate) fn ban_message(reason: &str, hours: u64) -> String {
6+
format!("You have been banned from The Rust Programming Language discord server for {}. The ban will expire in {} hours. If you feel this action was taken unfairly, you can reach the Rust moderation team at [email protected]", reason, hours)
7+
}

src/welcome.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
commands::Args,
44
db::DB,
55
schema::{messages, roles, users},
6+
text::WELCOME_BILLBOARD,
67
Result,
78
};
89
use diesel::prelude::*;
@@ -12,10 +13,6 @@ use serenity::{model::prelude::*, prelude::*};
1213
pub(crate) fn post_message(args: Args) -> Result {
1314
use std::str::FromStr;
1415

15-
const WELCOME_BILLBOARD: &'static str = "By participating in this community, you agree to follow the Rust Code of Conduct, as linked below. Please click the :white_check_mark: below to acknowledge and gain access to the channels.
16-
17-
https://www.rust-lang.org/policies/code-of-conduct ";
18-
1916
if api::is_mod(&args)? {
2017
let channel_name = &args
2118
.params

0 commit comments

Comments
 (0)