Skip to content

Commit 16c0305

Browse files
authored
Merge pull request #36 from rust-lang/cleanup
Code Cleanup
2 parents 29d5acb + 55b5ecc commit 16c0305

File tree

3 files changed

+163
-167
lines changed

3 files changed

+163
-167
lines changed

src/api.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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};
66

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

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

src/main.rs

Lines changed: 32 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ mod api;
1111
mod commands;
1212
mod crates;
1313
mod db;
14-
mod events;
1514
mod schema;
1615
mod state_machine;
1716
mod tags;
17+
mod welcome;
1818

1919
use crate::db::DB;
20-
use crate::schema::{messages, roles, users};
2120
use commands::{Args, Commands};
2221
use diesel::prelude::*;
23-
use events::{EventDispatcher, MessageDispatcher};
24-
use serenity::{model::prelude::*, utils::parse_username, Client};
25-
use std::str::FromStr;
22+
use serenity::{model::prelude::*, prelude::*};
2623

27-
type Result = crate::commands::Result<()>;
24+
pub(crate) type Result = crate::commands::Result<()>;
2825

2926
fn init_data() -> Result {
27+
use crate::schema::roles;
3028
info!("Loading data into database");
3129
let mod_role = std::env::var("MOD_ID").map_err(|_| "MOD_ID env var not found")?;
3230
let talk_role = std::env::var("TALK_ID").map_err(|_| "TALK_ID env var not found")?;
@@ -88,16 +86,16 @@ fn app() -> Result {
8886

8987
// Slow mode.
9088
// 0 seconds disables slowmode
91-
cmds.add("?slowmode {channel} {seconds}", slow_mode);
89+
cmds.add("?slowmode {channel} {seconds}", api::slow_mode);
9290

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

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

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

102100
let menu = cmds.menu().unwrap();
103101

@@ -106,10 +104,11 @@ fn app() -> Result {
106104
Ok(())
107105
});
108106

109-
let messages = MessageDispatcher::new(cmds);
110-
111-
let mut client =
112-
Client::new_with_handlers(&token, Some(messages), Some(EventDispatcher)).unwrap();
107+
let mut client = Client::new_with_extras(&token, |e| {
108+
e.event_handler(Messages { cmds });
109+
e.raw_event_handler(Events);
110+
e
111+
})?;
113112

114113
client.start()?;
115114

@@ -125,123 +124,31 @@ fn main() {
125124
}
126125
}
127126

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))?;
127+
struct Events;
128+
129+
impl RawEventHandler for Events {
130+
fn raw_event(&self, cx: Context, event: Event) {
131+
match event {
132+
Event::ReactionAdd(ref ev) => {
133+
if let Err(e) = welcome::assign_talk_role(&cx, ev) {
134+
println!("{}", e);
135+
}
136+
}
137+
_ => (),
138+
}
146139
}
147-
Ok(())
148140
}
149141

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(())
142+
struct Messages {
143+
cmds: Commands,
169144
}
170145

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-
}
146+
impl EventHandler for Messages {
147+
fn message(&self, cx: Context, msg: Message) {
148+
self.cmds.execute(cx, msg);
188149
}
189-
Ok(())
190-
}
191150

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)?;
151+
fn ready(&self, _: Context, ready: Ready) {
152+
info!("{} connected to discord", ready.user.name);
245153
}
246-
Ok(())
247154
}

0 commit comments

Comments
 (0)