Files
payphone/src/groups/about/embeds.rs
T
2026-04-06 17:30:19 -03:00

144 lines
5.4 KiB
Rust

use std::sync::Arc;
use twilight_model::channel::message::Embed;
use twilight_util::builder::embed::{
EmbedAuthorBuilder, EmbedBuilder, EmbedFieldBuilder, EmbedFooterBuilder, ImageSource,
};
use crate::constants::{COLOR_RED, COLOR_WHITE, VERSION};
use crate::discord::images;
use crate::discord::sanitizing::SanitizeDiscordMarkdown;
use crate::extras::Extras;
use crate::framework::commands::Command;
use crate::framework::commands::context::CommandContext;
use crate::framework::runtime::Runtime;
use crate::state::State;
use crate::utils::ToTitleCase;
pub(super) fn get_command_help_embed(
command: &Arc<dyn Command<State, Extras>>,
ctx: &CommandContext<'_, State, Extras>,
) -> Embed {
let name = format!("{}{}", ctx.prefix, command.name());
let arguments = command
.arguments()
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>()
.join(" ");
let usage = if !arguments.is_empty() {
format!("{name} {arguments}")
} else {
name
};
EmbedBuilder::new()
.author(
EmbedAuthorBuilder::new(ctx.rt.app.name.clone())
.icon_url(
ImageSource::url(images::get_avatar_url(
ctx.rt.app.bot.as_ref().unwrap().avatar,
ctx.rt.app.bot.as_ref().unwrap().id.get(),
ctx.rt.app.bot.as_ref().unwrap().discriminator as u64,
None,
))
.unwrap(),
)
.build(),
)
.thumbnail(
ImageSource::url(images::get_avatar_url(
ctx.rt.app.bot.as_ref().unwrap().avatar,
ctx.rt.app.bot.as_ref().unwrap().id.get(),
ctx.rt.app.bot.as_ref().unwrap().discriminator as u64,
None,
))
.unwrap(),
)
.color(COLOR_WHITE)
.title(format!("{} Command", command.name().to_title_case()))
.description(command.description())
.field(EmbedFieldBuilder::new("Usage", format!("`{usage}`")).build())
.field(EmbedFieldBuilder::new(
"Aliases",
if !command.aliases().is_empty() {
command
.aliases()
.iter()
.map(|alias| format!("`{}{alias}`", ctx.prefix))
.collect::<Vec<_>>()
.join(", ")
} else {
String::from("*This command doesn't have aliases.*")
},
))
.footer(EmbedFooterBuilder::new(format!(
"Use {}help <command> to get help about a specific command.",
ctx.prefix
)))
.build()
}
pub(super) fn get_all_command_help_embed(
prefix: &str,
rt: &Runtime<State, Extras>,
extras: &Extras,
) -> Embed {
EmbedBuilder::new()
.author(EmbedAuthorBuilder::new(rt.app.name.clone()).icon_url(ImageSource::url(images::get_avatar_url(
rt.app.bot.as_ref().unwrap().avatar,
rt.app.bot.as_ref().unwrap().id.get(),
rt.app.bot.as_ref().unwrap().discriminator as u64,
None
))
.unwrap()))
.title("Pick up the phone and start making friends!")
.description(format!("Here is the list of all available commands. You can see more details about a specific command with `{prefix}help <command>`."))
.thumbnail(ImageSource::url(images::get_avatar_url(
rt.app.bot.as_ref().unwrap().avatar,
rt.app.bot.as_ref().unwrap().id.get(),
rt.app.bot.as_ref().unwrap().discriminator as u64,
None
))
.unwrap())
.color(COLOR_WHITE)
.field(EmbedFieldBuilder::new("Commands", get_commands_table(rt, extras)).build())
.footer(EmbedFooterBuilder::new(format!("v{VERSION}-{} - Use {}help <command> to get more information about a specific command.", rt.shard_number + 1, prefix)))
.build()
}
pub(super) fn get_commands_table<T>(rt: &Runtime<T, Extras>, extras: &Extras) -> String {
let mut table = String::new();
for command in rt.commands.iter() {
if command.show_in_help(rt, extras) {
table.push_str(&format!("{:<13}", command.name()));
}
}
format!("```\n{table}\n```")
}
pub(super) fn get_command_not_found_embed<T>(rt: &Runtime<T, Extras>, name: String) -> Embed {
EmbedBuilder::new()
.color(COLOR_RED)
.author(EmbedAuthorBuilder::new(rt.app.name.clone()).icon_url(ImageSource::url(images::get_avatar_url(
rt.app.bot.as_ref().unwrap().avatar,
rt.app.bot.as_ref().unwrap().id.get(),
rt.app.bot.as_ref().unwrap().discriminator as u64,
None
))
.unwrap()))
.title("That's not a command")
.description(format!("`{}` is not a valid command name (nor alias). If you put it with the prefix on, take the prefix off before trying again. For example, if you did `p.help p.call`, do `p.help call` instead.\n\nIf you still can't figure it out, press the button below to go back to the full list. Everything's there!", name.sanitize(true)))
.thumbnail(ImageSource::url(images::get_avatar_url(
rt.app.bot.as_ref().unwrap().avatar,
rt.app.bot.as_ref().unwrap().id.get(),
rt.app.bot.as_ref().unwrap().discriminator as u64,
None
))
.unwrap())
.build()
}