|
|
@@ -1,3 +1,66 @@ |
|
|
|
extern crate clap; |
|
|
|
use clap::crate_version; |
|
|
|
|
|
|
|
use std::env; |
|
|
|
use std::boxed::Box; |
|
|
|
|
|
|
|
extern crate serenity; |
|
|
|
use serenity::{ |
|
|
|
model::{channel::Message, gateway::{Ready, Activity}}, |
|
|
|
prelude::*, |
|
|
|
utils::MessageBuilder, |
|
|
|
}; |
|
|
|
|
|
|
|
mod traits; |
|
|
|
mod commands; |
|
|
|
|
|
|
|
//use crate::traits::Command; |
|
|
|
|
|
|
|
struct Handler { |
|
|
|
pub commands: std::vec::Vec<Box<dyn traits::Command>>, |
|
|
|
} |
|
|
|
|
|
|
|
impl EventHandler for Handler { |
|
|
|
fn message(&self, ctx: Context, msg: Message) { |
|
|
|
// handle messages |
|
|
|
let mut count = 0; |
|
|
|
for cmd in self.commands.iter() { |
|
|
|
if cmd.valid(&ctx, &msg) { |
|
|
|
count+=1; |
|
|
|
cmd.execute(&ctx, &msg); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn ready(&self, ctx: Context, ready: Ready) { |
|
|
|
//ctx.online(); |
|
|
|
ctx.set_activity(Activity::playing(&("v".to_owned()+crate_version!()+&" (s:".to_owned()+&ctx.shard_id.to_string()+&")".to_owned()))); |
|
|
|
println!("Connected as {} (API v{})", ready.user.name, ready.version); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl Handler { |
|
|
|
pub fn new() -> Handler { |
|
|
|
return Handler{ |
|
|
|
commands: std::vec::Vec::new(), |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
pub fn add_command(&mut self, box_cmd: Box<dyn traits::Command>) { |
|
|
|
self.commands.push(box_cmd); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn main() { |
|
|
|
println!("Hello, world!"); |
|
|
|
println!("Leo42 v{} is starting", crate_version!()); |
|
|
|
let token = env::var("DISCORD_TOKEN") |
|
|
|
.expect("Expected a Discord API token in DISCORD_TOKEN environment variable"); |
|
|
|
let mut event_handler = Handler::new(); |
|
|
|
// register commands; |
|
|
|
event_handler.add_command(Box::new(commands::CmdMacro::new())); |
|
|
|
// start bot |
|
|
|
let mut client = Client::new(&token, event_handler).expect("Error creating client"); |
|
|
|
if let Err(why) = client.start() { |
|
|
|
println!("Client error: {:?}", why); |
|
|
|
} |
|
|
|
} |