Skip to content

Commit 5675c8c

Browse files
committed
Reorganize alittle. Add ban help command, only bans applied using the
?ban command are temporary. This means that right clicking on a user and banning them will still perma-ban them.
1 parent ef941f9 commit 5675c8c

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

src/api.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::{commands::{Args, Result}, db::DB, schema::roles, ban};
1+
use crate::{
2+
commands::{Args, Result},
3+
db::DB,
4+
schema::roles,
5+
};
26
use diesel::prelude::*;
37
use serenity::{model::prelude::*, utils::parse_username};
48

@@ -91,33 +95,3 @@ pub(crate) fn kick(args: Args) -> Result<()> {
9195
}
9296
Ok(())
9397
}
94-
95-
/// Ban an user from the guild.
96-
///
97-
/// Requires the ban members permission
98-
pub(crate) fn ban(args: Args) -> Result<()> {
99-
if is_mod(&args)? {
100-
let user_id = parse_username(
101-
&args
102-
.params
103-
.get("user")
104-
.ok_or("unable to retrieve user param")?,
105-
)
106-
.ok_or("unable to retrieve user id")?;
107-
108-
use std::str::FromStr;
109-
110-
let hours = u64::from_str(
111-
args.params
112-
.get("hours")
113-
.ok_or("unable to retrieve hours param")?,
114-
)?;
115-
116-
if let Some(guild) = args.msg.guild(&args.cx) {
117-
info!("Banning user from guild");
118-
guild.read().ban(args.cx, UserId::from(user_id), &"all")?;
119-
ban::save_ban(format!("{}", user_id), format!("{}", guild.read().id), hours)?;
120-
}
121-
}
122-
Ok(())
123-
}

src/ban.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::{commands::Result, db::DB, schema::bans};
1+
use crate::{api, commands::{Result, Args}, db::DB, schema::bans};
22
use diesel::prelude::*;
3-
use serenity::{model::prelude::*, prelude::*};
3+
use serenity::{prelude::*, model::prelude::*, utils::parse_username};
44
use std::{
55
sync::atomic::{AtomicBool, Ordering},
66
thread::sleep,
@@ -69,3 +69,46 @@ pub(crate) fn start_unban_thread(cx: Context) {
6969
});
7070
}
7171
}
72+
73+
/// Temporarily ban an user from the guild.
74+
///
75+
/// Requires the ban members permission
76+
pub(crate) fn temp_ban(args: Args) -> Result<()> {
77+
if api::is_mod(&args)? {
78+
let user_id = parse_username(
79+
&args
80+
.params
81+
.get("user")
82+
.ok_or("unable to retrieve user param")?,
83+
)
84+
.ok_or("unable to retrieve user id")?;
85+
86+
use std::str::FromStr;
87+
88+
let hours = u64::from_str(
89+
args.params
90+
.get("hours")
91+
.ok_or("unable to retrieve hours param")?,
92+
)?;
93+
94+
if let Some(guild) = args.msg.guild(&args.cx) {
95+
info!("Banning user from guild");
96+
guild.read().ban(args.cx, UserId::from(user_id), &"all")?;
97+
save_ban(
98+
format!("{}", user_id),
99+
format!("{}", guild.read().id),
100+
hours,
101+
)?;
102+
}
103+
}
104+
Ok(())
105+
}
106+
107+
pub(crate) fn help(args: Args) -> Result<()> {
108+
let help_string = "ban a user for a temporary amount of time
109+
```
110+
?ban {user} {hours}
111+
```";
112+
api::send_reply(&args, &help_string)?;
113+
Ok(())
114+
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ fn app() -> Result {
112112
cmds.add("?kick {user}", api::kick);
113113

114114
// Ban
115-
cmds.add("?ban {user} {hours}", api::ban);
115+
cmds.add("?ban help", ban::help);
116+
cmds.add("?ban {user} {hours}", ban::temp_ban);
116117

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

0 commit comments

Comments
 (0)