Skip to content

Commit

Permalink
feat: migrate to pistones
Browse files Browse the repository at this point in the history
  • Loading branch information
stifskere authored and Brayan-724 committed Nov 25, 2024
1 parent 08f5b57 commit 390939b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 167 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ tracing = "0.1.37"
once_cell = "1.18.0"
urlencoding = "2.1.3"
lazy_static = "1.5.0"
pistones = "0.2.1"
120 changes: 27 additions & 93 deletions src/bot/events/compile.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
mod api;

use pistones::Client;
use poise::serenity_prelude::{
Context, CreateEmbed, CreateEmbedFooter, CreateMessage, Message, ReactionType,
Context, CreateEmbed, CreateMessage, Message, ReactionType
};
use regex::Regex;
use std::time::Duration;
use tokio::time::sleep;

use crate::bot;

Expand Down Expand Up @@ -142,7 +139,6 @@ static MISSING_CODE_BLOCK: &str =
static MISSING_LANGUAGE: &str =
"Falta especificar un lenguaje a tu bloque de código, especificalo después de los \\`\\`\\`.";
static INVALID_LANGUAGE: &str = "El lenguaje especificado es invalido, los lenguajes validos son: ";
static INVALID_RESPONSE: &str = "La respuesta recibida del compilador no se pudo leer.";

pub async fn message(ctx: &Context, msg: &Message) -> Result<bool, bot::Error> {
if msg.author.bot || !msg.content.starts_with("&compile") {
Expand Down Expand Up @@ -243,98 +239,36 @@ pub async fn message(ctx: &Context, msg: &Message) -> Result<bool, bot::Error> {
code_block = code_block.replace(r"\`", "`");
}

let args = args_and_code[end_code.unwrap() + 3..]
.to_string()
.replace("\n", " ");

let api_response = api::compile_code(language, code_block, args).await;

if let Some(parsed_res) = api_response {
let mut response = parsed_res;
while response.status != "completed" {
sleep(Duration::from_secs(3)).await;
response = if let Some(new_status) = api::check_status(response.id).await {
new_status
} else {
msg.reply(ctx, INVALID_RESPONSE).await?;
return Ok(true);
};
}

let mut response_embed = CreateEmbed::default();

let mut succeded = false;

if let Some(build_details) = api::check_details(response.id).await {
if build_details.build_result.unwrap_or("success".to_string()) != "success" {
response_embed = response_embed
.title("Error de build!")
.description(format!(
"```\n{}\n```",
build_details
.build_stderr
.unwrap_or("<no se proporciono ningún error de build.>".to_string())
.replace("```", r"`‎`‎`")
let result = Client::new()
.await?
.run(language, code_block)
.await?
.run;

msg.channel_id.send_message(
&ctx.http,
CreateMessage::new()
.embed(
CreateEmbed::new()
.color(if result.code != 0 { 0xFF0000 } else { 0x00FF00 })
.title(format!(
"El programa se terminó {}",
if let Some(signal) = result.signal {
format!("con {signal} ({})", result.code)
} else {
format!("con código {}", result.code)
}
))
.color(0xFF0000)
.footer(CreateEmbedFooter::new(format!(
"El compilador salio con el código: {}",
build_details.build_exit_code.unwrap_or_default()
)));
} else if build_details.result.unwrap_or("success".to_string()) != "success" {
response_embed = response_embed
.title("Error de ejecución!")
.description(format!(
"```\n{}\n```",
build_details
.stderr
.unwrap_or("<no se proporciono ningún error de ejecución>".to_string())
.replace("```", r"`‎`‎`")
))
.color(0xFF0000)
.footer(CreateEmbedFooter::new(format!(
"El programa salio con el código: {}",
build_details.exit_code.unwrap_or_default()
)))
} else {
response_embed = response_embed
.title("El código se ejecuto correctamente")
.description(format!(
"```\n{}\n```",
build_details
"```{}```",
result
.stdout
.unwrap_or("<el código no escribió en la consola.>".to_string())
.replace("```", r"`‎`‎`")
.replace("```", "`‎`‎`")
))
.color(0x00FF00)
.footer(CreateEmbedFooter::new(format!(
"El programa salio con el código: {}",
build_details.exit_code.unwrap_or_default()
)));

succeded = true;
}

msg.channel_id
.send_message(
ctx,
CreateMessage::new()
.embed(response_embed)
.reference_message(msg),
)
.await?;

if !succeded {
msg.react(ctx, ReactionType::Unicode("❌".to_string()))
.await
.unwrap();
}
} else {
msg.reply(ctx, INVALID_RESPONSE).await?;
}
} else {
msg.reply(ctx, INVALID_RESPONSE).await?;
}
)
)
.await?;

Ok(true)
}
73 changes: 0 additions & 73 deletions src/bot/events/compile/api.rs

This file was deleted.

11 changes: 10 additions & 1 deletion src/bot/events/read_github_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use lazy_static::lazy_static;
use poise::serenity_prelude::{ButtonStyle, ComponentInteraction, Context, CreateButton, CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, Message, MESSAGE_CODE_LIMIT};
use regex::{Captures, Regex};
use reqwest::get;
use tracing::info;
use std::collections::{HashMap, HashSet};
use std::option::Option;

Expand Down Expand Up @@ -213,7 +214,15 @@ pub async fn handle_delete_embed(ctx: &Context, interaction: &ComponentInteracti
return false;
}

if interaction.message.author.id != interaction.user.id {
info!("{:?}", interaction.message.mentions);

let reference_message = interaction
.message
.referenced_message
.as_ref()
.unwrap();

if reference_message.author.id != interaction.user.id {
interaction
.create_response(
ctx,
Expand Down

0 comments on commit 390939b

Please sign in to comment.