Browse Source

Make prefix configurable

master
kageru 2 years ago
parent
commit
2003ab5cb9
Signed by: kageru GPG Key ID: 8282A2BEA4ADA3D2
  1. 6
      config.toml
  2. 18
      src/commands.rs
  3. 1
      src/config.rs
  4. 35
      src/main.rs

6
config.toml

@ -1,9 +1,9 @@
secret = "your login secret"
prefix = ">"
[[command]]
# The prefix (a constant in the source code)
# does not need to be specified here.
# It is added automatically.
# The prefix does not need to be added here.
# That is done automatically.
trigger = "create_file"
# Spaces are not escaped here
command = "touch /tmp/test-rce"

18
src/commands.rs

@ -1,9 +1,9 @@
use super::config::CONFIG;
use super::config::*;
use cmd_lib::{CmdResult, Process};
use serde::Deserialize;
use serenity::model::channel::Message;
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Command {
trigger: String,
command: String,
@ -14,8 +14,10 @@ pub fn print_commands() {
CONFIG.commands.iter().for_each(|c| println!("{:?}", c));
}
pub fn find_matching(message: &str) -> Option<&Command> {
CONFIG.commands.iter().find(|&c| message[1..] == c.trigger)
pub fn find_matching<'a>(message: &str, cfg: &'a Config) -> Option<&'a Command> {
cfg.commands
.iter()
.find(|&c| message[cfg.prefix.len()..] == c.trigger)
}
impl Command {
@ -33,4 +35,12 @@ impl Command {
.wait::<CmdResult>()
.map_err(|e| format!("{:?}", e))
}
pub fn new(command: &str, trigger: &str, users: Vec<u64>) -> Self {
Self {
command: String::from(command),
trigger: String::from(trigger),
users,
}
}
}

1
src/config.rs

@ -11,6 +11,7 @@ pub struct Config {
#[serde(rename = "command")]
pub commands: Vec<Command>,
pub secret: String,
pub prefix: String,
}
lazy_static! {

35
src/main.rs

@ -1,6 +1,7 @@
#[macro_use]
extern crate lazy_static;
use commands::*;
use config::CONFIG;
use serenity::model::channel::Message;
use serenity::model::id::ChannelId;
use serenity::prelude::*;
@ -15,18 +16,16 @@ pub fn main() {
.expect("Could not connect to discord");
}
const PREFIX: char = '>';
pub struct Handler;
impl EventHandler for Handler {
fn message(&self, ctx: Context, msg: Message) {
if !msg.content.starts_with(PREFIX) {
if !msg.content.starts_with(&CONFIG.prefix) {
return;
}
if let Some(command) = find_matching(&msg.content) {
if let Some(command) = find_matching(&msg.content, &CONFIG) {
let response = match command.execute(&msg) {
Err(e) => e,
Ok(()) => "Done".to_owned(),
Ok(()) => String::from("Done."),
};
send(msg.channel_id, &response, &ctx);
}
@ -38,3 +37,29 @@ pub fn send(target: ChannelId, message: &str, ctx: &Context) {
println!("Could not send message: {}", cause);
}
}
#[cfg(test)]
mod tests {
use super::commands::*;
use super::config::*;
use super::*;
#[test]
fn find_matching_test() {
let cmd = Command::new("ls -l", "show_files", vec![1234567890]);
let cfg = Config {
prefix: String::from(">"),
secret: String::from("1qay2wsx3edc45fv"),
commands: vec![cmd.clone()],
};
assert_eq!(find_matching(&">show_files", &cfg), Some(&cmd));
assert_eq!(find_matching(&">show something else", &cfg), None);
let cfg = Config {
prefix: String::from("!!"),
..cfg
};
assert_eq!(find_matching(&">show_files", &cfg), None);
assert_eq!(find_matching(&"!!show_files", &cfg), Some(&cmd));
}
}

Loading…
Cancel
Save