Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/cripto_api", "crates/gen_image"]
members = ["crates/cripto_api", "crates/gen_image", "crates/meme_generator"]


[workspace.dependencies]
Expand Down Expand Up @@ -59,6 +59,7 @@ num-format = { version = "0.4.4" }

gen_image = { version = "0.1.0", path = "crates/gen_image" }
cripto_api = { version = "0.1.0", path = "crates/cripto_api" }
meme_generator ={ version = "0.1.0", path = "crates/meme_generator" }

reqwest = { workspace = true }
serde = { workspace = true }
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Se debe de instalar `shuttle` para ello usar `cargo-binstall`.

#### 🍎 Mac / 🐧Linux:
```bash
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
curl -sSfL https://www.shuttle.dev/install | bash
```

#### Para Windows:

```powershell
Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr "https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1").Content
iwr https://www.shuttle.dev/install-win | iex
```

Una vez instalado instalamos shuttle:
Expand All @@ -33,17 +33,17 @@ cargo binstall cargo-shuttle

Luego ejecuta el siguiente comando para ejecutar de modo local el bot:
```bash
cargo shuttle run
shuttle run
```

## Producción

Para ejecutar el bot en modo producción debemos ejecutar el siguiente comando:

```bash
cargo shuttle deploy
shuttle deploy
```

Esto deployara en Shuttle el bot.

> Documentación de [Shuttle](https://docs.shuttle.rs/getting-started/installation) para más información.
> Documentación de [Shuttle](https://docs.shuttle.rs/getting-started/installation) para más información.
4 changes: 4 additions & 0 deletions Shuttle.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
id = "proj_01JK2AC6TTKNA7J84A9J35A81X"
[build]
assets = [
"assets/*",
]
[deploy]
include = [
"assets/*",
Expand Down
13 changes: 13 additions & 0 deletions crates/meme_generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "meme_generator"
version = "0.1.0"
edition = "2021"

[dependencies]
async-trait = { workspace = true }
serde = { workspace = true, features = ["derive"] }
reqwest = { workspace = true, features = ["blocking", "json"] }
serde_json = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
37 changes: 37 additions & 0 deletions crates/meme_generator/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::meme::{InfoMemeResponse, MemeResponse};
use reqwest::Error;

const RANDOM_MEME_API: &str = "https://meme-api.com/gimme";

pub async fn get_random_meme_information() -> Result<InfoMemeResponse, Error> {
let client = reqwest::Client::new();
let response = client
.get(RANDOM_MEME_API)
.header("accept", "application/json")
.send()
.await?;

let meme_data = response.json::<InfoMemeResponse>().await?;

Ok(meme_data)
}

pub async fn download_random_meme() -> Result<MemeResponse, Box<dyn std::error::Error>> {
let meme_info = get_random_meme_information().await?;
let meme_url = meme_info.preview.last().unwrap();

let client = reqwest::Client::new();

let response = client.get(meme_url).send().await?;

if !response.status().is_success() {
return Err(format!("Error on download: {}", response.status()).into());
}

let bytes = response.bytes().await?;

Ok(MemeResponse {
title: meme_info.title,
content: bytes.to_vec(),
})
}
2 changes: 2 additions & 0 deletions crates/meme_generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod api;
pub mod meme;
8 changes: 8 additions & 0 deletions crates/meme_generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use meme_generator::api::get_random_meme_information;

#[tokio::main]
async fn main() {
let meme = get_random_meme_information().await;

println!("Meme {:?}", meme);
}
21 changes: 21 additions & 0 deletions crates/meme_generator/src/meme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InfoMemeResponse {
pub post_link: String,
pub subreddit: String,
pub title: String,
pub url: String,
pub nsfw: bool,
pub spoiler: bool,
pub author: String,
pub ups: i64,
pub preview: Vec<String>,
}

#[derive(Debug)]
pub struct MemeResponse {
pub title: String,
pub content: Vec<u8>,
}
16 changes: 16 additions & 0 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ impl EventHandler for Handler {
}
}
}
"meme" => {
if let Err(why) = command.defer(&ctx.http).await {
log_error!("Error deferring interaction: {:?}", why);

return;
}
match commands::meme::run(&ctx, &command).await {
Ok(embed) => Some(EmbedPayload::new(vec![embed]).defer(true)),
Err(err) => {
log_error!("Error on interaction: {:?}", err);

None
}
}
}
_ => None,
};

Expand Down Expand Up @@ -329,6 +344,7 @@ impl EventHandler for Handler {
commands::wallet_leaderboard::register(),
commands::courses::register(),
commands::crypto_prices::register(),
commands::meme::register(),
],
)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/commands/bans_info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serenity::all::{Ban, CommandInteraction, Context, CreateCommand};
use serenity::all::{Ban, CommandInteraction, Context, CreateCommand, Permissions};

fn ban_message(ban: Ban) -> String {
let user = ban.user.name;
Expand Down Expand Up @@ -31,4 +31,5 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) -> String {
pub fn register() -> CreateCommand {
CreateCommand::new("bans_info")
.description("Devuelve un detalle de los miembros baneados del servidor 🌍")
.default_member_permissions(Permissions::ADMINISTRATOR)
}
11 changes: 11 additions & 0 deletions src/commands/meme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serenity::all::{CommandInteraction, Context, CreateCommand, CreateEmbed};

use crate::{errors::CustomError, meme};

pub async fn run(_: &Context, _: &CommandInteraction) -> Result<CreateEmbed, CustomError> {
meme::create_meme_embeb().await
}

pub fn register() -> CreateCommand {
CreateCommand::new("meme").description("Obtener un meme random")
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod donate_coins;
pub mod fox;
pub mod list_projects;
pub mod members_count;
pub mod meme;
pub mod propose_project;
pub mod register_wallet;
pub mod say_hello;
Expand Down
3 changes: 2 additions & 1 deletion src/commands/warn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serenity::all::{
CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption,
ResolvedOption, ResolvedValue,
Permissions, ResolvedOption, ResolvedValue,
};

pub async fn run(ctx: &Context, interaction: &CommandInteraction) -> String {
Expand Down Expand Up @@ -62,4 +62,5 @@ pub fn register() -> CreateCommand {
CreateCommandOption::new(CommandOptionType::String, "reason", "The reason of warn")
.required(true),
)
.default_member_permissions(Permissions::ADMINISTRATOR)
}
2 changes: 0 additions & 2 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub const BOTS_IDS: [i64; 4] = [
276060004262477825,
1265501760013729923,
];
pub const CAT_CHAD_STICKER: u64 = 1265339053356613663;
pub const BASED_CAT_STICKER: u64 = 1267682074341408891;
pub const ENGLISH_DAYS: [chrono::Weekday; 2] = [chrono::Weekday::Wed, chrono::Weekday::Fri];
pub const ENGLISH_DAY_WHITELIST: [u64; 1] = [1072587560116817930];
pub const DUDE_EMOJI: (u64, &str) = (1257619715430420540, "tonohmm");
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod errors;
mod events;
mod github;
mod helpers;
mod meme;
mod projects;
mod router;
mod state;
Expand Down
25 changes: 25 additions & 0 deletions src/meme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use meme_generator::{self};

use serenity::all::CreateEmbed;
use tracing::error;

use crate::errors::CustomError;

pub async fn create_meme_embeb() -> Result<CreateEmbed, CustomError> {
match meme_generator::api::get_random_meme_information().await {
Ok(meme_response) => {
let embed = CreateEmbed::new()
.title(meme_response.title)
.image(meme_response.preview.last().unwrap());

Ok(embed)
}
Err(err) => {
error!("{:?}", err);

Err(CustomError::FetchError(
"Error on download meme image".to_string(),
))
}
}
}
26 changes: 19 additions & 7 deletions src/router/reminders.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::setup::RouterState;
use crate::consts;
use crate::utils;
use crate::{consts, meme};
use axum::routing::post;
use axum::Router;
use reqwest::StatusCode;
Expand All @@ -9,11 +9,17 @@ use axum::{extract::State, response::IntoResponse};
use tracing::{error, info};

async fn reminder_good_night(State(ctx): State<RouterState>) -> impl IntoResponse {
let response = utils::send_message_to_channel(
let embeb_meme_result = meme::create_meme_embeb().await;

if embeb_meme_result.is_err() {
return (StatusCode::BAD_GATEWAY, "Error on generate meme").into_response();
}

let response = utils::send_embeds_to_channel(
&ctx.0,
consts::GENERAL_CHANNEL_ID,
"Buenas noches gente".to_string(),
Some(consts::CAT_CHAD_STICKER),
vec![embeb_meme_result.unwrap()],
Some("Buenas noches gente".to_string()),
)
.await;

Expand All @@ -29,11 +35,17 @@ async fn reminder_good_night(State(ctx): State<RouterState>) -> impl IntoRespons
}

async fn reminder_good_morning(State(ctx): State<RouterState>) -> impl IntoResponse {
let response = utils::send_message_to_channel(
let embeb_meme_result = meme::create_meme_embeb().await;

if embeb_meme_result.is_err() {
return (StatusCode::BAD_GATEWAY, "Error on generate meme").into_response();
}

let response = utils::send_embeds_to_channel(
&ctx.0,
consts::GENERAL_CHANNEL_ID,
"Buenos días gente".to_string(),
Some(consts::BASED_CAT_STICKER),
vec![embeb_meme_result.unwrap()],
Some("Buenos días gente".to_string()),
)
.await;

Expand Down
4 changes: 3 additions & 1 deletion src/welcome/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ fn generate_welcome_information(user_id: UserId) -> String {
Recuerda registrarte primero en la wallet y cada semana se te acreditarán **chad-coins** (solo válido dentro del servidor) ¡Estamos emocionados de que formes parte!"
);

let website = "Visita nuestro website: https://chads-programming.dev";

format!(
"🎉 **Bienvenido/a: ** <@{}> 🎉\nTe dejamos presente la siguiente información: \n\n{github_information}\n\n{wallet_information}",
"🎉 **Bienvenido/a: ** <@{}> 🎉\nTe dejamos presente la siguiente información: \n\n{github_information}\n\n{wallet_information}\n\n{website}",
user_id
)
}
Loading