Compare commits

..

No commits in common. "de13b472bbe0475e26a1735909568dffc4fc6fa1" and "890550c6efc6b598ce3ede5a0de4d4de0921d3b0" have entirely different histories.

7 changed files with 75 additions and 158 deletions

30
Cargo.lock generated
View File

@ -972,7 +972,7 @@ dependencies = [
[[package]]
name = "kon"
version = "0.3.3"
version = "0.3.1"
dependencies = [
"bb8",
"bb8-postgres",
@ -1104,14 +1104,13 @@ dependencies = [
[[package]]
name = "mio"
version = "1.0.1"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"hermit-abi",
"libc",
"wasi",
"windows-sys 0.52.0",
"windows-sys 0.48.0",
]
[[package]]
@ -1156,6 +1155,16 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "object"
version = "0.32.2"
@ -2170,27 +2179,28 @@ dependencies = [
[[package]]
name = "tokio"
version = "1.39.1"
version = "1.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d040ac2b29ab03b09d4129c2f5bbd012a3ac2f79d38ff506a4bf8dd34b0eac8a"
checksum = "eb2caba9f80616f438e09748d5acda951967e1ea58508ef53d9c6402485a46df"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.52.0",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-macros"
version = "2.4.0"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [
"proc-macro2",
"quote",

View File

@ -1,6 +1,6 @@
[package]
name = "kon"
version = "0.3.3"
version = "0.3.1"
edition = "2021"
[dependencies]
@ -16,7 +16,7 @@ serde = "1.0.204"
serde_json = "1.0.120"
sysinfo = "0.30.13"
tokenservice-client = { version = "0.3.2", registry = "gitea" }
tokio = { version = "1.39.1", features = ["macros", "signal", "rt-multi-thread"] }
tokio = { version = "1.38.1", features = ["macros", "signal", "rt-multi-thread"] }
tokio-postgres = "0.7.11"
uptime_lib = "0.3.1"

View File

@ -1,4 +1,4 @@
pub mod midi;
pub mod ping;
pub mod status;
pub mod midi;
pub mod uptime;

View File

@ -1,19 +1,13 @@
use crate::{
Error,
internals::utils::{
mention_dev,
format_bytes
}
internals::http::HttpClient
};
use regex::Regex;
use std::{
os::unix::fs::MetadataExt,
fs::{
use std::fs::{
write,
remove_file,
metadata
}
read_to_string,
remove_file
};
use poise::{
CreateReply,
@ -26,74 +20,57 @@ pub async fn midi_to_wav(
ctx: poise::Context<'_, (), Error>,
#[description = "MIDI file to be converted"] message: poise::serenity_prelude::Message
) -> Result<(), Error> {
let re = Regex::new(r"(?i)\.mid$").unwrap();
if !message.embeds.is_empty() || message.attachments.is_empty() || !re.is_match(&message.attachments[0].filename) {
ctx.reply("That ain't a MIDI file! What are you even doing??").await?;
return Ok(());
}
ctx.defer().await?;
let bytes = match message.attachments[0].download().await {
Ok(bytes) => bytes,
Err(y) => {
ctx.send(CreateReply::default()
.content(format!(
"Download failed, ask {} to check console for more information!",
mention_dev(ctx).unwrap_or_default()
))
)
.await.unwrap();
return Err(Error::from(format!("Failed to download the file: {}", y)))
}
};
let http = HttpClient::new();
let resp = http.get(&message.attachments[0].url, "MIDI Conversion").await?;
let bytes = resp.bytes().await?;
let midi_path = &message.attachments[0].filename;
write(midi_path, bytes)?;
let re = Regex::new(r"(?i)\.mid$").unwrap();
let wav_path = re.replace(&midi_path, ".wav");
let alpine_sf2 = include_bytes!("../internals/assets/FluidR3_GM.sf2");
let sf2_path = if let Ok(os_release) = read_to_string("/etc/os-release") {
if os_release.contains("Alpine") {
let sf2_path = "/tmp/FluidR3_GM.sf2";
write(sf2_path, include_bytes!("../internals/assets/FluidR3_GM.sf2"))?;
write(sf2_path, alpine_sf2)?;
sf2_path
} else {
"/usr/share/sounds/sf2/FluidR3_GM.sf2"
}
} else {
return Err(Error::from("Couldn't read \"/etc/os-release\" file!"))
};
let output = std::process::Command::new("fluidsynth")
.args(&["-ni", sf2_path, midi_path, "-F", &wav_path])
.args(&[
"-ni", sf2_path, midi_path, "-F", &wav_path
])
.output();
// Just to add an info to console to tell what the bot is doing when MIDI file is downloaded.
println!("Discord[{}:{}]: Processing MIDI file: \"{}\"", ctx.guild().unwrap().name, ctx.command().qualified_name, midi_path);
match output {
Ok(_) => {
let reply = ctx.send(CreateReply::default()
.attachment(CreateAttachment::path(&*wav_path).await.unwrap())
).await;
if reply.is_err() {
println!(
"Discord[{}:{}]: Processed file couldn't be uploaded back to Discord channel due to upload limit",
ctx.guild().unwrap().name, ctx.command().qualified_name
);
ctx.send(CreateReply::default()
.content(format!(
"Couldn't upload the processed file (`{}`, `{}`) due to upload limit",
&*wav_path, format_bytes(metadata(&*wav_path).unwrap().size())
))
).await.unwrap();
} else if reply.is_ok() {
.attachment(CreateAttachment::path(&*wav_path).await.unwrap())
).await.expect("Reply failed");
remove_file(midi_path)?;
remove_file(&*wav_path)?;
}
},
Err(y) => {
ctx.send(CreateReply::default()
.content("Command didn't execute successfully, check console for more information!")
).await.unwrap();
)
.await.unwrap();
return Err(Error::from(format!("Midi conversion failed: {}", y)))
return Err(Error::from(format!(
"Midi conversion failed: {}",
y
)))
}
}

View File

@ -9,43 +9,12 @@ use crate::{
use sysinfo::System;
use uptime_lib::get;
use std::{
fs::File,
path::Path,
time::{
use std::time::{
Duration,
SystemTime,
UNIX_EPOCH
},
io::{
BufRead,
BufReader
}
};
fn get_os_info() -> String {
let path = Path::new("/etc/os-release");
let mut name = "BoringOS".to_string();
let mut version = "v0.0".to_string();
if let Ok(file) = File::open(&path) {
let reader = BufReader::new(file);
for line in reader.lines() {
if let Ok(line) = line {
if line.starts_with("NAME=") {
name = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
} else if line.starts_with("VERSION=") {
version = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
} else if line.starts_with("VERSION_ID=") {
version = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
}
}
}
}
format!("{} {}", name, version)
}
/// Retrieve host and bot uptimes
#[poise::command(slash_command)]
pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
@ -53,12 +22,13 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
let mut sys = System::new_all();
sys.refresh_all();
// Fetch system's operating system
let sys_os_info = os_info::get();
let sys_os = format!("{} {}", sys_os_info.os_type(), sys_os_info.version());
// Fetch system's uptime
let sys_uptime = get().unwrap().as_secs();
// Fetch system's processor
let cpu = sys.cpus();
// Fetch bot's process uptime
let curr_pid = sysinfo::get_current_pid().unwrap();
let now = SystemTime::now();
@ -72,8 +42,7 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
format!("**{} {}**", _bot.name, BOT_VERSION.as_str()),
format!(">>> System: `{}`", format_duration(sys_uptime)),
format!("Process: `{}`", format_duration(proc_uptime)),
format!("CPU: `{}`", format!("{}", cpu[0].brand())),
format!("OS: `{}`", get_os_info())
format!("OS: `{}`", sys_os)
];
ctx.reply(concat_message(stat_msg)).await?;

View File

@ -1,4 +1,3 @@
use poise::serenity_prelude::UserId;
use once_cell::sync::Lazy;
use tokio::sync::Mutex;
use tokenservice_client::TokenServiceApi;
@ -24,25 +23,6 @@ pub fn concat_message(messages: Vec<String>) -> String {
messages.join("\n")
}
pub fn mention_dev(ctx: poise::Context<'_, (), crate::Error>) -> Option<String> {
let devs = super::config::BINARY_PROPERTIES.developers.clone();
let app_owners = ctx.framework().options().owners.clone();
let mut mentions = Vec::new();
for dev in devs {
if app_owners.contains(&UserId::new(dev)) {
mentions.push(format!("<@{}>", dev));
}
}
if mentions.is_empty() {
None
} else {
Some(mentions.join(", "))
}
}
pub fn format_duration(secs: u64) -> String {
let days = secs / 86400;
let hours = (secs % 86400) / 3600;
@ -63,24 +43,3 @@ pub fn format_duration(secs: u64) -> String {
formatted_string
}
pub fn format_bytes(bytes: u64) -> String {
let units = ["B", "KB", "MB", "GB", "TB", "PB"];
let mut value = bytes as f64;
let mut unit = units[0];
for &u in &units[1..] {
if value < 1024.0 {
break;
}
value /= 1024.0;
unit = u;
}
if unit == "B" {
format!("{}{}", value, unit)
} else {
format!("{:.2}{}", value, unit)
}
}

View File

@ -6,10 +6,7 @@ mod internals;
use crate::{
internals::{
utils::{
token_path,
mention_dev
},
utils::token_path,
config::BINARY_PROPERTIES
},
// controllers::database::DatabaseController
@ -28,6 +25,7 @@ use poise::serenity_prelude::{
ClientBuilder,
ChannelId,
Command,
UserId,
GatewayIntents
};
@ -115,8 +113,12 @@ async fn main() {
poise::FrameworkError::Command { error, ctx, .. } => {
println!("PoiseCommandError({}): {}", ctx.command().qualified_name, error);
ctx.reply(format!(
"Encountered an error during command execution, ask {} to check console for more details!",
mention_dev(ctx).unwrap_or_default()
"Encountered an error during command execution, ask **{}** to check console for more details!",
UserId::new(BINARY_PROPERTIES.developers[0])
.to_user(&ctx.http())
.await.expect("Error getting user")
.nick_in(&ctx.http(), BINARY_PROPERTIES.guild_id)
.await.expect("Error getting nickname")
)).await.expect("Error sending message");
},
poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {}", event.snake_case_name(), error),