|
- 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::*,
- };
-
- mod traits;
- mod commands;
-
- //use crate::traits::Command;
- struct CommandsKey;
-
- impl TypeMapKey for CommandsKey {
- type Value = std::vec::Vec<Box<dyn traits::Command>>;
- }
-
- struct Handler {
- pub commands: std::vec::Vec<Box<dyn traits::Command>>,
- }
-
- impl EventHandler for Handler {
- fn message(& self, ctx: Context, msg: Message) {
- let mut data = ctx.data.write();
- let commands = data.get_mut::<CommandsKey>().unwrap();
- // handle messages
- // check for help first
- for cmd in commands.iter_mut() {
- if cmd.valid_help(&ctx, &msg) {
- cmd.help(&ctx, &msg);
- return; // only do help
- }
- }
- for cmd in commands.iter_mut() {
- if cmd.valid(&ctx, &msg) {
- cmd.execute(&ctx, &msg);
- // multiple commands are allowed
- }
- }
- }
-
- fn ready(&self, ctx: Context, ready: Ready) {
- //ctx.online();
- ctx.set_activity(Activity::playing(&format!("v{} (s:{})", crate_version!(), ctx.shard_id.to_string())));
- println!("Connected as {} (API v{}) to {} guild(s)", ready.user.name, ready.version, ready.guilds.len());
- }
- }
-
- impl Handler {
- pub fn new() -> Handler {
- return Handler{
- commands: std::vec::Vec::new(),
- };
- }
- }
-
- fn main() {
- println!("Leo42 v{} is starting in {}", crate_version!(), env::current_dir().unwrap().to_str().unwrap());
- let token = env::var("DISCORD_TOKEN")
- .expect("Expected a Discord API token in DISCORD_TOKEN environment variable");
- let event_handler = Handler::new();
- // register commands;
- let mut commands = std::vec::Vec::<Box<dyn traits::Command>>::new();
- commands.push(Box::new(commands::CmdMacro::new()));
- commands.push(Box::new(commands::CmdPreview::new()));
- commands.push(Box::new(commands::CmdGitea::new()));
- // start bot
- let mut client = Client::new(&token, event_handler).expect("Error creating client");
- {
- let mut data = client.data.write();
- data.insert::<CommandsKey>(commands);
- }
- if let Err(why) = client.start() {
- println!("Client error: {:?}", why);
- }
- }
|