Skip to content

Commit

Permalink
Update async-minecraft-ping (#20)
Browse files Browse the repository at this point in the history
* Update depenencies and edition

* cargo fmt
  • Loading branch information
Hpmason authored Jan 1, 2022
1 parent 60c5824 commit ebda6db
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 46 deletions.
16 changes: 11 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mcbot-rs"
version = "0.2.1"
authors = ["Mason Ginter <mason@dagint.com>"]
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Discord bot for displaying the status of your minecraft server"
readme = "README.md"
Expand All @@ -14,9 +14,15 @@ keywords = ["discord", "minecraft", "bot"]

[dependencies]
serenity = "0.10"
tokio = { version = "1.10", features = ["macros", "rt-multi-thread", "time"] }

async-minecraft-ping = "0.4"
async-minecraft-ping = "0.8"

anyhow = "1.0"
lazy_static = "1.4"
lazy_static = "1.4"

[dependencies.tokio]
version = "1.15"
features = [
"macros",
"rt-multi-thread",
"time"
]
14 changes: 10 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use serenity::{client::Context, framework::standard::{Args, CommandResult, macros::command}, model::channel::Message};
use serenity::{
client::Context,
framework::standard::{macros::command, Args, CommandResult},
model::channel::Message,
};

use crate::helpers::*;
use crate::config::*;
use crate::helpers::*;

#[command]
#[usage("[address] [port]")]
Expand All @@ -11,11 +15,13 @@ use crate::config::*;
async fn info(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let addr = args.single::<String>().unwrap_or(ADDR.to_string());
let port = args.single::<u16>().unwrap_or(*PORT);

// Get status response from server
let res = get_status(&addr, port).await;
// println!("Res: {:?}", res);
// If successful, send response info to user
msg.channel_id.say(&ctx.http, status_or_error_message(res)).await?;
msg.channel_id
.say(&ctx.http, status_or_error_message(res))
.await?;
Ok(())
}
3 changes: 1 addition & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;

use lazy_static::lazy_static;


pub const REFRESH_TIME: u64 = 3000;

lazy_static! {
Expand All @@ -17,4 +16,4 @@ lazy_static! {
Ok(s) => s.parse().or::<u16>(Ok(25565)).unwrap(),
Err(_) => 25565
};
}
}
15 changes: 5 additions & 10 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::collections::HashSet;

use serenity::{async_trait, prelude::*};
use serenity::framework::standard::*;
use serenity::framework::standard::macros::*;
use serenity::framework::standard::*;
use serenity::model::prelude::*;
use serenity::{async_trait, prelude::*};

use tokio::time::{sleep, Duration};


use crate::commands::*;
use crate::config::*;
use crate::helpers::*;
use crate::commands::*;



#[help]
async fn my_help(
Expand All @@ -31,7 +28,6 @@ async fn my_help(
Ok(())
}


#[group]
#[commands(info)]
/// Get server info
Expand All @@ -50,9 +46,8 @@ impl EventHandler for Handler {
Ok(status) => get_activity(status),
Err(e) => {
println!("Error: {}", e);
Activity::playing("with some errors")

},
Activity::playing("with some errors")
}
};
ctx.set_presence(Some(act), OnlineStatus::Online).await;
sleep(Duration::from_millis(REFRESH_TIME)).await;
Expand Down
31 changes: 11 additions & 20 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use async_minecraft_ping::{ConnectionConfig, StatusResponse};
use async_minecraft_ping::{ConnectionConfig, ServerError, StatusResponse};
use serenity::model::prelude::Activity;

use anyhow::Result;
/// Get status using server_addr and port
pub async fn get_status(server_addr: &str, port: u16) -> Result<StatusResponse> {
let config = ConnectionConfig::build(server_addr.to_string())
.with_port(port);
pub async fn get_status(server_addr: &str, port: u16) -> Result<StatusResponse, ServerError> {
let config = ConnectionConfig::build(server_addr.to_string()).with_port(port);

let mut conn = config
.connect()
.await?;

let result = conn
.status()
.await?;
Ok(result)
let conn = config.connect().await?;

let ping_conn = conn.status().await?;
Ok(ping_conn.status)
}
/// Get discord activity from StatusResponse
pub fn get_activity(status: StatusResponse) -> Activity {
Expand All @@ -26,11 +20,8 @@ pub fn get_activity(status: StatusResponse) -> Activity {
}
// If there are 3 or less players, display player names
if players.len() <= 3 {
let comma_players: String = players
.into_iter()
.map(|a| a.name + ", ")
.collect();

let comma_players: String = players.into_iter().map(|a| a.name + ", ").collect();

let presence = format!("w/ {}", &comma_players);
return Activity::playing(&presence);
}
Expand All @@ -54,9 +45,9 @@ pub fn generate_message(status: StatusResponse) -> String {
msg
}

pub fn status_or_error_message(res: Result<StatusResponse>) -> String {
pub fn status_or_error_message(res: Result<StatusResponse, ServerError>) -> String {
match res {
Ok(status) => generate_message(status),
Err(e) => format!("Error getting server info: {}", e),
}
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

mod commands;
pub mod config;
pub mod handler;
mod helpers;
mod helpers;
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use serenity::framework::standard::StandardFramework;
use serenity::Client;

use mcbot_rs::handler::*;
use mcbot_rs::config::*;
use mcbot_rs::handler::*;

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -32,4 +31,4 @@ async fn main() {
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
}
}
}

0 comments on commit ebda6db

Please sign in to comment.