From bfe537eb3fd1a0336727af9f5cdbbbebbee737df Mon Sep 17 00:00:00 2001 From: GiyoMoon Date: Sat, 10 Sep 2022 14:41:31 +0200 Subject: [PATCH] :children_crossing: Make MAX_PLAYERS optional, accept interactions from all channels --- .env.example | 3 +++ Cargo.lock | 59 ++++++++++++++++++------------------------ Cargo.toml | 8 +++--- README.md | 6 ++--- src/bot.rs | 24 +++++++---------- src/discord.rs | 49 +++++++++++++++++++---------------- src/main.rs | 10 +++---- src/minecraft/enums.rs | 5 +++- 8 files changed, 80 insertions(+), 84 deletions(-) diff --git a/.env.example b/.env.example index 5c05bac..623508a 100644 --- a/.env.example +++ b/.env.example @@ -3,8 +3,11 @@ DISCORD_TOKEN= CONSOLE_CHANNEL_ID= SERVER_JAR_PATH=./server/server.jar SERVER_MEMORY=6144 +# Optional MAX_PLAYERS=5 +# Optional JVM_FLAGS= + # Only for development DEV=1 GUILD_ID= diff --git a/Cargo.lock b/Cargo.lock index eef563e..6c3e27c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,9 +34,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a26fa4d7e3f2eebadf743988fc8aec9fa9a9e82611acafd77c1462ed6262440a" +checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7" [[package]] name = "atty" @@ -69,9 +69,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block-buffer" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" dependencies = [ "generic-array", ] @@ -145,9 +145,9 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "cpufeatures" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc948ebb96241bb40ab73effeb80d9f93afaad49359d159a5e61be51619fe813" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" dependencies = [ "libc", ] @@ -225,7 +225,7 @@ dependencies = [ [[package]] name = "eve" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "dotenvy", @@ -259,11 +259,10 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "form_urlencoded" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" dependencies = [ - "matches", "percent-encoding", ] @@ -491,11 +490,10 @@ dependencies = [ [[package]] name = "idna" -version = "0.2.3" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" dependencies = [ - "matches", "unicode-bidi", "unicode-normalization", ] @@ -566,12 +564,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "matches" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" - [[package]] name = "memchr" version = "2.5.0" @@ -580,9 +572,9 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "miniz_oxide" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" dependencies = [ "adler", ] @@ -650,9 +642,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "pin-project-lite" @@ -1082,9 +1074,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45" +checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" dependencies = [ "bytes", "futures-core", @@ -1161,9 +1153,9 @@ dependencies = [ [[package]] name = "twilight-gateway" -version = "0.13.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a461bda77e2c634d1b2511ab094908f05a1977e8d963940a26b4eca8fb37c2c2" +checksum = "e5fd0fa1ef021ac6bc3027e2feae5743c8ee62072a56b0f947ed67bb5709fb02" dependencies = [ "bitflags", "flate2", @@ -1215,9 +1207,9 @@ dependencies = [ [[package]] name = "twilight-http-ratelimiting" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b75d1f386856ab4e769d26f31c76f6eacd55ae6125d288d1fc68c75632012f9e" +checksum = "effb20caaa9acad1036e37a14102cc999e254e929f2013b11057a544ec8d0ada" dependencies = [ "futures-util", "http", @@ -1227,9 +1219,9 @@ dependencies = [ [[package]] name = "twilight-model" -version = "0.13.2" +version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60367fe4ab87ac8ebd31077add81282cb99cc8519060834d5fed77521ab2d97b" +checksum = "a586bb38f14ac8282a7c032c146ce49a8d419e353d901d7f4c27ed21fac42305" dependencies = [ "bitflags", "serde", @@ -1293,13 +1285,12 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "url" -version = "2.2.2" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" dependencies = [ "form_urlencoded", "idna", - "matches", "percent-encoding", ] diff --git a/Cargo.toml b/Cargo.toml index be5c83b..cc246a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eve" -version = "0.1.0" +version = "0.1.1" authors = ["GiyoMoon "] edition = "2021" publish = false @@ -13,14 +13,14 @@ codegen-units = 1 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.63" +anyhow = "1.0.64" dotenvy = "0.15.3" env_logger = "0.9.0" futures = "0.3.24" log = "0.4.17" thiserror = "1.0.34" tokio = { version = "1.21.0", features = ["rt-multi-thread", "macros", "process"] } -twilight-gateway = "0.13.0" +twilight-gateway = "0.13.1" twilight-http = "0.13.0" -twilight-model = "0.13.2" +twilight-model = "0.13.3" twilight-util = { version = "0.13.1", features = ["builder"] } diff --git a/README.md b/README.md index 1bfb1ab..feca430 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ EVE lets you control a Minecraft server through Discord. It routes everything fr ![Example](./assets/example.png) ## Configuration -To run Eve, you need to set up a few environment variables: +To run EVE, you need to set up a few environment variables: - `DISCORD_TOKEN`: The token of you Discord bot - `CONSOLE_CHANNEL_ID`: The the ID of the Discord channel which should be used as the console. EVE will pass every output from the server into this channel - `SERVER_JAR_PATH`: Path to the server executable. E.g. `/srv/server/server.jar` - `SERVER_MEMORY`: Memory in megabytes to assign to the minecraft server. E.g `6144` -- `MAX_PLAYERS`: Max players of your minecraft server. (This is only used for the bot presence) +- `MAX_PLAYERS`: (Optional) Max players of your minecraft server. This is only used for the bot presence and if not provided, it won't show the player count there. - `JVM_FLAGS`: (Optional) Additional jvm flags to pass to the server instance - `RUST_LOG`: (Optional) Rust log level (Does not affect the server output). Set it to `info` to recieve all information or to `warn` if you just want to receive warnings/errors. @@ -43,7 +43,7 @@ sudo touch /etc/systemd/system/eve.service Insert this content: ``` [Unit] -Description=Eve +Description=EVE Wants=network-online.target After=network-online.target diff --git a/src/bot.rs b/src/bot.rs index ea7b3f4..8e2008e 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -15,7 +15,7 @@ pub async fn init() -> Result<(), anyhow::Error> { let token = env::var("DISCORD_TOKEN").unwrap(); let channel_id: Id = Id::new(env::var("CONSOLE_CHANNEL_ID").unwrap().parse().unwrap()); - let max_players: u8 = env::var("MAX_PLAYERS").unwrap().parse().unwrap(); + let max_players: Option = env::var("MAX_PLAYERS").ok().map(|max| max.parse().unwrap()); let scheme = ShardScheme::Range { from: 0, @@ -100,20 +100,14 @@ pub async fn init() -> Result<(), anyhow::Error> { while let Some((_, event)) = events.next().await { match event { Event::InteractionCreate(interaction) => { - match interaction.0.channel_id { - // check if interaction comes from configured channel - Some(c_id) if c_id == channel_id => { - handle_interaction( - application_id, - Arc::clone(&http), - Arc::clone(&server), - sender.clone(), - interaction, - ) - .await?; - } - _ => {} - } + handle_interaction( + application_id, + Arc::clone(&http), + Arc::clone(&server), + sender.clone(), + interaction, + ) + .await?; } Event::Ready(_) => { info!("Bot started!"); diff --git a/src/discord.rs b/src/discord.rs index 47d20c0..f80eae6 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -105,7 +105,7 @@ pub(crate) async fn handle_interaction( interaction_client, interaction.id, &interaction.token, - format!(":green_circle: Passing `{}` to the server instance", cmd), + format!("`{}`", cmd), ) .await; sender.send(ServerCommand::Stdin(cmd)).await.unwrap(); @@ -127,13 +127,6 @@ pub(crate) async fn handle_interaction( .find(|option| option.name == "message") { if let CommandOptionValue::String(msg) = cmd.value { - respond_to_interaction( - interaction_client, - interaction.id, - &interaction.token, - format!(":green_circle: Passing `{}` to the minecraft chat", msg), - ) - .await; let user = client .user(interaction.author_id().unwrap()) .exec() @@ -143,6 +136,14 @@ pub(crate) async fn handle_interaction( .await .unwrap() .name; + + respond_to_interaction( + interaction_client, + interaction.id, + &interaction.token, + format!("<{} Discord> {}", user, msg), + ) + .await; let msg = format!( r##"tellraw @a ["",{{"text":"<{} "}},{{"text":"Discord","color":"#5865F2"}},{{"text":">","color":"white"}},{{"text":" {}"}}]"##, user, msg @@ -188,18 +189,18 @@ pub(crate) async fn set_commands( let commands = [ CommandBuilder::new( "start", - "Starts the minecraft server", + "Starts the Minecraft server", CommandType::ChatInput, ) .build(), - CommandBuilder::new("stop", "Stops the minecraft server", CommandType::ChatInput).build(), + CommandBuilder::new("stop", "Stops the Minecraft server", CommandType::ChatInput).build(), CommandBuilder::new( "send", - "Send any command to the Minecraft server instance.", + "Pass a command to the Minecraft server.", CommandType::ChatInput, ) .option(CommandOption::String(ChoiceCommandOptionData { - description: "Command to pass to the server instance.".to_string(), + description: "Command to pass to the server.".to_string(), name: "command".to_string(), required: true, ..Default::default() @@ -271,7 +272,7 @@ async fn respond_to_interaction( pub(crate) async fn manage_status( cluster: Arc, current_status: ServerStatus, - max_players: u8, + max_players: Option, msg: &str, ) -> ServerStatus { if current_status == ServerStatus::Offline { @@ -282,13 +283,13 @@ pub(crate) async fn manage_status( set_status( cluster, ServerStatus::Running { - players: 0, + players: Some(0), max_players, }, ) .await; return ServerStatus::Running { - players: 0, + players: Some(0), max_players, }; } @@ -297,31 +298,31 @@ pub(crate) async fn manage_status( max_players, } = current_status { - if msg.contains("logged in with entity id") { + if msg.contains("logged in with entity id") && max_players.is_some() { set_status( cluster, ServerStatus::Running { - players: players + 1, + players: Some(players.unwrap() + 1), max_players, }, ) .await; return ServerStatus::Running { - players: players + 1, + players: Some(players.unwrap() + 1), max_players, }; } - if msg.contains("lost connection: ") { + if msg.contains("lost connection: ") && max_players.is_some() { set_status( cluster, ServerStatus::Running { - players: players - 1, + players: Some(players.unwrap() - 1), max_players, }, ) .await; return ServerStatus::Running { - players: players - 1, + players: Some(players.unwrap() - 1), max_players, }; } @@ -361,7 +362,11 @@ pub(crate) async fn set_status(cluster: Arc, status: ServerStatus) { } => { let activity = Activity::from(MinimalActivity { kind: ActivityType::Playing, - name: format!("🟢 Online | {}/{}", players, max_players), + name: if players.is_some() && max_players.is_some() { + format!("🟢 Online | {}/{}", players.unwrap(), max_players.unwrap()) + } else { + "🟢 Online".to_string() + }, url: None, }); UpdatePresence::new(Vec::from([activity]), false, None, Status::Online).unwrap() diff --git a/src/main.rs b/src/main.rs index 85c95fc..fa03b8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,11 +18,11 @@ async fn main() -> Result<(), anyhow::Error> { env::var("SERVER_MEMORY") .expect("SERVER_MEMORY env var not found") .parse::() - .expect("SERVER_MEMORY env var has to be a u16 integer"); - env::var("MAX_PLAYERS") - .expect("MAX_PLAYERS env var not found") - .parse::() - .expect("MAX_PLAYERS env var has to be a u8 integer"); + .expect("SERVER_MEMORY env var has to be an u16 integer"); + let _ = env::var("MAX_PLAYERS").map(|max| { + max.parse::() + .expect("MAX_PLAYERS env var has to be an u8 integer") + }); info!("Starting up..."); diff --git a/src/minecraft/enums.rs b/src/minecraft/enums.rs index aa2b2c5..4254909 100644 --- a/src/minecraft/enums.rs +++ b/src/minecraft/enums.rs @@ -26,6 +26,9 @@ pub(crate) enum ServerStartError { pub(crate) enum ServerStatus { Offline, Starting, - Running { players: u8, max_players: u8 }, + Running { + players: Option, + max_players: Option, + }, Stopping, }