This commit is contained in:
April 2019-10-21 15:56:27 -04:00
parent b1e6d7596d
commit c78afd731e
9 changed files with 124 additions and 1 deletions

2
Cargo.lock generated
View File

@ -533,6 +533,8 @@ dependencies = [
name = "makomo"
version = "0.1.0"
dependencies = [
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
"serenity 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -8,3 +8,5 @@ edition = "2018"
[dependencies]
serenity = "0.7"
serde = "1.0"
serde_json = "1.0"

5
conf.json Normal file
View File

@ -0,0 +1,5 @@
{
"token": "NjI5MzgwNjE2NDM3MzY2ODI0.Xa5CTg.ynVtKebrpgY6YKyis2bNLg-e0S8",
"prefix": "mako",
"owner_id": 257521982021566464
}

22
src/config.rs Normal file
View File

@ -0,0 +1,22 @@
use std::path::Path;
use std::fs::File;
use std::io::Read;
use serde_json::from_str;
use serde::{Deserialize, Serialize};
use serde_json::Result;
#[derive(Deserialize, Serialize)]
pub struct Config {
pub token: String,
pub prefix: String,
pub owner_id: u64
}
impl Config {
pub fn new<P: AsRef<Path>>(file: P) -> Result<Self> {
let mut file = File::open(file).unwrap();
let mut data = String::new();
file.read_to_string(&mut data);
Ok(from_str(&data)?)
}
}

20
src/handler.rs Normal file
View File

@ -0,0 +1,20 @@
use serenity::prelude::*;
use serenity::model::{
gateway:: {
Ready,
Activity
}
};
pub struct Handler;
impl EventHandler for Handler {
fn ready(&self, ctx: Context, ready: Ready) {
println!("Bot is ready on {}", ready.user.tag());
println!("Servers: {}", ready.guilds.len());
let act = Activity::playing("with rust");
ctx.set_activity(act);
}
}

View File

@ -1,3 +1,19 @@
#![allow(unused_must_use)]
mod config;
mod makomo;
mod handler;
mod modules;
use config::Config;
use makomo::init_client;
fn main() {
println!("Hello, world!");
let conf: Config = Config::new("conf.json").unwrap();
let mut client = init_client(conf);
if let Err(why) = client.start() {
println!("Error starting client. {}", why)
}
}

33
src/makomo.rs Normal file
View File

@ -0,0 +1,33 @@
use serenity::client::Client;
use serenity::framework::standard::StandardFramework;
use serenity::model::id::UserId;
use crate::config::Config;
use crate::handler::Handler;
// Modules
use crate::modules::generic::GENERIC_GROUP;
pub fn init_client(conf: Config) -> Client {
let mut client = Client::new(&conf.token, Handler).expect("Error creating client.");
client.with_framework(framework(conf));
return client
}
fn framework(conf: Config) -> StandardFramework {
let fw = StandardFramework::new()
.configure(|c| c
.prefix(&conf.prefix)
.with_whitespace(true)
.owners(vec![UserId(conf.owner_id)].into_iter().collect()))
.after(|_ctx, msg, cmd_name, error| {
match error {
Ok(()) => println!("Processed command '{}' from author {}", cmd_name, msg.author.name),
Err(why) => println!("The command '{}' gave the error: {:?}", cmd_name, why)
}
})
.group(&GENERIC_GROUP);
return fw;
}

22
src/modules/generic.rs Normal file
View File

@ -0,0 +1,22 @@
use serenity::model::channel::Message;
use serenity::prelude::*;
use serenity::framework::standard::{
CommandResult,
macros::{
command,
group
}
};
group!({
name: "generic",
options: {},
commands: [ping]
});
#[command]
pub fn ping(ctx: &mut Context, msg: &Message) -> CommandResult {
msg.reply(ctx, "Pong!")?;
Ok(())
}

1
src/modules/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod generic;