Browse Source

Add limited file attachment info functionality

master
NGnius 4 years ago
parent
commit
c0a4e14968
4 changed files with 105 additions and 1 deletions
  1. +1
    -1
      Cargo.toml
  2. +2
    -0
      src/commands.rs
  3. +101
    -0
      src/commands/cmd_preview.rs
  4. +1
    -0
      src/main.rs

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package]
name = "leo"
version = "0.2.0"
version = "0.3.0"
authors = ["NGnius <ngniusness@gmail.com>"]
edition = "2018"



+ 2
- 0
src/commands.rs View File

@@ -1,5 +1,7 @@
mod cmd_macro;
mod cmd_preview;

pub use self::{
cmd_macro::CmdMacro,
cmd_preview::CmdPreview,
};

+ 101
- 0
src/commands/cmd_preview.rs View File

@@ -0,0 +1,101 @@
extern crate serenity;
use serenity::{
model::channel::Message,
model::channel::Attachment,
prelude::*,
utils::MessageBuilder,
utils::Colour,
};

extern crate regex;
use regex::{Regex, RegexBuilder};

use crate::traits::Command;

pub struct CmdPreview {
format: Regex,
help_format: Regex,
extension_format: Regex,
}

impl Command for CmdPreview {
fn execute(&mut self, ctx: &Context, msg: &Message) {
for attachment in &msg.attachments {
if self.attachment_extension(&attachment) != "" {
if let Err(why) = msg.channel_id.send_message(&ctx.http, |m| {
return m.embed(|e| {
return e.colour(Colour::from_rgb(200, 200, 0))
.title(self.title_attachment(&attachment))
.description(self.describe_attachment(&attachment));
});
}) {
println!("Failed to send file preview message {:?}", why);
}
}
}
}

fn valid(&self, _ctx: &Context, msg: &Message) -> bool {
return self.format.is_match(&msg.content) && msg.attachments.len() != 0 && !msg.author.bot;
}

fn help(&self, ctx: &Context, msg:&Message) {
let mut response = MessageBuilder::new();
response.push("**Generate file attachment previews**\n*This has no standalone commands*");
if let Err(why) = msg.channel_id.say(&ctx.http, &response.build()) {
println!("Failed to send file preview help message {:?}", why);
}
}

fn valid_help(&self, _ctx: &Context, msg: &Message) -> bool {
return self.help_format.is_match(&msg.content);
}
}

impl CmdPreview {
pub fn new() -> CmdPreview {
return CmdPreview {
format:
RegexBuilder::new(r#".*"#)
.multi_line(true)
.case_insensitive(true)
.build()
.unwrap(),
help_format:
RegexBuilder::new(r#"^!help\s*(?:preview|file)"#)
.case_insensitive(true)
.build()
.unwrap(),
extension_format:
RegexBuilder::new(r#"\.(dll|exe|so)$"#)
.case_insensitive(true)
.build()
.unwrap(),
};
}

fn describe_attachment(&self, attachment: &Attachment) -> String {
let ext = self.attachment_extension(attachment);
if ext == "dll" {
return "Microsoft Windows **__D__ynamic __L__ink __L__ibrary**\nPlease make sure you trust who sent this before downloading this file.".to_string();
} else if ext == "exe" {
return "Microsoft Windows **__Exe__cutable**\nPlease make sure you trust who sent this before downloading or running this file.".to_string();
} else if ext == "so" {
return "Generic Operating System **__S__hared (__O__bject) Library**\nPlease make sure you trust who sent this before downloading or running this file.".to_string();
}
return "".to_string();
}

fn title_attachment(&self, attachment: &Attachment) -> String {
return attachment.filename.to_uppercase();
}

fn attachment_extension(&self, attachment: &Attachment) -> String {
if let Some(parsed) = self.extension_format.captures(&attachment.filename) {
if let Some(ext) = parsed.get(1) {
return ext.as_str().to_lowercase().to_string();
}
}
return "".to_string();
}
}

+ 1
- 0
src/main.rs View File

@@ -67,6 +67,7 @@ fn main() {
// register commands;
let mut commands = std::vec::Vec::<Box<dyn traits::Command>>::new();
commands.push(Box::new(commands::CmdMacro::new()));
commands.push(Box::new(commands::CmdPreview::new()));
// start bot
let mut client = Client::new(&token, event_handler).expect("Error creating client");
{