Compare commits
2 Commits
rust
..
4f10af4373
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f10af4373 | |||
| 8cc56646ea |
Generated
+1
-1355
File diff suppressed because it is too large
Load Diff
+2
-4
@@ -1,10 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "discord-cli-rust"
|
name = "discord-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serenity = "0.11"
|
|
||||||
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
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")))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
pub struct ClientRepl {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub mod client;
|
|
||||||
+2
-46
@@ -1,47 +1,3 @@
|
|||||||
use crate::command::CommandList;
|
fn main() {
|
||||||
use crate::repl::{Interactive};
|
println!("Hello, world!");
|
||||||
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
@@ -1,33 +0,0 @@
|
|||||||
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(())
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
use std::io::{self, Write};
|
|
||||||
|
|
||||||
pub fn print(str: String) {
|
|
||||||
print!("{}", str);
|
|
||||||
io::stdout().flush().unwrap();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user