Skip to content

Commit 64d8d66

Browse files
committed
ref: overhaul useEmbedify
1 parent 61a856c commit 64d8d66

File tree

5 files changed

+56
-71
lines changed

5 files changed

+56
-71
lines changed

src/commands/Config.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ButtonBuilder, ButtonStyle, PermissionFlagsBits, SlashCommandBuilder, type AutocompleteInteraction, type CommandInteraction, type CommandInteractionOption, type SlashCommandSubcommandBuilder } from "discord.js";
2-
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
2+
import { Col, useEmbedify } from "@lib/embedify.ts";
33
import { CmdBase, SlashCommand } from "@lib/Command.ts";
44
import { numberFormatChoices, videoInfoTypeChoices } from "@cmd/VideoInfo.ts";
55
import { em } from "@lib/db.ts";
@@ -177,13 +177,10 @@ export class ConfigCmd extends SlashCommand {
177177
return `${acc}${i !== 0 ? "\n" : ""}- **${capitalize(tr.for(locale, settingNameTrKey))}**: \`${val}\``;
178178
}, "");
179179

180-
return int.editReply({
181-
embeds: [
182-
embedify(cfgList, Col.Info)
183-
.setTitle(tr.for(locale, "commands.config.listEmbed.title"))
184-
.setFooter({ text: tr.for(locale, "commands.config.listEmbed.footer") }),
185-
],
186-
});
180+
return int.editReply(useEmbedify(cfgList, Col.Info, (e) => e
181+
.setTitle(tr.for(locale, "commands.config.listEmbed.title"))
182+
.setFooter({ text: tr.for(locale, "commands.config.listEmbed.footer") })
183+
));
187184
}
188185
case "reset": {
189186
const confirmBtns = [
@@ -200,10 +197,7 @@ export class ConfigCmd extends SlashCommand {
200197
];
201198

202199
await int.editReply({
203-
embeds: [
204-
embedify(`**${tr.for(locale, "commands.config.reset.confirm")}**`, Col.Warning)
205-
.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", 30) }),
206-
],
200+
...useEmbedify(`**${tr.for(locale, "commands.config.reset.confirm")}**`, Col.Warning, (e) => e.setFooter({ text: tr.for(locale, "general.promptExpiryNotice", 30) })),
207201
...useButtons([confirmBtns]),
208202
});
209203

src/commands/Help.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SlashCommandBuilder, type CommandInteraction, type CommandInteractionOption } from "discord.js";
2-
import { embedify } from "@lib/embedify.ts";
2+
import { useEmbedify } from "@lib/embedify.ts";
33
import { CmdBase, SlashCommand } from "@lib/Command.ts";
44
import pkg from "@root/package.json" with { type: "json" };
55
import { getEnvVar } from "@lib/env.ts";
@@ -79,40 +79,32 @@ export class HelpCmd extends SlashCommand {
7979
await int.deferReply({ ephemeral });
8080
const locale = await HelpCmd.getGuildLocale(int);
8181

82-
return int.editReply({
83-
embeds: [
84-
embedify(cmdList)
85-
.setTitle(tr.for(locale, "commands.help.embedTitles.commands"))
86-
.setFooter({ text: tr.for(locale, "commands.help.embedFooters.commands") }),
87-
],
88-
});
82+
return int.editReply(useEmbedify(cmdList, undefined, (e) => e
83+
.setTitle(tr.for(locale, "commands.help.embedTitles.commands"))
84+
.setFooter({ text: tr.for(locale, "commands.help.embedFooters.commands") }),
85+
));
8986
}
9087
case "info": {
9188
await int.deferReply();
9289
const locale = await HelpCmd.getGuildLocale(int);
9390

9491
const { version, author: { name, url }} = pkg;
95-
return int.editReply({
96-
embeds: [
97-
embedify([
98-
tr.for(locale, "commands.help.info.headline", { version, name, url }),
99-
tr.for(locale, "commands.help.info.donationLink", pkg.funding.url),
100-
"",
101-
tr.for(locale, "commands.help.info.bugsLink", pkg.bugs.url),
102-
tr.for(locale, "commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
103-
tr.for(locale, "commands.help.info.globalOptOut"),
104-
"",
105-
tr.for(locale, "commands.help.info.installExtensions"),
106-
tr.for(locale, "commands.help.info.installExtReturnYtDislike"),
107-
tr.for(locale, "commands.help.info.installExtSponsorBlock"),
108-
tr.for(locale, "commands.help.info.installExtDeArrow"),
109-
tr.for(locale, "commands.help.info.installExtMobile"),
110-
"",
111-
tr.for(locale, "commands.help.info.poweredBy"),
112-
])
113-
.setTitle(tr.for(locale, "commands.help.embedTitles.info")),
114-
],
115-
});
92+
return int.editReply(useEmbedify([
93+
tr.for(locale, "commands.help.info.headline", { version, name, url }),
94+
tr.for(locale, "commands.help.info.donationLink", pkg.funding.url),
95+
"",
96+
tr.for(locale, "commands.help.info.bugsLink", pkg.bugs.url),
97+
tr.for(locale, "commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
98+
tr.for(locale, "commands.help.info.globalOptOut"),
99+
"",
100+
tr.for(locale, "commands.help.info.installExtensions"),
101+
tr.for(locale, "commands.help.info.installExtReturnYtDislike"),
102+
tr.for(locale, "commands.help.info.installExtSponsorBlock"),
103+
tr.for(locale, "commands.help.info.installExtDeArrow"),
104+
tr.for(locale, "commands.help.info.installExtMobile"),
105+
"",
106+
tr.for(locale, "commands.help.info.poweredBy"),
107+
], undefined, (e) => e.setTitle(tr.for(locale, "commands.help.embedTitles.info"))));
116108
}
117109
}
118110
}

src/commands/Privacy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ButtonBuilder, ButtonStyle, SlashCommandBuilder, type CommandInteraction } from "discord.js";
2-
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
2+
import { Col, useEmbedify } from "@lib/embedify.ts";
33
import { CmdBase, SlashCommand } from "@lib/Command.ts";
44
import { em } from "@lib/db.ts";
55
import { UserSettings } from "@models/UserSettings.model.ts";
@@ -66,10 +66,11 @@ export class PrivacyCmd extends SlashCommand {
6666
const promptSec = 60;
6767

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

src/commands/Settings.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ButtonBuilder, ButtonStyle, SlashCommandBuilder, type CommandInteraction, type CommandInteractionOption, type SlashCommandSubcommandBuilder } from "discord.js";
2-
import { Col, embedify, useEmbedify } from "@lib/embedify.ts";
2+
import { Col, useEmbedify } from "@lib/embedify.ts";
33
import { CmdBase, SlashCommand } from "@lib/Command.ts";
44
import { em } from "@lib/db.ts";
55
import { UserSettings } from "@models/UserSettings.model.ts";
@@ -101,13 +101,10 @@ export class SettingsCmd extends SlashCommand {
101101
return `${acc}${i !== 0 ? "\n" : ""}- **${capitalize(settingName)}**: \`${val}\``;
102102
}, "");
103103

104-
return int.editReply({
105-
embeds: [
106-
embedify(cfgList, Col.Info)
107-
.setTitle("User settings values:")
108-
.setFooter({ text: "Use /settings set <name> to edit a setting" }),
109-
],
110-
});
104+
return int.editReply(useEmbedify(cfgList, Col.Info, (e) => e
105+
.setTitle("User settings values:")
106+
.setFooter({ text: "Use /settings set <name> to edit a setting" })
107+
));
111108
}
112109
case "reset": {
113110
const confirmBtns = [
@@ -124,10 +121,9 @@ export class SettingsCmd extends SlashCommand {
124121
];
125122

126123
await int.editReply({
127-
embeds: [
128-
embedify("**Are you sure you want to reset your settings?**", Col.Warning)
129-
.setFooter({ text: "This prompt will expire in 30s" }),
130-
],
124+
...useEmbedify("**Are you sure you want to reset your settings?**", Col.Warning, (e) =>
125+
e.setFooter({ text: "This prompt will expire in 30s" })
126+
),
131127
...useButtons([confirmBtns]),
132128
});
133129

src/lib/embedify.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import type { Stringifiable } from "@root/src/types.ts";
2-
import { ColorResolvable, Colors, EmbedBuilder } from "discord.js";
2+
import { ColorResolvable, EmbedBuilder } from "discord.js";
33

4+
/** Color constants for embeds */
45
export enum Col {
5-
Success = Colors.Green,
6-
Error = Colors.Red,
7-
Warning = Colors.Orange,
8-
Info = Colors.Blurple,
9-
Secondary = Colors.Greyple,
6+
Success = 0x1f8b4c,
7+
Error = 0xe34041,
8+
Warning = 0xc27c0e,
9+
Info = 0x3c4afc,
10+
Secondary = 0x99aad5,
1011
}
1112

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

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

0 commit comments

Comments
 (0)