Commit 4dd4f44b authored by spookydonut's avatar spookydonut

cleanup macros and reduce code duplication

parent 0919ef79
Pipeline #184 failed with stages
......@@ -17,26 +17,18 @@ use serenity::{
utils::MessageBuilder,
};
#[macro_use]
mod util;
mod config;
mod reaction_roles;
mod token_management;
mod user_management;
mod util;
mod voting;
use config::CONFIG;
use reaction_roles::{add_role_by_reaction, remove_role_by_reaction};
use util::get_string_from_react;
macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => error!($error, why),
}
};
}
struct Handler;
impl EventHandler for Handler {
......@@ -51,30 +43,17 @@ impl EventHandler for Handler {
}
let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect();
match message_content[0] {
"say" => {
println!("{:#?}", msg.content);
}
"say" => println!("{:#?}", msg.content),
"register" => user_management::Commands::register(ctx, msg.clone(), message_content[1]),
"verify" => user_management::Commands::verify(ctx, msg.clone(), message_content[1]),
"move" => {
voting::Commands::move_something(ctx, msg.clone(), message_content[1]);
}
"motion" => {
voting::Commands::motion(ctx, msg.clone(), message_content[1]);
}
"poll" => {
voting::Commands::poll(ctx, msg.clone(), message_content[1]);
}
"cowsay" => {
voting::Commands::cowsay(ctx, msg.clone(), message_content[1]);
}
"move" => voting::Commands::move_something(ctx, msg.clone(), message_content[1]),
"motion" => voting::Commands::motion(ctx, msg.clone(), message_content[1]),
"poll" => voting::Commands::poll(ctx, msg.clone(), message_content[1]),
"cowsay" => voting::Commands::cowsay(ctx, msg.clone(), message_content[1]),
"logreact" => {
e!("Error deleting logreact prompt: {:?}", msg.delete(&ctx));
e!(
"Error sending message {:?}",
msg.channel_id
.say(&ctx.http, "React to this to log the ID (for the next 5min)")
)
send_message!(msg.channel_id, &ctx.http,
"React to this to log the ID (for the next 5min)");
}
"help" => {
let mut message = MessageBuilder::new();
......@@ -86,40 +65,25 @@ impl EventHandler for Handler {
"Use {}poll <proposal> to see what people think about something",
&CONFIG.command_prefix
));
e!(
"Error sending message: {:?}",
msg.channel_id.say(&ctx.http, message.build())
);
}
_ => {
e!(
"Error sending message: {:?}",
msg.channel_id.say(
&ctx.http,
format!("Unrecognised command. Try {}help", &CONFIG.command_prefix)
)
);
}
send_message!(msg.channel_id, &ctx.http, message.build());
},
_ => send_message!(msg.channel_id, &ctx.http,
format!("Unrecognised command. Try {}help", &CONFIG.command_prefix)),
}
}
fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) {
match add_reaction.message(&ctx.http) {
Ok(message) => {
let message_type = get_message_type(&message);
if message_type == MessageType::RoleReactMessage
&& add_reaction.user_id.0 != CONFIG.bot_id
{
add_role_by_reaction(&ctx, message, add_reaction);
return;
}
if message.author.id.0 != CONFIG.bot_id || add_reaction.user_id == CONFIG.bot_id {
return;
}
match message_type {
MessageType::Motion => {
voting::reaction_add(ctx, add_reaction);
}
match get_message_type(&message) {
MessageType::RoleReactMessage if add_reaction.user_id.0 != CONFIG.bot_id => {
add_role_by_reaction(&ctx, message, add_reaction);
return
},
_ if message.author.id.0 != CONFIG.bot_id || add_reaction.user_id == CONFIG.bot_id => {
return;
},
MessageType::Motion => voting::reaction_add(ctx, add_reaction),
MessageType::LogReact => {
let react_user = add_reaction.user(&ctx).unwrap();
let react_as_string = get_string_from_react(&add_reaction.emoji);
......@@ -141,14 +105,11 @@ impl EventHandler for Handler {
add_reaction.emoji,
));
msg.push_mono(react_as_string);
e!(
"Error sending message: {:?}",
message.channel_id.say(&ctx.http, msg.build())
);
}
_ => {}
send_message!(message.channel_id, &ctx.http, msg.build());
},
_ => {},
}
}
},
Err(why) => error!("Failed to get react message {:?}", why),
}
}
......@@ -156,21 +117,18 @@ impl EventHandler for Handler {
fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) {
match removed_reaction.message(&ctx.http) {
Ok(message) => {
let message_type = get_message_type(&message);
if message_type == MessageType::RoleReactMessage
&& removed_reaction.user_id != CONFIG.bot_id
{
remove_role_by_reaction(&ctx, message, removed_reaction);
return;
}
if message.author.id.0 != CONFIG.bot_id || removed_reaction.user_id == CONFIG.bot_id
{
return;
match get_message_type(&message) {
MessageType::RoleReactMessage if removed_reaction.user_id != CONFIG.bot_id => {
remove_role_by_reaction(&ctx, message, removed_reaction);
return
},
_ if message.author.id.0 != CONFIG.bot_id || removed_reaction.user_id == CONFIG.bot_id => {
return;
},
MessageType::Motion => voting::reaction_remove(ctx, removed_reaction),
_ => {},
}
if message_type == MessageType::Motion {
voting::reaction_remove(ctx, removed_reaction);
}
}
},
Err(why) => error!("Failed to get react message {:?}", why),
}
}
......
......@@ -8,15 +8,6 @@ use serenity::{
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => error!($error, why),
}
};
}
pub fn add_role_by_reaction(ctx: &Context, msg: Message, added_reaction: Reaction) {
let user = added_reaction
.user_id
......
......@@ -8,15 +8,6 @@ use serenity::{
use crate::config::CONFIG;
use crate::token_management::*;
macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => error!($error, why),
}
};
}
pub fn new_member(ctx: &Context, mut new_member: Member) {
let mut message = MessageBuilder::new();
message.push("Nice to see you here ");
......@@ -24,16 +15,12 @@ pub fn new_member(ctx: &Context, mut new_member: Member) {
message.push_line("! Would you care to introduce yourself?");
message.push_line("If you're not sure where to start, perhaps you could tell us about your projects, your first computer…");
message.push_line("You should also know that we follow the Freenode Channel Guidelines: https://freenode.net/changuide, and try to avoid defamatory content");
if let Err(why) = CONFIG.welcome_channel.say(&ctx, message.build()) {
error!("Error sending message: {:?}", why);
}
send_message!(CONFIG.welcome_channel, &ctx, message.build());
let mut message = MessageBuilder::new();
message.push(format!("Say hi to {} in ", new_member.display_name()));
message.mention(&CONFIG.welcome_channel);
if let Err(why) = CONFIG.main_channel.say(&ctx, message.build()) {
error!("Error sending message: {:?}", why);
}
send_message!(CONFIG.main_channel, &ctx, message.build());
if let Err(why) = new_member.add_role(&ctx.http, CONFIG.unregistered_member_role) {
error!("Error adding user role: {:?}", why);
......@@ -53,19 +40,15 @@ pub struct Commands;
impl Commands {
pub fn register(ctx: Context, msg: Message, account_name: &str) {
if account_name.is_empty() {
e!(
"Error sending message: {:?}",
msg.channel_id
.say(&ctx.http, "Usage: !register <ucc username>")
);
send_message!(msg.channel_id, &ctx.http, "Usage: !register <ucc username>");
return;
}
e!(
"Error sending message: {:?}",
// TODO convert to email
msg.channel_id
.say(&ctx.http, format!("Hey {} here's that token you ordered: {}\nIf this wasn't you just ignore this.", account_name, generate_token(&msg.author, account_name)))
);
send_message!(
msg.channel_id,
&ctx.http,
format!("Hey {} here's that token you ordered: {}\nIf this wasn't you just ignore this.",
account_name,
generate_token(&msg.author, account_name)));
e!("Error deleting register message: {:?}", msg.delete(ctx));
}
pub fn verify(ctx: Context, msg: Message, token: &str) {
......@@ -102,11 +85,7 @@ impl Commands {
})
);
}
Err(reason) => e!(
"Error sending message: {:?}",
msg.channel_id
.say(&ctx.http, format!("Verification error: {:?}", reason))
),
Err(reason) => send_message!(msg.channel_id, &ctx.http, format!("Verification error: {:?}", reason)),
}
e!("Error deleting register message: {:?}", msg.delete(&ctx));
}
......
......@@ -21,3 +21,24 @@ pub fn get_react_from_string(string: String, guild: PartialGuild) -> ReactionTyp
|custom_emoji| ReactionType::from(custom_emoji.clone()),
)
}
#[macro_use]
macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => error!($error, why),
}
};
}
#[macro_use]
macro_rules! send_message {
($chan:expr, $context:expr, $message:expr) => {
match $chan
.say($context, $message) {
Ok(_) => (),
Err(why) => error!("Error sending message: {:?}", why),
}
};
}
......@@ -9,15 +9,6 @@ use std::sync::Mutex;
use crate::config::CONFIG;
use crate::util::get_string_from_react;
macro_rules! e {
($error: literal, $x:expr) => {
match $x {
Ok(_) => (),
Err(why) => error!($error, why),
}
};
}
pub struct Commands;
impl Commands {
pub fn move_something(ctx: Context, msg: Message, content: &str) {
......@@ -26,20 +17,12 @@ impl Commands {
create_motion(&ctx, &msg, motion);
return;
}
e!(
"Error sending message: {:?}",
msg.channel_id.say(
&ctx.http,
"If there's something you want to motion, put it after the !move keyword",
)
);
send_message!(msg.channel_id, &ctx.http,
"If there's something you want to motion, put it after the !move keyword");
}
pub fn motion(ctx: Context, msg: Message, _content: &str) {
e!("Error sending message: {:?}",
msg.channel_id.say(
&ctx.http,
"I hope you're not having a motion. You may have wanted to !move something instead."
));
send_message!(msg.channel_id, &ctx.http,
"I hope you're not having a motion. You may have wanted to !move something instead.");
}
pub fn poll(ctx: Context, msg: Message, content: &str) {
let topic = content;
......@@ -47,13 +30,8 @@ impl Commands {
create_poll(&ctx, &msg, topic);
return;
}
e!(
"Error sending message: {:?}",
msg.channel_id.say(
&ctx.http,
"If there's something you want to motion, put it after the !move keyword",
)
);
send_message!(msg.channel_id, &ctx.http,
"If there's something you want to motion, put it after the !move keyword");
}
pub fn cowsay(ctx: Context, msg: Message, content: &str) {
let output = if !content.trim().is_empty() {
......@@ -78,10 +56,7 @@ impl Commands {
String::from_utf8(output.stdout).expect("unable to parse stdout to String"),
None,
);
e!(
"Error sending message: {:?}",
msg.channel_id.say(&ctx.http, message.build())
);
send_message!(msg.channel_id, &ctx.http, message.build());
}
}
......@@ -284,9 +259,7 @@ fn update_motion(
message.push(" is now ");
message.push_bold(status);
message.push_italic(format!(" (was {})", last_status));
if let Err(why) = CONFIG.announcement_channel.say(&ctx.http, message.build()) {
error!("Error sending message: {:?}", why);
};
send_message!(CONFIG.announcement_channel, &ctx.http, message.build());
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment