Browse Source

Create command structure

master
Graham Littlewood 4 years ago
parent
commit
59038dce1a
8 changed files with 134 additions and 2 deletions
  1. +3
    -0
      .gitignore
  2. +4
    -0
      Cargo.toml
  3. +8
    -1
      README.md
  4. +5
    -0
      src/commands.rs
  5. +35
    -0
      src/commands/cmd_macro.rs
  6. +64
    -1
      src/main.rs
  7. +5
    -0
      src/traits.rs
  8. +10
    -0
      src/traits/command.rs

+ 3
- 0
.gitignore View File

@@ -15,3 +15,6 @@ Cargo.lock
#Added by cargo

/target

#Gnome Builder files
.buildconfig

+ 4
- 0
Cargo.toml View File

@@ -7,3 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serenity = "0.8"
clap = "2.33.0"
json = "0.12.4"
regex = "1.3.6"

+ 8
- 1
README.md View File

@@ -1,3 +1,10 @@
# Leo

Productivity bot for Discord
Productivity bot for Discord

## Libraries

This bot uses the Discord API for Rust called [Serenity](https://github.com/serenity-rs/serenity).
([LICENSE](https://github.com/serenity-rs/serenity/blob/current/LICENSE.md))

This bot also uses [Clap](https://clap.rs/) for sprinkles under the hood.

+ 5
- 0
src/commands.rs View File

@@ -0,0 +1,5 @@
mod cmd_macro;

pub use self::{
cmd_macro::CmdMacro,
};

+ 35
- 0
src/commands/cmd_macro.rs View File

@@ -0,0 +1,35 @@
extern crate serenity;
use serenity::{
model::channel::Message,
prelude::*,
utils::MessageBuilder,
};

extern crate json;

extern crate regex;
use regex::Regex;

use crate::traits::Command;

pub struct CmdMacro {}

impl Command for CmdMacro {
fn execute(&self, ctx: &Context, msg: &Message) {

let response = MessageBuilder::new()
.push("Hello World")
.build();
msg.channel_id.say(&ctx.http, &response);
}

fn valid(&self, ctx: &Context, msg: &Message) -> bool {
return msg.content == "macro";
}
}

impl CmdMacro {
pub fn new() -> CmdMacro{
return CmdMacro{};
}
}

+ 64
- 1
src/main.rs View File

@@ -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);
}
}

+ 5
- 0
src/traits.rs View File

@@ -0,0 +1,5 @@
mod command;

pub use self::{
command::Command,
};

+ 10
- 0
src/traits/command.rs View File

@@ -0,0 +1,10 @@
extern crate serenity;
use serenity::{
model::channel::Message,
prelude::*,
};

pub trait Command: std::marker::Sync + std::marker::Send {
fn execute(&self, ctx: &Context, msg: &Message);
fn valid(&self, ctx: &Context, msg: &Message) -> bool;
}