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.

82 lines
2.3KB

  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. };
  10. mod traits;
  11. mod commands;
  12. //use crate::traits::Command;
  13. struct CommandsKey;
  14. impl TypeMapKey for CommandsKey {
  15. type Value = std::vec::Vec<Box<dyn traits::Command>>;
  16. }
  17. struct Handler {
  18. pub commands: std::vec::Vec<Box<dyn traits::Command>>,
  19. }
  20. impl EventHandler for Handler {
  21. fn message(& self, ctx: Context, msg: Message) {
  22. let mut data = ctx.data.write();
  23. let commands = data.get_mut::<CommandsKey>().unwrap();
  24. // handle messages
  25. // check for help first
  26. for cmd in commands.iter_mut() {
  27. if cmd.valid_help(&ctx, &msg) {
  28. cmd.help(&ctx, &msg);
  29. return; // only do help
  30. }
  31. }
  32. for cmd in commands.iter_mut() {
  33. if cmd.valid(&ctx, &msg) {
  34. cmd.execute(&ctx, &msg);
  35. // multiple commands are allowed
  36. }
  37. }
  38. }
  39. fn ready(&self, ctx: Context, ready: Ready) {
  40. //ctx.online();
  41. ctx.set_activity(Activity::playing(&format!("v{} (s:{})", crate_version!(), ctx.shard_id.to_string())));
  42. println!("Connected as {} (API v{}) to {} guild(s)", ready.user.name, ready.version, ready.guilds.len());
  43. }
  44. }
  45. impl Handler {
  46. pub fn new() -> Handler {
  47. return Handler{
  48. commands: std::vec::Vec::new(),
  49. };
  50. }
  51. }
  52. fn main() {
  53. println!("Leo42 v{} is starting in {}", crate_version!(), env::current_dir().unwrap().to_str().unwrap());
  54. let token = env::var("DISCORD_TOKEN")
  55. .expect("Expected a Discord API token in DISCORD_TOKEN environment variable");
  56. let event_handler = Handler::new();
  57. // register commands;
  58. let mut commands = std::vec::Vec::<Box<dyn traits::Command>>::new();
  59. commands.push(Box::new(commands::CmdMacro::new()));
  60. commands.push(Box::new(commands::CmdPreview::new()));
  61. commands.push(Box::new(commands::CmdGitea::new()));
  62. // start bot
  63. let mut client = Client::new(&token, event_handler).expect("Error creating client");
  64. {
  65. let mut data = client.data.write();
  66. data.insert::<CommandsKey>(commands);
  67. }
  68. if let Err(why) = client.start() {
  69. println!("Client error: {:?}", why);
  70. }
  71. }