Commit 48a78062 authored by Timothy du Heaume's avatar Timothy du Heaume

put all the config stuff in a struct

parent 84346539
./config.test.rs use serenity;
\ No newline at end of file
pub const CONFIG: UccbotConfig = UccbotConfig {
discord_token: include_str!("discord_token"),
server_id: 606351521117896704,
main_channel: serenity::model::id::ChannelId(606351521117896706),
welcome_channel: serenity::model::id::ChannelId(606351613816209418),
announcement_channel: serenity::model::id::ChannelId(606351521117896706),
bot_id: 607078903969742848,
vote_pool_size: 2,
vote_role: 607478818038480937,
tiebreaker_role: 607509283483025409,
unregistered_member_role: 608282247350714408,
registered_member_role: 608282133118582815,
command_prefix: "!",
for_vote: "👍",
against_vote: "👎",
abstain_vote: "🙊",
approve_react: "⬆",
disapprove_react: "⬇",
unsure_react: "❔",
};
pub struct UccbotConfig {
pub discord_token: &'static str,
pub server_id: u64,
// #general
pub main_channel: serenity::model::id::ChannelId,
// #the-corner
pub welcome_channel: serenity::model::id::ChannelId,
// #general
pub announcement_channel: serenity::model::id::ChannelId,
pub bot_id: u64,
pub vote_pool_size: i8,
pub vote_role: u64,
pub tiebreaker_role: u64,
pub unregistered_member_role: u64,
pub registered_member_role: u64,
pub command_prefix: &'static str,
pub for_vote: &'static str,
pub against_vote: &'static str,
pub abstain_vote: &'static str,
pub approve_react: &'static str,
pub disapprove_react: &'static str,
pub unsure_react: &'static str,
}
impl UccbotConfig {
pub fn allowed_reacts(&self) -> Vec<String> {
vec!(self.for_vote.to_string(),
self.against_vote.to_string(),
self.abstain_vote.to_string(),
self.approve_react.to_string(),
self.disapprove_react.to_string(),
self.unsure_react.to_string())
}
}
\ No newline at end of file
...@@ -17,6 +17,8 @@ mod config; ...@@ -17,6 +17,8 @@ mod config;
mod user_management; mod user_management;
mod voting; mod voting;
use config::CONFIG;
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
match $x { match $x {
...@@ -35,7 +37,7 @@ impl EventHandler for Handler { ...@@ -35,7 +37,7 @@ impl EventHandler for Handler {
// Event handlers are dispatched through a threadpool, and so multiple // Event handlers are dispatched through a threadpool, and so multiple
// events can be dispatched simultaneously. // events can be dispatched simultaneously.
fn message(&self, ctx: Context, msg: Message) { fn message(&self, ctx: Context, msg: Message) {
if !(msg.content.starts_with(config::COMMAND_PREFIX)) { if !(msg.content.starts_with(CONFIG.command_prefix)) {
return; return;
} }
let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect(); let message_content: Vec<_> = msg.content[1..].splitn(2, ' ').collect();
...@@ -63,11 +65,11 @@ impl EventHandler for Handler { ...@@ -63,11 +65,11 @@ impl EventHandler for Handler {
let mut message = MessageBuilder::new(); let mut message = MessageBuilder::new();
message.push_line(format!( message.push_line(format!(
"Use {}move <action> to make a circular motion", "Use {}move <action> to make a circular motion",
config::COMMAND_PREFIX &CONFIG.command_prefix
)); ));
message.push_line(format!( message.push_line(format!(
"Use {}poll <proposal> to see what people think about something", "Use {}poll <proposal> to see what people think about something",
config::COMMAND_PREFIX &CONFIG.command_prefix
)); ));
e!( e!(
"Error sending message: {:?}", "Error sending message: {:?}",
...@@ -79,7 +81,7 @@ impl EventHandler for Handler { ...@@ -79,7 +81,7 @@ impl EventHandler for Handler {
"Error sending message: {:?}", "Error sending message: {:?}",
msg.channel_id.say( msg.channel_id.say(
&ctx.http, &ctx.http,
format!("Unrecognised command. Try {}help", config::COMMAND_PREFIX) format!("Unrecognised command. Try {}help", &CONFIG.command_prefix)
) )
); );
} }
...@@ -89,7 +91,7 @@ impl EventHandler for Handler { ...@@ -89,7 +91,7 @@ impl EventHandler for Handler {
fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) { fn reaction_add(&self, ctx: Context, add_reaction: channel::Reaction) {
match add_reaction.message(&ctx.http) { match add_reaction.message(&ctx.http) {
Ok(message) => { Ok(message) => {
if message.author.id.0 != config::BOT_ID || add_reaction.user_id == config::BOT_ID { if message.author.id.0 != CONFIG.bot_id || add_reaction.user_id == CONFIG.bot_id {
return; return;
} }
match message_type(&message) { match message_type(&message) {
...@@ -106,7 +108,7 @@ impl EventHandler for Handler { ...@@ -106,7 +108,7 @@ impl EventHandler for Handler {
fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) { fn reaction_remove(&self, ctx: Context, removed_reaction: channel::Reaction) {
match removed_reaction.message(&ctx.http) { match removed_reaction.message(&ctx.http) {
Ok(message) => { Ok(message) => {
if message.author.id.0 != config::BOT_ID || removed_reaction.user_id == config::BOT_ID { if message.author.id.0 != CONFIG.bot_id || removed_reaction.user_id == CONFIG.bot_id {
return; return;
} }
match message_type(&message) { match message_type(&message) {
...@@ -150,8 +152,10 @@ fn main() { ...@@ -150,8 +152,10 @@ fn main() {
), ),
]) ])
.unwrap(); .unwrap();
// Configure the client with your Discord bot token in the environment. // Configure the client with your Discord bot token in the environment.
let token = config::DISCORD_TOKEN; let token = CONFIG.discord_token;
// Create a new instance of the Client, logging in as a bot. This will // Create a new instance of the Client, logging in as a bot. This will
// automatically prepend your bot token with "Bot ", which is a requirement // automatically prepend your bot token with "Bot ", which is a requirement
......
...@@ -5,7 +5,7 @@ use serenity::{ ...@@ -5,7 +5,7 @@ use serenity::{
utils::MessageBuilder, utils::MessageBuilder,
}; };
use crate::config; use crate::config::CONFIG;
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
...@@ -23,18 +23,18 @@ pub fn new_member(ctx: &Context, mut new_member: Member) { ...@@ -23,18 +23,18 @@ pub fn new_member(ctx: &Context, mut new_member: Member) {
message.push_line("! Would you care to introduce yourself?"); 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("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"); 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()) { if let Err(why) = CONFIG.welcome_channel.say(&ctx, message.build()) {
error!("Error sending message: {:?}", why); error!("Error sending message: {:?}", why);
} }
let mut message = MessageBuilder::new(); let mut message = MessageBuilder::new();
message.push(format!("Say hi to {} in ", new_member.display_name())); message.push(format!("Say hi to {} in ", new_member.display_name()));
message.mention(&config::WELCOME_CHANNEL); message.mention(&CONFIG.welcome_channel);
if let Err(why) = config::MAIN_CHANNEL.say(&ctx, message.build()) { if let Err(why) = CONFIG.main_channel.say(&ctx, message.build()) {
error!("Error sending message: {:?}", why); error!("Error sending message: {:?}", why);
} }
if let Err(why) = new_member.add_role(&ctx.http, config::UNREGISTERED_MEMBER_ROLE) { if let Err(why) = new_member.add_role(&ctx.http, CONFIG.unregistered_member_role) {
error!("Error adding user role: {:?}", why); error!("Error adding user role: {:?}", why);
}; };
} }
...@@ -44,7 +44,7 @@ impl Commands { ...@@ -44,7 +44,7 @@ impl Commands {
pub fn join(ctx: Context, msg: Message, _content: &str) { pub fn join(ctx: Context, msg: Message, _content: &str) {
e!( e!(
"Unable to get user: {:?}", "Unable to get user: {:?}",
serenity::model::id::GuildId(config::SERVER_ID) serenity::model::id::GuildId(CONFIG.server_id)
.member(ctx.http.clone(), msg.author.id) .member(ctx.http.clone(), msg.author.id)
.map(|member| new_member(&ctx, member)) .map(|member| new_member(&ctx, member))
); );
...@@ -61,12 +61,12 @@ impl Commands { ...@@ -61,12 +61,12 @@ impl Commands {
} }
e!( e!(
"Unable to get member: {:?}", "Unable to get member: {:?}",
serenity::model::id::GuildId(config::SERVER_ID) serenity::model::id::GuildId(CONFIG.server_id)
.member(ctx.http.clone(), msg.author.id) .member(ctx.http.clone(), msg.author.id)
.map(|mut member| { .map(|mut member| {
e!( e!(
"Unable to remove role: {:?}", "Unable to remove role: {:?}",
member.remove_role(&ctx.http, config::UNREGISTERED_MEMBER_ROLE) member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
); );
e!( e!(
"Unable to edit nickname: {:?}", "Unable to edit nickname: {:?}",
...@@ -90,7 +90,7 @@ impl Commands { ...@@ -90,7 +90,7 @@ impl Commands {
.map(|()| { .map(|()| {
e!( e!(
"Unable to add role: {:?}", "Unable to add role: {:?}",
member.add_role(&ctx.http, config::REGISTERED_MEMBER_ROLE) member.add_role(&ctx.http, CONFIG.registered_member_role)
); );
}) })
); );
......
...@@ -6,7 +6,7 @@ use serenity::{ ...@@ -6,7 +6,7 @@ use serenity::{
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use crate::config; use crate::config::CONFIG;
macro_rules! e { macro_rules! e {
($error: literal, $x:expr) => { ($error: literal, $x:expr) => {
...@@ -96,7 +96,7 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) { ...@@ -96,7 +96,7 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) {
embed.colour(serenity::utils::Colour::GOLD); embed.colour(serenity::utils::Colour::GOLD);
embed.title(format!("Motion to {}", topic)); embed.title(format!("Motion to {}", topic));
let mut desc = MessageBuilder::new(); let mut desc = MessageBuilder::new();
desc.role(config::VOTE_ROLE); desc.role(CONFIG.vote_role);
desc.push(" take a look at this motion from "); desc.push(" take a look at this motion from ");
desc.mention(&msg.author); desc.mention(&msg.author);
embed.description(desc.build()); embed.description(desc.build());
...@@ -106,11 +106,11 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) { ...@@ -106,11 +106,11 @@ fn create_motion(ctx: &Context, msg: &Message, topic: &str) {
embed embed
}); });
m.reactions(vec![ m.reactions(vec![
config::FOR_VOTE, CONFIG.for_vote.to_string(),
config::AGAINST_VOTE, CONFIG.against_vote.to_string(),
config::ABSTAIN_VOTE, CONFIG.abstain_vote.to_string(),
config::APPROVE_REACT, CONFIG.approve_react.to_string(),
config::DISAPPROVE_REACT, CONFIG.disapprove_react.to_string(),
]); ]);
m m
}) { }) {
...@@ -144,9 +144,9 @@ fn create_poll(ctx: &Context, msg: &Message, topic: &str) { ...@@ -144,9 +144,9 @@ fn create_poll(ctx: &Context, msg: &Message, topic: &str) {
embed embed
}); });
m.reactions(vec![ m.reactions(vec![
config::APPROVE_REACT, CONFIG.approve_react.to_string(),
config::DISAPPROVE_REACT, CONFIG.disapprove_react.to_string(),
config::UNSURE_REACT, CONFIG.unsure_react.to_string(),
]); ]);
m m
}) { }) {
...@@ -179,18 +179,18 @@ fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo { ...@@ -179,18 +179,18 @@ fn get_cached_motion(ctx: &Context, msg: &Message) -> MotionInfo {
votes: { votes: {
let mut m = HashMap::new(); let mut m = HashMap::new();
m.insert( m.insert(
config::FOR_VOTE, CONFIG.for_vote,
msg.reaction_users(ctx, config::FOR_VOTE, None, None) msg.reaction_users(ctx, CONFIG.for_vote, None, None)
.unwrap(), .unwrap(),
); );
m.insert( m.insert(
config::AGAINST_VOTE, CONFIG.against_vote,
msg.reaction_users(ctx, config::AGAINST_VOTE, None, None) msg.reaction_users(ctx, CONFIG.against_vote, None, None)
.unwrap(), .unwrap(),
); );
m.insert( m.insert(
config::ABSTAIN_VOTE, CONFIG.abstain_vote,
msg.reaction_users(ctx, config::ABSTAIN_VOTE, None, None) msg.reaction_users(ctx, CONFIG.abstain_vote, None, None)
.unwrap(), .unwrap(),
); );
m m
...@@ -217,31 +217,31 @@ fn update_motion( ...@@ -217,31 +217,31 @@ fn update_motion(
) { ) {
let motion_info: MotionInfo = get_cached_motion(ctx, msg); let motion_info: MotionInfo = get_cached_motion(ctx, msg);
let for_votes = motion_info.votes.get(config::FOR_VOTE).unwrap().len() as isize - 1; let for_votes = motion_info.votes.get(&CONFIG.for_vote).unwrap().len() as isize - 1;
let against_votes = motion_info.votes.get(config::AGAINST_VOTE).unwrap().len() as isize - 1; let against_votes = motion_info.votes.get(&CONFIG.against_vote).unwrap().len() as isize - 1;
let abstain_votes = motion_info.votes.get(config::ABSTAIN_VOTE).unwrap().len() as isize - 1; let abstain_votes = motion_info.votes.get(&CONFIG.abstain_vote).unwrap().len() as isize - 1;
let has_tiebreaker = |users: &Vec<serenity::model::user::User>| { let has_tiebreaker = |users: &Vec<serenity::model::user::User>| {
users.iter().any(|u| { users.iter().any(|u| {
u.has_role(ctx, config::SERVER_ID, config::TIEBREAKER_ROLE) u.has_role(ctx, CONFIG.server_id, CONFIG.tiebreaker_role)
.unwrap() .unwrap()
}) })
}; };
let for_strength = for_votes as f32 let for_strength = for_votes as f32
+ (if has_tiebreaker(motion_info.votes.get(config::FOR_VOTE).unwrap()) { + (if has_tiebreaker(motion_info.votes.get(&CONFIG.for_vote).unwrap()) {
0.25 0.25
} else { } else {
0.0 0.0
}); });
let against_strength = against_votes as f32 let against_strength = against_votes as f32
+ (if has_tiebreaker(motion_info.votes.get(config::AGAINST_VOTE).unwrap()) { + (if has_tiebreaker(motion_info.votes.get(&CONFIG.against_vote).unwrap()) {
0.25 0.25
} else { } else {
0.0 0.0
}); });
let abstain_strength = abstain_votes as f32 let abstain_strength = abstain_votes as f32
+ (if has_tiebreaker(motion_info.votes.get(config::ABSTAIN_VOTE).unwrap()) { + (if has_tiebreaker(motion_info.votes.get(&CONFIG.abstain_vote).unwrap()) {
0.25 0.25
} else { } else {
0.0 0.0
...@@ -278,7 +278,7 @@ fn update_motion( ...@@ -278,7 +278,7 @@ fn update_motion(
message.push(" is now "); message.push(" is now ");
message.push_bold(status); message.push_bold(status);
message.push_italic(format!(" (was {})", last_status)); message.push_italic(format!(" (was {})", last_status));
if let Err(why) = config::ANNOUNCEMENT_CHANNEL.say(&ctx.http, message.build()) { if let Err(why) = CONFIG.announcement_channel.say(&ctx.http, message.build()) {
error!("Error sending message: {:?}", why); error!("Error sending message: {:?}", why);
}; };
} }
...@@ -306,10 +306,10 @@ fn update_motion( ...@@ -306,10 +306,10 @@ fn update_motion(
.expect("No previous status") .expect("No previous status")
.clone() .clone()
.value; .value;
if for_strength > (config::VOTE_POOL_SIZE as f32 / 2.0) { if for_strength > (CONFIG.vote_pool_size as f32 / 2.0) {
e.colour(serenity::utils::Colour::TEAL); e.colour(serenity::utils::Colour::TEAL);
update_status(e, "Passed", last_status_full, &topic); update_status(e, "Passed", last_status_full, &topic);
} else if against_strength + abstain_strength > (config::VOTE_POOL_SIZE as f32 / 2.0) { } else if against_strength + abstain_strength > (CONFIG.vote_pool_size as f32 / 2.0) {
e.colour(serenity::utils::Colour::RED); e.colour(serenity::utils::Colour::RED);
update_status(e, "Failed", last_status_full, &topic); update_status(e, "Failed", last_status_full, &topic);
} else { } else {
...@@ -320,7 +320,7 @@ fn update_motion( ...@@ -320,7 +320,7 @@ fn update_motion(
format!( format!(
"Votes ({}/{})", "Votes ({}/{})",
for_votes + against_votes + abstain_votes, for_votes + against_votes + abstain_votes,
config::VOTE_POOL_SIZE CONFIG.vote_pool_size
), ),
format!( format!(
"For: {}\nAgainst: {}\nAbstain: {}", "For: {}\nAgainst: {}\nAbstain: {}",
...@@ -344,10 +344,10 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -344,10 +344,10 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
match add_reaction.message(&ctx.http) { match add_reaction.message(&ctx.http) {
Ok(mut message) => { Ok(mut message) => {
if let Ok(user) = add_reaction.user(&ctx) { if let Ok(user) = add_reaction.user(&ctx) {
match user.has_role(&ctx, config::SERVER_ID, config::VOTE_ROLE) { match user.has_role(&ctx, CONFIG.server_id, CONFIG.vote_role) {
Ok(true) => { Ok(true) => {
// remove vote if already voted // remove vote if already voted
for react in [config::FOR_VOTE, config::AGAINST_VOTE, config::ABSTAIN_VOTE] for react in [CONFIG.for_vote, CONFIG.against_vote, CONFIG.abstain_vote]
.iter() .iter()
.filter(|r| r != &&add_reaction.emoji.as_data().as_str()) .filter(|r| r != &&add_reaction.emoji.as_data().as_str())
{ {
...@@ -362,7 +362,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -362,7 +362,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
} }
} }
// remove 'illegal' reacts // remove 'illegal' reacts
if !config::ALLOWED_REACTS.contains(&add_reaction.emoji.as_data().as_str()) if !CONFIG.allowed_reacts().contains(&add_reaction.emoji.as_data())
{ {
if let Err(why) = add_reaction.delete(&ctx) { if let Err(why) = add_reaction.delete(&ctx) {
error!("Error deleting react: {:?}", why); error!("Error deleting react: {:?}", why);
...@@ -382,7 +382,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) { ...@@ -382,7 +382,7 @@ pub fn reaction_add(ctx: Context, add_reaction: channel::Reaction) {
update_motion(&ctx, &mut message, &user, "add", add_reaction); update_motion(&ctx, &mut message, &user, "add", add_reaction);
} }
Ok(false) => { Ok(false) => {
if ![config::APPROVE_REACT, config::DISAPPROVE_REACT] if ![CONFIG.approve_react, CONFIG.disapprove_react]
.contains(&add_reaction.emoji.as_data().as_str()) .contains(&add_reaction.emoji.as_data().as_str())
{ {
if let Err(why) = add_reaction.delete(&ctx) { if let Err(why) = add_reaction.delete(&ctx) {
......
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