Skip to content

Commit

Permalink
add !poll command
Browse files Browse the repository at this point in the history
  • Loading branch information
timlathy committed Aug 4, 2020
1 parent 0da6663 commit 401a4bf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod chain;
mod img2msg;
mod joker;
mod poll;
mod taki;
mod wdyt;

pub use chain::Chain;
pub use img2msg::Img2msg;
pub use joker::Joker;
pub use poll::Poll;
pub use taki::Taki;
pub use wdyt::Wdyt;
55 changes: 55 additions & 0 deletions src/commands/poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::JoeResult;
use regex::Regex;
use serenity::{model::prelude::*, prelude::*, utils::MessageBuilder};
use std::fmt::Write;

pub struct Poll {
item_re: Regex,
}

impl Poll {
pub fn new() -> Self {
Self {
item_re: Regex::new(r#""([^"]+)""#).unwrap(),
}
}

pub fn handle_message(&mut self, ctx: &Context, msg: &Message) -> JoeResult<bool> {
if !msg.content.starts_with("!poll") {
return Ok(false);
}

let mut items = self.item_re.captures_iter(&msg.content).peekable();
match items.next() {
// topic + at least one option provided
Some(topic_cap) if items.peek().is_some() => {
let mut body = String::new();
let mut reactions = Vec::new();
for (i, item_cap) in items.enumerate() {
let reaction = std::char::from_u32(0x1f1e6 as u32 + i as u32)
.unwrap()
.to_string();
writeln!(&mut body, "{} {}", reaction, &item_cap[1])?;
reactions.push(ReactionType::Unicode(reaction));
}
msg.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title(&topic_cap[1]);
e.description(body);
e
});
m.reactions(reactions);
m
})?;
}
_ => {
let help = MessageBuilder::new()
.push_mono(r#"!poll "OUPOC" "AGA" "NE""#)
.build();
msg.channel_id.say(&ctx.http, help)?;
}
}

Ok(true)
}
}
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serenity::{model::prelude::*, prelude::*, utils::MessageBuilder};
struct MessageHandlers {
taki: commands::Taki,
chain: commands::Chain,
poll: commands::Poll,
joker: commands::Joker,
wdyt: commands::Wdyt,
img2msg: commands::Img2msg,
Expand Down Expand Up @@ -44,6 +45,10 @@ impl Handler {
if chain_result.map_err(|e| format!("Chain: {:?}", e))? {
return Ok(());
}
let poll_result = handlers.poll.handle_message(&ctx, &msg);
if poll_result.map_err(|e| format!("Poll: {:?}", e))? {
return Ok(());
}
let joker_result = handlers.joker.handle_message(&ctx, &msg);
if joker_result.map_err(|e| format!("Joker: {:?}", e))? {
return Ok(());
Expand All @@ -70,6 +75,8 @@ impl Handler {
.push_line(" — посплетничаем еще")
.push_mono("!mashupstars")
.push_line(" — поприветствуем жителей городка")
.push_mono("!poll")
.push_line(" — устроим честный суд")
.push_line("")
.push_underline_line("поговорим с джо:")
.push_mono_line("что думаешь об итмо и бонче")
Expand Down Expand Up @@ -133,13 +140,15 @@ fn init_handlers(channel_id: u64, redis: &storage::Redis) -> MessageHandlers {

let taki = commands::Taki::new(messages.clone(), channel_id, redis);
let chain = commands::Chain::new(chain_data);
let poll = commands::Poll::new();
let joker = commands::Joker::new(messages.clone()).unwrap();
let wdyt = commands::Wdyt::new(messages.clone()).unwrap();
let img2msg = commands::Img2msg::new(messages.clone()).unwrap();

MessageHandlers {
taki,
chain,
poll,
joker,
wdyt,
img2msg,
Expand Down

0 comments on commit 401a4bf

Please sign in to comment.