feat: reintroduce tag command

Signed-off-by: seth <getchoo@tuta.io>
This commit is contained in:
seth 2023-12-04 05:11:54 -05:00
parent a8eb4a212a
commit 30cc4a6220
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
12 changed files with 256 additions and 10 deletions

View file

@ -3,9 +3,11 @@ mod members;
mod rory;
mod say;
mod stars;
mod tag;
pub use joke::joke;
pub use members::members;
pub use rory::rory;
pub use say::say;
pub use stars::stars;
pub use tag::tag;

View file

@ -0,0 +1,57 @@
#![allow(non_camel_case_types, clippy::upper_case_acronyms)]
use crate::tags::Tag;
use crate::{consts, Context};
use std::env;
use color_eyre::eyre::{eyre, Result};
use once_cell::sync::Lazy;
use poise::serenity_prelude::{Color, User};
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
static TAGS: Lazy<Vec<Tag>> = Lazy::new(|| serde_json::from_str(env!("TAGS")).unwrap());
/// Send a tag
#[poise::command(slash_command)]
pub async fn tag(
ctx: Context<'_>,
#[description = "the copypasta you want to send"] name: TagChoice,
user: Option<User>,
) -> Result<()> {
let tag_file = name.as_str();
let tag = TAGS
.iter()
.find(|t| t.file_name == tag_file)
.ok_or_else(|| eyre!("Tried to get non-existent tag: {tag_file}"))?;
let frontmatter = &tag.frontmatter;
ctx.send(|m| {
if let Some(user) = user {
m.content(format!("<@{}>", user.id));
}
m.embed(|e| {
if let Some(color) = &frontmatter.color {
let color = *consts::COLORS
.get(color.as_str())
.unwrap_or(&Color::default());
e.color(color);
}
if let Some(image) = &frontmatter.image {
e.image(image);
}
if let Some(fields) = &frontmatter.fields {
for field in fields {
e.field(&field.name, &field.value, field.inline);
}
}
e
})
})
.await?;
Ok(())
}

View file

@ -13,6 +13,7 @@ pub fn to_global_commands() -> Vec<Command<Data, Report>> {
general::rory(),
general::say(),
general::stars(),
general::tag(),
moderation::ban_user(),
moderation::kick_user(),
]

View file

@ -1,14 +1,15 @@
use std::collections::HashMap;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Color;
pub static COLORS: Lazy<HashMap<&str, (u8, u8, u8)>> = Lazy::new(|| {
pub static COLORS: Lazy<HashMap<&str, Color>> = Lazy::new(|| {
HashMap::from([
("red", (239, 68, 68)),
("green", (34, 197, 94)),
("blue", (96, 165, 250)),
("yellow", (253, 224, 71)),
("orange", (251, 146, 60)),
("red", Color::from((239, 68, 68))),
("green", Color::from((34, 197, 94))),
("blue", Color::from((96, 165, 250))),
("yellow", Color::from((253, 224, 71))),
("orange", Color::from((251, 146, 60))),
])
});

View file

@ -9,8 +9,8 @@ mod eta;
pub async fn handle(
ctx: &Context,
event: &Event<'_>,
framework: FrameworkContext<'_, Data, Report>,
data: &Data,
_framework: FrameworkContext<'_, Data, Report>,
_data: &Data,
) -> Result<()> {
match event {
Event::Ready { data_about_bot } => {

View file

@ -12,6 +12,7 @@ mod commands;
mod config;
mod consts;
mod handlers;
mod tags;
mod utils;
type Context<'a> = poise::Context<'a, Data, Report>;

21
src/tags.rs Normal file
View file

@ -0,0 +1,21 @@
use poise::serenity_prelude::EmbedField;
use serde::{Deserialize, Serialize};
#[allow(dead_code)]
pub const TAG_DIR: &str = "tags";
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TagFrontmatter {
pub title: String,
pub aliases: Option<Vec<String>>,
pub color: Option<String>,
pub image: Option<String>,
pub fields: Option<Vec<EmbedField>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Tag {
pub content: String,
pub file_name: String,
pub frontmatter: TagFrontmatter,
}