Make prefix configurable
This commit is contained in:
parent
4e605d1648
commit
2003ab5cb9
|
@ -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"
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
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…
Reference in New Issue
Block a user