mirror of
https://github.com/Astatin3/rustbot.git
synced 2026-06-08 16:18:08 -06:00
Make command system
This commit is contained in:
@@ -23,6 +23,9 @@ impl Chat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BotTask for Chat {
|
impl BotTask for Chat {
|
||||||
|
fn get_name(&self) -> &str {
|
||||||
|
"Chat"
|
||||||
|
}
|
||||||
fn on_event(&mut self, bot: &Client, _event: &Event) {
|
fn on_event(&mut self, bot: &Client, _event: &Event) {
|
||||||
if self.last_update.elapsed() >= DELAY {
|
if self.last_update.elapsed() >= DELAY {
|
||||||
bot.chat(self.messages[self.index].as_str());
|
bot.chat(self.messages[self.index].as_str());
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ impl GotoBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BotTask for GotoBlock {
|
impl BotTask for GotoBlock {
|
||||||
|
fn get_name(&self) -> &str {
|
||||||
|
"Goto"
|
||||||
|
}
|
||||||
fn on_event(&mut self, bot: &Client, event: &Event) {
|
fn on_event(&mut self, bot: &Client, event: &Event) {
|
||||||
if !self.started {
|
if !self.started {
|
||||||
self.started = true;
|
self.started = true;
|
||||||
|
|||||||
@@ -1,27 +1,14 @@
|
|||||||
use azalea::{Client, Event};
|
use azalea::{Client, Event};
|
||||||
|
use std::fmt::Debug;
|
||||||
use crate::bot_task::*;
|
|
||||||
|
|
||||||
pub trait BotTask: Send {
|
pub trait BotTask: Send {
|
||||||
|
fn get_name(&self) -> &str;
|
||||||
fn on_event(&mut self, bot: &Client, event: &Event);
|
fn on_event(&mut self, bot: &Client, event: &Event);
|
||||||
fn end(&self) -> bool;
|
fn end(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_command(command: &str) -> Option<Box<dyn BotTask>> {
|
impl Debug for dyn BotTask {
|
||||||
let args = command
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
.split_whitespace()
|
write!(f, "BotTask({})", self.get_name())
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
|
|
||||||
match args[0].as_str() {
|
|
||||||
"!chat" => Some(Box::new(Chat::init(args[1..].to_vec()))),
|
|
||||||
"!goto" => {
|
|
||||||
if let Some(task) = GotoBlock::parse(args[1..].to_vec()) {
|
|
||||||
Some(Box::new(task))
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+48
-3
@@ -1,4 +1,4 @@
|
|||||||
use std::sync::Arc;
|
use std::{alloc::System, sync::Arc};
|
||||||
|
|
||||||
pub mod bot_task;
|
pub mod bot_task;
|
||||||
pub mod command_controler;
|
pub mod command_controler;
|
||||||
@@ -10,6 +10,8 @@ use azalea::{
|
|||||||
use command_controler::BotTask;
|
use command_controler::BotTask;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
|
use crate::bot_task::*;
|
||||||
|
|
||||||
pub static BOT_COUNT: usize = 3;
|
pub static BOT_COUNT: usize = 3;
|
||||||
pub static BOT_PREFIX: &'static str = "bot";
|
pub static BOT_PREFIX: &'static str = "bot";
|
||||||
|
|
||||||
@@ -67,12 +69,29 @@ async fn handle(bot: Client, event: Event, state: BotState) -> anyhow::Result<()
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Component)]
|
#[derive(Clone, Component)]
|
||||||
pub struct BotState {
|
pub struct BotState {
|
||||||
pub task: Arc<Mutex<Option<Box<dyn BotTask>>>>,
|
pub task: Arc<Mutex<Option<Box<dyn BotTask>>>>,
|
||||||
// pub messages_received: Arc<Mutex<usize>>,
|
// pub messages_received: Arc<Mutex<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for BotState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
task: Arc::new(Mutex::new(None)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BotState {
|
||||||
|
fn get_task(&self) -> String {
|
||||||
|
match self.task.lock().as_ref() {
|
||||||
|
Some(task) => task.get_name().to_string(),
|
||||||
|
None => "No task".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default, Clone)]
|
#[derive(Resource, Default, Clone)]
|
||||||
struct SwarmState {
|
struct SwarmState {
|
||||||
pub tasks: Arc<Mutex<Vec<Box<dyn BotTask>>>>,
|
pub tasks: Arc<Mutex<Vec<Box<dyn BotTask>>>>,
|
||||||
@@ -97,7 +116,33 @@ async fn handle_swarm(swarm: Swarm, event: SwarmEvent, state: SwarmState) -> any
|
|||||||
println!("Chat message: {}", command);
|
println!("Chat message: {}", command);
|
||||||
|
|
||||||
println!("{}", command);
|
println!("{}", command);
|
||||||
if let Some(command) = command_controler::parse_command(command.as_str()) {
|
if let Some(command) = {
|
||||||
|
let args = command
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
match args[0].as_str() {
|
||||||
|
"!chat" => Some(Box::new(Chat::init(args[1..].to_vec())) as Box<dyn BotTask>),
|
||||||
|
"!goto" => {
|
||||||
|
if let Some(task) = GotoBlock::parse(args[1..].to_vec()) {
|
||||||
|
Some(Box::new(task) as Box<dyn BotTask>)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"!status" => {
|
||||||
|
for bot in swarm {
|
||||||
|
let botstate = &bot.get_component::<BotState>().unwrap();
|
||||||
|
println!("{} - {}", bot.username(), botstate.get_task());
|
||||||
|
}
|
||||||
|
println!("Unstarted: {:?}", state.tasks.lock());
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} {
|
||||||
state.tasks.lock().push(command);
|
state.tasks.lock().push(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user