Skip to content

Commit 888e8f6

Browse files
committed
Cleanup some of the code by moving some of the features into the api
module.
1 parent 29d5acb commit 888e8f6

File tree

3 files changed

+144
-130
lines changed

3 files changed

+144
-130
lines changed

src/api.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::commands::{Args, Result};
22
use crate::db::DB;
33
use crate::schema::roles;
44
use diesel::prelude::*;
5-
use serenity::model::prelude::*;
5+
use serenity::{model::prelude::*, utils::parse_username};
6+
67

78
/// Send a reply to the channel the message was received on.
89
pub(crate) fn send_reply(args: &Args, message: &str) -> Result<()> {
@@ -48,3 +49,69 @@ pub(crate) fn is_wg_and_teams(args: &Args) -> Result<bool> {
4849

4950
check_permission(args, role.map(|(_, role_id, _)| role_id))
5051
}
52+
53+
/// Set slow mode for a channel.
54+
///
55+
/// A `seconds` value of 0 will disable slowmode
56+
pub(crate) fn slow_mode(args: Args) -> Result<()> {
57+
use std::str::FromStr;
58+
59+
if is_mod(&args)? {
60+
let seconds = &args
61+
.params
62+
.get("seconds")
63+
.ok_or("unable to retrieve seconds param")?
64+
.parse::<u64>()?;
65+
66+
let channel_name = &args
67+
.params
68+
.get("channel")
69+
.ok_or("unable to retrieve channel param")?;
70+
71+
info!("Applying slowmode to channel {}", &channel_name);
72+
ChannelId::from_str(channel_name)?.edit(&args.cx, |c| c.slow_mode_rate(*seconds))?;
73+
}
74+
Ok(())
75+
}
76+
77+
/// Kick a user from the guild.
78+
///
79+
/// Requires the kick members permission
80+
pub(crate) fn kick(args: Args) -> Result<()> {
81+
if is_mod(&args)? {
82+
let user_id = parse_username(
83+
&args
84+
.params
85+
.get("user")
86+
.ok_or("unable to retrieve user param")?,
87+
)
88+
.ok_or("unable to retrieve user id")?;
89+
90+
if let Some(guild) = args.msg.guild(&args.cx) {
91+
info!("Kicking user from guild");
92+
guild.read().kick(&args.cx, UserId::from(user_id))?
93+
}
94+
}
95+
Ok(())
96+
}
97+
98+
/// Ban an user from the guild.
99+
///
100+
/// Requires the ban members permission
101+
pub(crate) fn ban(args: Args) -> Result<()> {
102+
if is_mod(&args)? {
103+
let user_id = parse_username(
104+
&args
105+
.params
106+
.get("user")
107+
.ok_or("unable to retrieve user param")?,
108+
)
109+
.ok_or("unable to retrieve user id")?;
110+
111+
if let Some(guild) = args.msg.guild(&args.cx) {
112+
info!("Banning user from guild");
113+
guild.read().ban(args.cx, UserId::from(user_id), &"all")?
114+
}
115+
}
116+
Ok(())
117+
}

src/main.rs

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ mod events;
1515
mod schema;
1616
mod state_machine;
1717
mod tags;
18+
mod welcome;
1819

1920
use crate::db::DB;
20-
use crate::schema::{messages, roles, users};
2121
use commands::{Args, Commands};
2222
use diesel::prelude::*;
2323
use events::{EventDispatcher, MessageDispatcher};
24-
use serenity::{model::prelude::*, utils::parse_username, Client};
25-
use std::str::FromStr;
24+
use serenity::Client;
2625

27-
type Result = crate::commands::Result<()>;
26+
pub(crate) type Result = crate::commands::Result<()>;
2827

2928
fn init_data() -> Result {
29+
use crate::schema::roles;
3030
info!("Loading data into database");
3131
let mod_role = std::env::var("MOD_ID").map_err(|_| "MOD_ID env var not found")?;
3232
let talk_role = std::env::var("TALK_ID").map_err(|_| "TALK_ID env var not found")?;
@@ -88,16 +88,16 @@ fn app() -> Result {
8888

8989
// Slow mode.
9090
// 0 seconds disables slowmode
91-
cmds.add("?slowmode {channel} {seconds}", slow_mode);
91+
cmds.add("?slowmode {channel} {seconds}", api::slow_mode);
9292

9393
// Kick
94-
cmds.add("?kick {user}", kick);
94+
cmds.add("?kick {user}", api::kick);
9595

9696
// Ban
97-
cmds.add("?ban {user}", ban);
97+
cmds.add("?ban {user}", api::ban);
9898

9999
// Post the welcome message to the welcome channel.
100-
cmds.add("?CoC {channel}", welcome_message);
100+
cmds.add("?CoC {channel}", welcome::post_message);
101101

102102
let menu = cmds.menu().unwrap();
103103

@@ -124,124 +124,3 @@ fn main() {
124124
std::process::exit(1);
125125
}
126126
}
127-
128-
/// Set slow mode for a channel.
129-
///
130-
/// A `seconds` value of 0 will disable slowmode
131-
fn slow_mode(args: Args) -> Result {
132-
if api::is_mod(&args)? {
133-
let seconds = &args
134-
.params
135-
.get("seconds")
136-
.ok_or("unable to retrieve seconds param")?
137-
.parse::<u64>()?;
138-
139-
let channel_name = &args
140-
.params
141-
.get("channel")
142-
.ok_or("unable to retrieve channel param")?;
143-
144-
info!("Applying slowmode to channel {}", &channel_name);
145-
ChannelId::from_str(channel_name)?.edit(&args.cx, |c| c.slow_mode_rate(*seconds))?;
146-
}
147-
Ok(())
148-
}
149-
150-
/// Kick a user from the guild.
151-
///
152-
/// Requires the kick members permission
153-
fn kick(args: Args) -> Result {
154-
if api::is_mod(&args)? {
155-
let user_id = parse_username(
156-
&args
157-
.params
158-
.get("user")
159-
.ok_or("unable to retrieve user param")?,
160-
)
161-
.ok_or("unable to retrieve user id")?;
162-
163-
if let Some(guild) = args.msg.guild(&args.cx) {
164-
info!("Kicking user from guild");
165-
guild.read().kick(&args.cx, UserId::from(user_id))?
166-
}
167-
}
168-
Ok(())
169-
}
170-
171-
/// Ban an user from the guild.
172-
///
173-
/// Requires the ban members permission
174-
fn ban(args: Args) -> Result {
175-
if api::is_mod(&args)? {
176-
let user_id = parse_username(
177-
&args
178-
.params
179-
.get("user")
180-
.ok_or("unable to retrieve user param")?,
181-
)
182-
.ok_or("unable to retrieve user id")?;
183-
184-
if let Some(guild) = args.msg.guild(&args.cx) {
185-
info!("Banning user from guild");
186-
guild.read().ban(args.cx, UserId::from(user_id), &"all")?
187-
}
188-
}
189-
Ok(())
190-
}
191-
192-
/// Write the welcome message to the welcome channel.
193-
fn welcome_message(args: Args) -> Result {
194-
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.
195-
196-
https://www.rust-lang.org/policies/code-of-conduct ";
197-
198-
if api::is_mod(&args)? {
199-
let channel_name = &args
200-
.params
201-
.get("channel")
202-
.ok_or("unable to retrieve channel param")?;
203-
204-
let channel_id = ChannelId::from_str(channel_name)?;
205-
info!("Posting welcome message");
206-
let message = channel_id.say(&args.cx, WELCOME_BILLBOARD)?;
207-
let bot_id = &message.author.id;
208-
209-
let conn = DB.get()?;
210-
211-
let _ = conn
212-
.build_transaction()
213-
.read_write()
214-
.run::<_, Box<dyn std::error::Error>, _>(|| {
215-
let message_id = message.id.0.to_string();
216-
let channel_id = channel_id.0.to_string();
217-
218-
diesel::insert_into(messages::table)
219-
.values((
220-
messages::name.eq("welcome"),
221-
messages::message.eq(&message_id),
222-
messages::channel.eq(&channel_id),
223-
))
224-
.on_conflict(messages::name)
225-
.do_update()
226-
.set((
227-
messages::message.eq(&message_id),
228-
messages::channel.eq(&channel_id),
229-
))
230-
.execute(&conn)?;
231-
232-
let user_id = &bot_id.to_string();
233-
234-
diesel::insert_into(users::table)
235-
.values((users::user_id.eq(user_id), users::name.eq("me")))
236-
.on_conflict(users::name)
237-
.do_update()
238-
.set((users::name.eq("me"), users::user_id.eq(user_id)))
239-
.execute(&conn)?;
240-
Ok(())
241-
})?;
242-
243-
let white_check_mark = ReactionType::from("✅");
244-
message.react(args.cx, white_check_mark)?;
245-
}
246-
Ok(())
247-
}

src/welcome.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::{
2+
api,
3+
commands::Args,
4+
schema::{messages, users},
5+
db::DB,
6+
Result,
7+
};
8+
use diesel::prelude::*;
9+
use serenity::model::prelude::*;
10+
11+
/// Write the welcome message to the welcome channel.
12+
pub(crate) fn post_message(args: Args) -> Result {
13+
use std::str::FromStr;
14+
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+
19+
if api::is_mod(&args)? {
20+
let channel_name = &args
21+
.params
22+
.get("channel")
23+
.ok_or("unable to retrieve channel param")?;
24+
25+
let channel_id = ChannelId::from_str(channel_name)?;
26+
info!("Posting welcome message");
27+
let message = channel_id.say(&args.cx, WELCOME_BILLBOARD)?;
28+
let bot_id = &message.author.id;
29+
30+
let conn = DB.get()?;
31+
32+
let _ = conn
33+
.build_transaction()
34+
.read_write()
35+
.run::<_, Box<dyn std::error::Error>, _>(|| {
36+
let message_id = message.id.0.to_string();
37+
let channel_id = channel_id.0.to_string();
38+
39+
diesel::insert_into(messages::table)
40+
.values((
41+
messages::name.eq("welcome"),
42+
messages::message.eq(&message_id),
43+
messages::channel.eq(&channel_id),
44+
))
45+
.on_conflict(messages::name)
46+
.do_update()
47+
.set((
48+
messages::message.eq(&message_id),
49+
messages::channel.eq(&channel_id),
50+
))
51+
.execute(&conn)?;
52+
53+
let user_id = &bot_id.to_string();
54+
55+
diesel::insert_into(users::table)
56+
.values((users::user_id.eq(user_id), users::name.eq("me")))
57+
.on_conflict(users::name)
58+
.do_update()
59+
.set((users::name.eq("me"), users::user_id.eq(user_id)))
60+
.execute(&conn)?;
61+
Ok(())
62+
})?;
63+
64+
let white_check_mark = ReactionType::from("✅");
65+
message.react(args.cx, white_check_mark)?;
66+
}
67+
Ok(())
68+
}

0 commit comments

Comments
 (0)