Skip to content

Commit

Permalink
ref: overhaul useEmbedify
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Jan 5, 2025
1 parent 61a856c commit 64d8d66
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 71 deletions.
18 changes: 6 additions & 12 deletions src/commands/Config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ButtonBuilder, ButtonStyle, PermissionFlagsBits, SlashCommandBuilder, type AutocompleteInteraction, type CommandInteraction, type CommandInteractionOption, type SlashCommandSubcommandBuilder } from "discord.js";
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
import { Col, useEmbedify } from "@lib/embedify.ts";
import { CmdBase, SlashCommand } from "@lib/Command.ts";
import { numberFormatChoices, videoInfoTypeChoices } from "@cmd/VideoInfo.ts";
import { em } from "@lib/db.ts";
Expand Down Expand Up @@ -177,13 +177,10 @@ export class ConfigCmd extends SlashCommand {
return `${acc}${i !== 0 ? "\n" : ""}- **${capitalize(tr.for(locale, settingNameTrKey))}**: \`${val}\``;
}, "");

return int.editReply({
embeds: [
embedify(cfgList, Col.Info)
.setTitle(tr.for(locale, "commands.config.listEmbed.title"))
.setFooter({ text: tr.for(locale, "commands.config.listEmbed.footer") }),
],
});
return int.editReply(useEmbedify(cfgList, Col.Info, (e) => e
.setTitle(tr.for(locale, "commands.config.listEmbed.title"))
.setFooter({ text: tr.for(locale, "commands.config.listEmbed.footer") })
));
}
case "reset": {
const confirmBtns = [
Expand All @@ -200,10 +197,7 @@ export class ConfigCmd extends SlashCommand {
];

await int.editReply({
embeds: [
embedify(`**${tr.for(locale, "commands.config.reset.confirm")}**`, Col.Warning)
.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", 30) }),
],
...useEmbedify(`**${tr.for(locale, "commands.config.reset.confirm")}**`, Col.Warning, (e) => e.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", 30) })),
...useButtons([confirmBtns]),
});

Expand Down
50 changes: 21 additions & 29 deletions src/commands/Help.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, type CommandInteraction, type CommandInteractionOption } from "discord.js";
import { embedify } from "@lib/embedify.ts";
import { useEmbedify } from "@lib/embedify.ts";
import { CmdBase, SlashCommand } from "@lib/Command.ts";
import pkg from "@root/package.json" with { type: "json" };
import { getEnvVar } from "@lib/env.ts";
Expand Down Expand Up @@ -79,40 +79,32 @@ export class HelpCmd extends SlashCommand {
await int.deferReply({ ephemeral });
const locale = await HelpCmd.getGuildLocale(int);

return int.editReply({
embeds: [
embedify(cmdList)
.setTitle(tr.for(locale, "commands.help.embedTitles.commands"))
.setFooter({ text: tr.for(locale, "commands.help.embedFooters.commands") }),
],
});
return int.editReply(useEmbedify(cmdList, undefined, (e) => e
.setTitle(tr.for(locale, "commands.help.embedTitles.commands"))
.setFooter({ text: tr.for(locale, "commands.help.embedFooters.commands") }),
));
}
case "info": {
await int.deferReply();
const locale = await HelpCmd.getGuildLocale(int);

const { version, author: { name, url }} = pkg;
return int.editReply({
embeds: [
embedify([
tr.for(locale, "commands.help.info.headline", { version, name, url }),
tr.for(locale, "commands.help.info.donationLink", pkg.funding.url),
"",
tr.for(locale, "commands.help.info.bugsLink", pkg.bugs.url),
tr.for(locale, "commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
tr.for(locale, "commands.help.info.globalOptOut"),
"",
tr.for(locale, "commands.help.info.installExtensions"),
tr.for(locale, "commands.help.info.installExtReturnYtDislike"),
tr.for(locale, "commands.help.info.installExtSponsorBlock"),
tr.for(locale, "commands.help.info.installExtDeArrow"),
tr.for(locale, "commands.help.info.installExtMobile"),
"",
tr.for(locale, "commands.help.info.poweredBy"),
])
.setTitle(tr.for(locale, "commands.help.embedTitles.info")),
],
});
return int.editReply(useEmbedify([
tr.for(locale, "commands.help.info.headline", { version, name, url }),
tr.for(locale, "commands.help.info.donationLink", pkg.funding.url),
"",
tr.for(locale, "commands.help.info.bugsLink", pkg.bugs.url),
tr.for(locale, "commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
tr.for(locale, "commands.help.info.globalOptOut"),
"",
tr.for(locale, "commands.help.info.installExtensions"),
tr.for(locale, "commands.help.info.installExtReturnYtDislike"),
tr.for(locale, "commands.help.info.installExtSponsorBlock"),
tr.for(locale, "commands.help.info.installExtDeArrow"),
tr.for(locale, "commands.help.info.installExtMobile"),
"",
tr.for(locale, "commands.help.info.poweredBy"),
], undefined, (e) => e.setTitle(tr.for(locale, "commands.help.embedTitles.info"))));
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/commands/Privacy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ButtonBuilder, ButtonStyle, SlashCommandBuilder, type CommandInteraction } from "discord.js";
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
import { Col, useEmbedify } from "@lib/embedify.ts";
import { CmdBase, SlashCommand } from "@lib/Command.ts";
import { em } from "@lib/db.ts";
import { UserSettings } from "@models/UserSettings.model.ts";
Expand Down Expand Up @@ -66,10 +66,11 @@ export class PrivacyCmd extends SlashCommand {
const promptSec = 60;

const reply = await int.editReply({
embeds: [
embedify(Array.from({ length: 4 }).map((_, i) => tr.for(locale, `commands.privacy.delete.confirmLine${(i + 1) as 1}`)), Col.Warning)
.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", promptSec) }),
],
...useEmbedify(
Array.from({ length: 4 }).map((_, i) => tr.for(locale, `commands.privacy.delete.confirmLine${(i + 1) as 1}`)),
Col.Warning,
(e) => e.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", promptSec) })
),
...useButtons([confirmBtns]),
});

Expand Down
20 changes: 8 additions & 12 deletions src/commands/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ButtonBuilder, ButtonStyle, SlashCommandBuilder, type CommandInteraction, type CommandInteractionOption, type SlashCommandSubcommandBuilder } from "discord.js";
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
import { Col, useEmbedify } from "@lib/embedify.ts";
import { CmdBase, SlashCommand } from "@lib/Command.ts";
import { em } from "@lib/db.ts";
import { UserSettings } from "@models/UserSettings.model.ts";
Expand Down Expand Up @@ -101,13 +101,10 @@ export class SettingsCmd extends SlashCommand {
return `${acc}${i !== 0 ? "\n" : ""}- **${capitalize(settingName)}**: \`${val}\``;
}, "");

return int.editReply({
embeds: [
embedify(cfgList, Col.Info)
.setTitle("User settings values:")
.setFooter({ text: "Use /settings set <name> to edit a setting" }),
],
});
return int.editReply(useEmbedify(cfgList, Col.Info, (e) => e
.setTitle("User settings values:")
.setFooter({ text: "Use /settings set <name> to edit a setting" })
));
}
case "reset": {
const confirmBtns = [
Expand All @@ -124,10 +121,9 @@ export class SettingsCmd extends SlashCommand {
];

await int.editReply({
embeds: [
embedify("**Are you sure you want to reset your settings?**", Col.Warning)
.setFooter({ text: "This prompt will expire in 30s" }),
],
...useEmbedify("**Are you sure you want to reset your settings?**", Col.Warning, (e) =>
e.setFooter({ text: "This prompt will expire in 30s" })
),
...useButtons([confirmBtns]),
});

Expand Down
28 changes: 15 additions & 13 deletions src/lib/embedify.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import type { Stringifiable } from "@root/src/types.ts";
import { ColorResolvable, Colors, EmbedBuilder } from "discord.js";
import { ColorResolvable, EmbedBuilder } from "discord.js";

/** Color constants for embeds */
export enum Col {
Success = Colors.Green,
Error = Colors.Red,
Warning = Colors.Orange,
Info = Colors.Blurple,
Secondary = Colors.Greyple,
Success = 0x1f8b4c,
Error = 0xe34041,
Warning = 0xc27c0e,
Info = 0x3c4afc,
Secondary = 0x99aad5,
}

/** Creates a simple EmbedBuilder with the given text and color */
export function embedify(text: Stringifiable | Stringifiable[], color: ColorResolvable = Col.Info): EmbedBuilder {
const t = Array.isArray(text) ? text.map(String).join("\n") : text;
return new EmbedBuilder()
.setDescription(String(t)).setColor(color);
const txt = String(Array.isArray(text) ? text.map(String).join("\n") : text);
return new EmbedBuilder().setDescription(txt).setColor(color);
}

/**
* Like {@linkcode embedify()}, but can be passed to or spread into a `int.reply()`, `msg.edit()`, etc.
* @example ```ts
* await msg.edit(useEmbedify("ayo?"));
* await int.reply({ ...useEmbedify("waddup"), ...Command.useButtons(btns), attachments: [...] });
* await msg.edit(useEmbedify("ayo?", 0xffabff));
* await int.reply({ ...useEmbedify("waddup", undefined, (e) => e.setFooter({ text: "footer" })), ...Command.useButtons(btns), attachments: [...] });
* ```
*/
export function useEmbedify(text: Stringifiable | Stringifiable[], color?: ColorResolvable) {
return { embeds: [ embedify(text, color) ]};
export function useEmbedify(text: Stringifiable | Stringifiable[], color?: ColorResolvable, modify: (ebd: EmbedBuilder) => void = () => {}): { embeds: EmbedBuilder[] } {
const ebd = embedify(text, color);
modify?.(ebd);
return { embeds: [ebd] };
}

0 comments on commit 64d8d66

Please # to comment.