Productivity bot for Discord
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

67 lines
1.7KB

  1. extern crate clap;
  2. use clap::crate_version;
  3. use std::env;
  4. use std::boxed::Box;
  5. extern crate serenity;
  6. use serenity::{
  7. model::{channel::Message, gateway::{Ready, Activity}},
  8. prelude::*,
  9. utils::MessageBuilder,
  10. };
  11. mod traits;
  12. mod commands;
  13. //use crate::traits::Command;
  14. struct Handler {
  15. pub commands: std::vec::Vec<Box<dyn traits::Command>>,
  16. }
  17. impl EventHandler for Handler {
  18. fn message(&self, ctx: Context, msg: Message) {
  19. // handle messages
  20. let mut count = 0;
  21. for cmd in self.commands.iter() {
  22. if cmd.valid(&ctx, &msg) {
  23. count+=1;
  24. cmd.execute(&ctx, &msg);
  25. }
  26. }
  27. }
  28. fn ready(&self, ctx: Context, ready: Ready) {
  29. //ctx.online();
  30. ctx.set_activity(Activity::playing(&("v".to_owned()+crate_version!()+&" (s:".to_owned()+&ctx.shard_id.to_string()+&")".to_owned())));
  31. println!("Connected as {} (API v{})", ready.user.name, ready.version);
  32. }
  33. }
  34. impl Handler {
  35. pub fn new() -> Handler {
  36. return Handler{
  37. commands: std::vec::Vec::new(),
  38. };
  39. }
  40. pub fn add_command(&mut self, box_cmd: Box<dyn traits::Command>) {
  41. self.commands.push(box_cmd);
  42. }
  43. }
  44. fn main() {
  45. println!("Leo42 v{} is starting", crate_version!());
  46. let token = env::var("DISCORD_TOKEN")
  47. .expect("Expected a Discord API token in DISCORD_TOKEN environment variable");
  48. let mut event_handler = Handler::new();
  49. // register commands;
  50. event_handler.add_command(Box::new(commands::CmdMacro::new()));
  51. // start bot
  52. let mut client = Client::new(&token, event_handler).expect("Error creating client");
  53. if let Err(why) = client.start() {
  54. println!("Client error: {:?}", why);
  55. }
  56. }