41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
#[macro_use]
|
|
extern crate lazy_static;
|
|
use commands::*;
|
|
use serenity::model::channel::Message;
|
|
use serenity::model::id::ChannelId;
|
|
use serenity::prelude::*;
|
|
mod commands;
|
|
mod config;
|
|
|
|
pub fn main() {
|
|
print_commands();
|
|
Client::new(&config::CONFIG.secret, Handler)
|
|
.expect("Error creating client")
|
|
.start()
|
|
.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) {
|
|
return;
|
|
}
|
|
if let Some(command) = find_matching(&msg.content) {
|
|
let response = match command.execute(&msg) {
|
|
Err(e) => e,
|
|
Ok(()) => "Done".to_owned(),
|
|
};
|
|
send(msg.channel_id, &response, &ctx);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn send(target: ChannelId, message: &str, ctx: &Context) {
|
|
if let Err(cause) = target.say(&ctx.http, message) {
|
|
println!("Could not send message: {}", cause);
|
|
}
|
|
}
|