refactor: random cleanups and efficiency improvements
This commit is contained in:
43
src/main.rs
43
src/main.rs
@@ -16,7 +16,6 @@ use bevy_log::{
|
||||
};
|
||||
use clap::Parser;
|
||||
use commands::{CommandSource, register};
|
||||
use events::handle_event;
|
||||
use futures::lock::Mutex;
|
||||
use mlua::{Function, Lua};
|
||||
use std::{
|
||||
@@ -43,13 +42,14 @@ pub struct State {
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = arguments::Arguments::parse();
|
||||
|
||||
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
|
||||
let event_listeners = Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
globals.set("script_path", &*script_path)?;
|
||||
lua::register_functions(&lua, &globals)?;
|
||||
|
||||
lua::register_functions(&lua, &globals, event_listeners.clone())?;
|
||||
lua.load(
|
||||
read_to_string(script_path)
|
||||
.expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),
|
||||
@@ -65,28 +65,29 @@ async fn main() -> anyhow::Result<()> {
|
||||
let mut commands = CommandDispatcher::new();
|
||||
register(&mut commands);
|
||||
|
||||
let log_plugin = LogPlugin {
|
||||
custom_layer: |_| {
|
||||
env::var("LOG_FILE").ok().map(|log_file| {
|
||||
layer()
|
||||
.with_writer(
|
||||
OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(log_file)
|
||||
.expect("should have been able to open log file"),
|
||||
)
|
||||
.boxed()
|
||||
})
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let Err(error) = ClientBuilder::new_without_plugins()
|
||||
.add_plugins(DefaultPlugins.set(LogPlugin {
|
||||
custom_layer: |_| {
|
||||
env::var("LOG_FILE").ok().map(|log_file| {
|
||||
layer()
|
||||
.with_writer(
|
||||
OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(log_file)
|
||||
.expect("should have been able to open log file"),
|
||||
)
|
||||
.boxed()
|
||||
})
|
||||
},
|
||||
..Default::default()
|
||||
}))
|
||||
.add_plugins(DefaultPlugins.set(log_plugin))
|
||||
.add_plugins(DefaultBotPlugins)
|
||||
.set_handler(handle_event)
|
||||
.set_handler(events::handle_event)
|
||||
.set_state(State {
|
||||
lua,
|
||||
event_listeners: Arc::new(Mutex::new(HashMap::new())),
|
||||
event_listeners,
|
||||
commands: Arc::new(commands),
|
||||
http_address: args.http_address,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user