2020-01-02 17:51:02 +01:00
#[ macro_use ]
extern crate lazy_static ;
use commands ::* ;
2020-01-03 11:19:53 +01:00
use config ::CONFIG ;
2020-01-02 17:51:02 +01:00
use serenity ::model ::channel ::Message ;
use serenity ::model ::id ::ChannelId ;
use serenity ::prelude ::* ;
mod commands ;
2020-01-02 23:10:28 +01:00
mod config ;
2020-01-02 17:51:02 +01:00
pub fn main ( ) {
print_commands ( ) ;
2020-01-02 23:10:28 +01:00
Client ::new ( & config ::CONFIG . secret , Handler )
. expect ( " Error creating client " )
. start ( )
. expect ( " Could not connect to discord " ) ;
2020-01-02 17:51:02 +01:00
}
pub struct Handler ;
impl EventHandler for Handler {
fn message ( & self , ctx : Context , msg : Message ) {
2020-01-03 11:19:53 +01:00
if ! msg . content . starts_with ( & CONFIG . prefix ) {
2020-01-02 17:51:02 +01:00
return ;
}
2020-01-03 11:19:53 +01:00
if let Some ( command ) = find_matching ( & msg . content , & CONFIG ) {
2020-01-02 17:51:02 +01:00
let response = match command . execute ( & msg ) {
Err ( e ) = > e ,
2020-01-03 11:19:53 +01:00
Ok ( ( ) ) = > String ::from ( " Done. " ) ,
2020-01-02 17:51:02 +01:00
} ;
2020-01-02 23:10:28 +01:00
send ( msg . channel_id , & response , & ctx ) ;
2020-01-02 17:51:02 +01:00
}
}
}
pub fn send ( target : ChannelId , message : & str , ctx : & Context ) {
if let Err ( cause ) = target . say ( & ctx . http , message ) {
println! ( " Could not send message: {} " , cause ) ;
}
}
2020-01-03 11:19:53 +01:00
#[ 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 ) ) ;
}
}