Commit d29b59eb authored by tec's avatar tec

Auto-formatting

parent a54e4762
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate hex_literal;
#[macro_use]
extern crate log;
......@@ -15,14 +17,15 @@ use serenity::{
};
mod config;
mod reaction_roles;
mod token_management;
mod user_management;
mod voting;
mod util;
mod reaction_roles;
mod voting;
use config::CONFIG;
use util::get_string_from_react;
use reaction_roles::{add_role_by_reaction, remove_role_by_reaction};
use util::get_string_from_react;
macro_rules! e {
($error: literal, $x:expr) => {
......@@ -129,8 +132,7 @@ impl EventHandler for Handler {
}
info!(
"The react {} just added is {:?}",
react_user.name,
react_as_string
react_user.name, react_as_string
);
let mut msg = MessageBuilder::new();
msg.push_italic(react_user.name);
......@@ -234,11 +236,15 @@ enum MessageType {
RoleReactMessage,
LogReact,
Poll,
Misc
Misc,
}
fn get_message_type(message: &Message) -> MessageType {
if CONFIG.react_role_messages.iter().any(|rrm| rrm.message == message.id) {
if CONFIG
.react_role_messages
.iter()
.any(|rrm| rrm.message == message.id)
{
return MessageType::RoleReactMessage;
}
if message.embeds.len() <= 0 {
......
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use crate::config::CONFIG;
use crate::util::{get_react_from_string, get_string_from_react};
use serenity::{
client::Context,
model::{channel::Message, channel::Reaction, id::UserId},
client::Context
};
use crate::util::{get_string_from_react, get_react_from_string};
use crate::config::CONFIG;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
pub fn add_role_by_reaction(ctx: Context, msg: Message, added_reaction: Reaction) {
CONFIG.react_role_messages.iter().find(|rrm| rrm.message == msg.id).and_then(|reaction_mapping| {
let react_as_string = get_string_from_react(added_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
}).and_then(|role_id|{
return ctx.http.add_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()).ok();
});
CONFIG
.react_role_messages
.iter()
.find(|rrm| rrm.message == msg.id)
.and_then(|reaction_mapping| {
let react_as_string = get_string_from_react(added_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
})
.and_then(|role_id| {
return ctx
.http
.add_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64())
.ok();
});
}
pub fn remove_role_by_reaction(ctx: Context, msg: Message, removed_reaction: Reaction) {
CONFIG.react_role_messages.iter().find(|rrm| rrm.message == msg.id).and_then(|reaction_mapping| {
let react_as_string = get_string_from_react(removed_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
}).and_then(|role_id|{
return ctx.http.remove_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64()).ok();
});
CONFIG
.react_role_messages
.iter()
.find(|rrm| rrm.message == msg.id)
.and_then(|reaction_mapping| {
let react_as_string = get_string_from_react(removed_reaction.emoji);
return reaction_mapping.mapping.get(&react_as_string);
})
.and_then(|role_id| {
return ctx
.http
.remove_member_role(CONFIG.server_id, *msg.author.id.as_u64(), *role_id.as_u64())
.ok();
});
}
pub fn add_all_role_reactions(ctx: Context) {
......@@ -31,7 +47,10 @@ pub fn add_all_role_reactions(ctx: Context) {
// this method supports paging, but we probably don't need it since the server only has a couple of
// hundred members. the Reaction.users() method can apparently only retrieve 100 users at once, but
// this one seems to work fine when set to 1000 (I tried 10,000 but the api returned a 400)
let all_members = ctx.http.get_guild_members(CONFIG.server_id, Some(1000), None).unwrap();
let all_members = ctx
.http
.get_guild_members(CONFIG.server_id, Some(1000), None)
.unwrap();
for (message, mapping) in messages_with_role_mappings {
for (react, role) in mapping {
......@@ -39,7 +58,9 @@ pub fn add_all_role_reactions(ctx: Context) {
// to work fine when set to 255...
// TODO: proper pagination for the unlikely scenario that there are more than 100 (255?) reactions?
let reaction_type = get_react_from_string(react.clone(), guild.clone());
let reactors = message.reaction_users(ctx.http.clone(), reaction_type, Some(255), None).unwrap();
let reactors = message
.reaction_users(ctx.http.clone(), reaction_type, Some(255), None)
.unwrap();
let reactor_ids: HashSet<UserId> = HashSet::from_iter(reactors.iter().map(|r| r.id));
// this looks O(n!), but n will probably never be more than three digits, so maybe it's okay?
......@@ -56,14 +77,26 @@ pub fn add_all_role_reactions(ctx: Context) {
}
}
fn get_all_role_reaction_message(ctx: &Context) -> Vec<(Message, &'static HashMap<String, serenity::model::id::RoleId>)> {
fn get_all_role_reaction_message(
ctx: &Context,
) -> Vec<(
Message,
&'static HashMap<String, serenity::model::id::RoleId>,
)> {
let guild = ctx.http.get_guild(CONFIG.server_id).unwrap();
let channels = ctx.http.get_channels(*guild.id.as_u64()).unwrap();
return channels.iter().flat_map(|channel| {
let ctxx = ctx.clone();
// since we don't know which channels the messages are in, we check every combination of message and
// channel and ignore the bad matches using .ok() and .filter_map()
CONFIG.react_role_messages.iter().filter_map(move |rrm|
ctxx.http.get_message(*channel.id.as_u64(), *rrm.message.as_u64()).ok().map(|m| (m, &rrm.mapping)))
}).collect();
return channels
.iter()
.flat_map(|channel| {
let ctxx = ctx.clone();
// since we don't know which channels the messages are in, we check every combination of message and
// channel and ignore the bad matches using .ok() and .filter_map()
CONFIG.react_role_messages.iter().filter_map(move |rrm| {
ctxx.http
.get_message(*channel.id.as_u64(), *rrm.message.as_u64())
.ok()
.map(|m| (m, &rrm.mapping))
})
})
.collect();
}
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