mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-18 16:28:55 +00:00
Clean up voting
This commit is contained in:
parent
2a0398b2a0
commit
5f328829c5
1 changed files with 34 additions and 42 deletions
|
@ -30,11 +30,8 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let question = args.single::<String>()?;
|
let question = args.single::<String>()?;
|
||||||
let (choices, reactions) = if args.is_empty() {
|
let choices = if args.is_empty() {
|
||||||
(
|
vec![("😍", "Yes! 😍".to_owned()), ("🤢", "No! 🤢".to_owned())]
|
||||||
vec!["Yes! 😍".to_owned(), "No! 🤢".to_owned()],
|
|
||||||
vec!["😍", "🤢"],
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
let choices: Vec<_> = args.iter().map(|v| v.unwrap()).collect();
|
let choices: Vec<_> = args.iter().map(|v| v.unwrap()).collect();
|
||||||
if choices.len() < 2 {
|
if choices.len() < 2 {
|
||||||
|
@ -57,14 +54,15 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let reactions = pick_n_reactions(choices.len())?;
|
pick_n_reactions(choices.len())?
|
||||||
(choices, reactions)
|
.into_iter()
|
||||||
|
.zip(choices.into_iter())
|
||||||
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
let fields: Vec<_> = {
|
let fields: Vec<_> = {
|
||||||
choices
|
choices
|
||||||
.iter()
|
.iter()
|
||||||
.zip(reactions.iter())
|
|
||||||
.map(|(choice, reaction)| {
|
.map(|(choice, reaction)| {
|
||||||
(
|
(
|
||||||
MessageBuilder::new().push_bold_safe(choice).build(),
|
MessageBuilder::new().push_bold_safe(choice).build(),
|
||||||
|
@ -92,12 +90,14 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
})?;
|
})?;
|
||||||
msg.delete(&ctx)?;
|
msg.delete(&ctx)?;
|
||||||
// React on all the choices
|
// React on all the choices
|
||||||
reactions.iter().try_for_each(|v| panel.react(&ctx, *v))?;
|
choices
|
||||||
|
.iter()
|
||||||
|
.try_for_each(|(v, _)| panel.react(&ctx, *v))?;
|
||||||
|
|
||||||
// Start sleeping
|
// Start sleeping
|
||||||
thread::sleep(duration.to_std()?);
|
thread::sleep(duration.to_std()?);
|
||||||
|
|
||||||
let result = collect_reactions(ctx, panel, &reactions, &choices)?;
|
let result = collect_reactions(ctx, &panel, &choices)?;
|
||||||
if result.len() == 0 {
|
if result.len() == 0 {
|
||||||
msg.reply(
|
msg.reply(
|
||||||
&ctx,
|
&ctx,
|
||||||
|
@ -136,7 +136,7 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
msg.delete(&ctx)?;
|
panel.delete(&ctx)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
// unimplemented!();
|
// unimplemented!();
|
||||||
|
@ -145,41 +145,33 @@ pub fn vote(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
// Collect reactions and store them as a map from choice to
|
// Collect reactions and store them as a map from choice to
|
||||||
fn collect_reactions<'a>(
|
fn collect_reactions<'a>(
|
||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
msg: Message,
|
msg: &Message,
|
||||||
reaction_emojis: &[&'static str],
|
choices: &'a [(&'static str, String)],
|
||||||
choices: &'a [String],
|
|
||||||
) -> Result<Vec<(&'a str, Vec<UserId>)>, Error> {
|
) -> Result<Vec<(&'a str, Vec<UserId>)>, Error> {
|
||||||
// Get a brand new version of the Message
|
// Get a brand new version of the Message
|
||||||
let reactions = msg.channel_id.message(&ctx, msg.id)?.reactions;
|
let reactions = msg.channel_id.message(&ctx, msg.id)?.reactions;
|
||||||
let reaction_to_choice: Map<_, _> = reaction_emojis
|
let reaction_to_choice: Map<_, _> = choices.into_iter().map(|r| (r.0, &r.1)).collect();
|
||||||
|
let mut vec: Vec<(&str, Vec<UserId>)> = Vec::new();
|
||||||
|
reactions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.zip(choices.into_iter())
|
.filter_map(|r| {
|
||||||
.collect();
|
if let ReactionType::Unicode(ref v) = r.reaction_type {
|
||||||
let result: Result<Vec<_>, Error> = {
|
reaction_to_choice
|
||||||
let mut vec: Vec<(&str, Vec<UserId>)> = Vec::new();
|
.get(&&v[..])
|
||||||
reactions
|
.cloned()
|
||||||
.into_iter()
|
.filter(|_| r.count > 1)
|
||||||
.filter_map(|r| {
|
.map(|choice| (r.clone(), choice))
|
||||||
if let ReactionType::Unicode(ref v) = r.reaction_type {
|
} else {
|
||||||
reaction_to_choice
|
None
|
||||||
.get(&&v[..])
|
}
|
||||||
.cloned()
|
})
|
||||||
.filter(|_| r.count > 1)
|
.try_for_each(|(r, choice)| -> Result<_, Error> {
|
||||||
.map(|choice| (r.clone(), choice))
|
let users = collect_reaction_users(ctx, &msg, &r)?;
|
||||||
} else {
|
vec.push((choice, users));
|
||||||
None
|
Ok(())
|
||||||
}
|
})?;
|
||||||
})
|
vec.sort_by(|(_, b): &(_, Vec<_>), (_, d)| d.len().cmp(&b.len()));
|
||||||
.try_for_each(|(r, choice)| -> Result<_, Error> {
|
Ok(vec)
|
||||||
let users = collect_reaction_users(ctx, &msg, &r)?;
|
|
||||||
vec.push((choice, users));
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
vec.sort_by(|(_, b): &(_, Vec<_>), (_, d)| d.len().cmp(&b.len()));
|
|
||||||
Ok(vec)
|
|
||||||
};
|
|
||||||
let result = result?;
|
|
||||||
Ok(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_reaction_users(
|
fn collect_reaction_users(
|
||||||
|
|
Loading…
Add table
Reference in a new issue