|
| 1 | +use regex::Regex; |
| 2 | +use scryfall::Card; |
| 3 | + |
| 4 | +use crate::prelude::*; |
| 5 | + |
| 6 | +pub struct ScryfallPlugin { |
| 7 | + re: Regex, |
| 8 | +} |
| 9 | + |
| 10 | +impl ScryfallPlugin { |
| 11 | + pub fn new() -> Self { |
| 12 | + ScryfallPlugin { |
| 13 | + re: Regex::new(r#"\[\[(.+?)\]\]"#).unwrap(), |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl ScryfallPlugin { |
| 19 | + async fn handle_scryfall(&self, ctx: &Arc<Context>, arg: &str) -> Result<()> { |
| 20 | + let card = Card::named_fuzzy(arg).await?; |
| 21 | + |
| 22 | + let card_uri = card.scryfall_uri; |
| 23 | + let image_uri = card.image_uris.and_then(|uris| uris.png); |
| 24 | + |
| 25 | + match image_uri { |
| 26 | + Some(image_uri) => { |
| 27 | + ctx.mention_reply(&format!("{} ({})", card_uri, image_uri.as_str())) |
| 28 | + .await? |
| 29 | + } |
| 30 | + None => ctx.mention_reply(&format!("{}", card_uri)).await?, |
| 31 | + } |
| 32 | + |
| 33 | + Ok(()) |
| 34 | + } |
| 35 | + |
| 36 | + async fn handle_privmsg(&self, ctx: &Arc<Context>, msg: &str) -> Result<()> { |
| 37 | + let captures: Vec<_> = self.re.captures_iter(msg).collect(); |
| 38 | + |
| 39 | + if captures.is_empty() { |
| 40 | + return Ok(()); |
| 41 | + } |
| 42 | + |
| 43 | + let mut change_errors = Vec::new(); |
| 44 | + |
| 45 | + // Loop through all captures, adding them to the output. |
| 46 | + for capture in captures { |
| 47 | + match self.handle_scryfall(ctx, &capture[1]).await { |
| 48 | + Ok(_) => {} |
| 49 | + Err(e) => { |
| 50 | + change_errors.push(format!("failed to look up \"{}\": {}", &capture[1], e)); |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + if !change_errors.is_empty() { |
| 56 | + ctx.mention_reply(&change_errors.join(", ")).await?; |
| 57 | + } |
| 58 | + |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[async_trait] |
| 64 | +impl Plugin for ScryfallPlugin { |
| 65 | + fn new_from_env() -> Result<Self> { |
| 66 | + Ok(ScryfallPlugin::new()) |
| 67 | + } |
| 68 | + |
| 69 | + fn command_metadata(&self) -> Vec<CommandMetadata> { |
| 70 | + vec![CommandMetadata { |
| 71 | + name: "scryfall".to_string(), |
| 72 | + short_help: "usage: scryfall [card name]. gives a link to a card on Scryfall." |
| 73 | + .to_string(), |
| 74 | + full_help: "gives a link to a given card on Scryfall if it exists".to_string(), |
| 75 | + }] |
| 76 | + } |
| 77 | + |
| 78 | + async fn run(self, bot: Arc<Client>) -> Result<()> { |
| 79 | + let mut stream = bot.subscribe(); |
| 80 | + |
| 81 | + while let Ok(ctx) = stream.recv().await { |
| 82 | + let res = match ctx.as_event() { |
| 83 | + Ok(Event::Command("scryfall", possible_arg)) => { |
| 84 | + match possible_arg.or_else(|| ctx.sender()) { |
| 85 | + Some(nick) => self.handle_scryfall(&ctx, nick).await, |
| 86 | + None => Err(format_err!("no card name found")), |
| 87 | + } |
| 88 | + } |
| 89 | + Ok(Event::Message(_, msg)) => self.handle_privmsg(&ctx, msg).await, |
| 90 | + _ => Ok(()), |
| 91 | + }; |
| 92 | + |
| 93 | + crate::check_err(&ctx, res).await; |
| 94 | + } |
| 95 | + |
| 96 | + Err(format_err!("karma plugin lagged")) |
| 97 | + } |
| 98 | +} |
0 commit comments