This commit is contained in:
2022-06-27 08:03:46 -05:00
commit 129732ef37
9 changed files with 1530 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

1361
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

10
Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "discord-cli-rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serenity = "0.11"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

68
src/command.rs Normal file
View File

@@ -0,0 +1,68 @@
use std::sync::Mutex;
use std::sync::Arc;
use std::error::Error;
pub struct Command {
name: String,
run_func: fn(Vec<String>) -> Result<(), Box<dyn Error>>
}
impl Command {
pub fn new(name: String, run: fn(Vec<String>) -> Result<(), Box<dyn Error>>) -> Command {
Command {
name: name,
run_func: run
}
}
pub fn run(&self, args: Vec<String>) -> Result<(), Box<dyn Error>> {
(self.run_func)(args)
}
}
// fn test() {
// let cmd = Command::new("hi".to_string(), testInner);
// }
// fn testInner(args: Vec<String>) -> Result<(), Box<dyn Error>> {
// Ok(())
// }
pub struct CommandList {
commands: Arc<Mutex<Vec<Command>>>
}
impl CommandList {
pub fn new(commands: Vec<Command>) -> CommandList {
CommandList {
commands: Arc::new(Mutex::new(commands))
}
}
pub fn command_names(&self) -> Vec<String> {
let commands = self.commands.lock().unwrap();
let mut names = Vec::new();
for cmd in commands.iter() {
names.push(cmd.name.clone())
}
names
}
pub fn has(&self, name: String) -> bool {
let commands = self.commands.lock().unwrap();
for cmd in commands.iter() {
if cmd.name == name {
return true;
}
}
false
}
pub fn run(&self, name: String, args: Vec<String>) -> Result<(), Box<dyn Error>> {
let commands = self.commands.lock().unwrap();
for cmd in commands.iter() {
if cmd.name == name {
return cmd.run(args);
}
}
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Command not found")))
}
}

3
src/discord/client.rs Normal file
View File

@@ -0,0 +1,3 @@
pub struct ClientRepl {
}

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

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

47
src/main.rs Normal file
View File

@@ -0,0 +1,47 @@
use crate::command::CommandList;
use crate::repl::{Interactive};
use serenity::prelude::*;
mod command;
mod discord;
mod repl;
mod util;
#[tokio::main]
async fn main() {
// let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let token = "TOKEN".to_string();
// Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&token, intents).await.expect("Err creating client");
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
//connect to discord
//on connect, open repl for Client
//commands may call another repl, calling a repl inside a repl *should* simply pause the first repl
let current_repl = Interactive {
prompt: "> ".to_string(),
commands: CommandList::new(vec![
command::Command::new("hi".to_string(), |_| {
println!("hi");
Ok(())
}),
command::Command::new("bye".to_string(), |_| {
println!("bye");
Ok(())
}),
]),
};
match current_repl.start() {
Ok(()) => {},
Err(_e) => {}
}
}

33
src/repl.rs Normal file
View File

@@ -0,0 +1,33 @@
use crate::command::CommandList;
use crate::util::print;
use std::error::Error;
use std::io::{self, BufRead};
pub struct Interactive {
pub commands: CommandList,
pub prompt: String,
}
impl Interactive {
pub fn start(&self) -> Result<(), Box<dyn Error>> {
let stdin = io::stdin();
print(self.prompt.clone());
for line in stdin.lock().lines() {
let line = line?;
let mut all_args = line.split(" ");
let name = all_args.next();
let args = all_args.map(|s| s.to_string()).collect();
if let Some(name) = name {
if let Err(e) = self.commands.run(name.to_string(), args) {
return Err(e);
}
}
print(self.prompt.clone());
}
Ok(())
}
}

6
src/util.rs Normal file
View File

@@ -0,0 +1,6 @@
use std::io::{self, Write};
pub fn print(str: String) {
print!("{}", str);
io::stdout().flush().unwrap();
}