Skip to content

Commit

Permalink
feat: translate cmd names & fix: use actual guild locale for translat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
Sv443 committed Dec 31, 2024
1 parent e45ea2b commit 9fbe813
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 129 deletions.
17 changes: 12 additions & 5 deletions src/assets/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"subcmd": {
"commands": "kommandos",
"info": "info"
},
"args": {
"show_hidden": "versteckte_anzeigen"
}
},
"args": {
"show_hidden": "versteckte_anzeigen"
},
"descriptions": {
"command": "Zeigt alle Kommandos oder Infos über den Bot an.",
"subcmd": {
Expand All @@ -50,6 +50,9 @@
"embedTitles": {
"commands": "Befehle:",
"info": "Informationen:"
},
"embedFooters": {
"commands": "Du kannst auch auf eine Nachricht rechtsklicken bzw. tippen und halten und unter \"Apps\" Kommandos ausführen."
}
},
"invite": {
Expand Down Expand Up @@ -93,13 +96,17 @@
},
"video_info": {
"names": {
"command": "video_info"
"command": "video_info",
"args": {
"video": "video",
"type": "typ"
}
},
"descriptions": {
"command": "Zeigt Infos zu einem Video anhand seiner URL oder ID.",
"options": {
"video": "URL oder ID des Videos - unterstützt music.youtube.com, youtube.com und youtu.be links.",
"type": "Art der Infos - Standardwert ist \"reduziert\"."
"type": "Typ der Infos - Standardwert ist \"reduziert\"."
}
},
"errors": {
Expand Down
12 changes: 11 additions & 1 deletion src/assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"subcmd": {
"commands": "commands",
"info": "info"
},
"args": {
"show_hidden": "show_hidden"
}
},
"descriptions": {
Expand Down Expand Up @@ -50,6 +53,9 @@
"embedTitles": {
"commands": "Commands:",
"info": "Information:"
},
"embedFooters": {
"commands": "You can also right click or tap and hold on a message and use commands under \"Apps\""
}
},
"invite": {
Expand Down Expand Up @@ -93,7 +99,11 @@
},
"video_info": {
"names": {
"command": "video_info"
"command": "video_info",
"args": {
"video": "video",
"type": "type"
}
},
"descriptions": {
"command": "Show information about a video, given its URL",
Expand Down
16 changes: 9 additions & 7 deletions src/commands/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useButtons } from "@lib/components.ts";
import { capitalize } from "@lib/text.ts";
import localesJson from "@assets/locales.json" with { type: "json" };
import type { Stringifiable } from "@src/types.ts";
import { tr } from "@lib/translate.ts";
import { registerCommandsForGuild } from "@lib/registry.ts";

//#region constants

Expand Down Expand Up @@ -70,8 +70,7 @@ const configurableOptions: Record<
opt.setName("new_value")
.setDescription("The new locale")
.setAutocomplete(true)
.setMinLength(5)
.setMaxLength(5)
.setMinLength(2)
.setRequired(true)
)
},
Expand Down Expand Up @@ -121,8 +120,8 @@ export class ConfigCmd extends SlashCommand {
//#region pb:run

public async run(int: CommandInteraction, opt: CommandInteractionOption) {
if(!int.inGuild())
return int.reply(useEmbedify("This command can only be used in a server", Col.Error));
if(!ConfigCmd.checkInGuild(int))
return;

const reply = await int.deferReply({ ephemeral: true });

Expand Down Expand Up @@ -253,8 +252,8 @@ export class ConfigCmd extends SlashCommand {
invalidHint?: string;
}) {
try {
if(!int.inGuild())
return int.reply(useEmbedify(tr("errors.onlyRunInGuild"), Col.Error));
if(!ConfigCmd.checkInGuild(int))
return;

await GuildConfig.ensureExists(int.guildId);
const cfg = await em.findOne(GuildConfig, { id: int.guildId });
Expand All @@ -277,6 +276,9 @@ export class ConfigCmd extends SlashCommand {
cfg.lastAccessed = new Date();
await em.flush();

if(cfgProp === "locale" && !this.global)
await registerCommandsForGuild(int.guildId);

return int.editReply(useEmbedify(`Successfully set the ${settingName} to \`${getValueLabel(newValue) ?? newValue}\``, Col.Success));
}
catch(err) {
Expand Down
32 changes: 20 additions & 12 deletions src/commands/Help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@ import { getLocMap, tr } from "@lib/translate.ts";
export class HelpCmd extends SlashCommand {
constructor() {
super(new SlashCommandBuilder()
.setName(CmdBase.getCmdName("help"))
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.help.names.command")))
.setNameLocalizations(getLocMap("commands.help.names.command", HelpCmd.cmdPrefix))
.setDescription(tr.forLang("en-US", "commands.help.descriptions.command"))
.setDescriptionLocalizations(getLocMap("commands.help.descriptions.command"))
.addSubcommand(subcommand =>
subcommand
.setName("commands")
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.help.names.subcmd.commands")))
.setNameLocalizations(getLocMap("commands.help.names.subcmd.commands"))
.setDescription(tr.forLang("en-US", "commands.help.descriptions.subcmd.commands"))
.setDescriptionLocalizations(getLocMap("commands.help.descriptions.subcmd.commands"))
.addBooleanOption(option =>
option
.setName("show_hidden")
.setName(tr.forLang("en-US", "commands.help.names.args.show_hidden"))
.setNameLocalizations(getLocMap("commands.help.names.args.show_hidden"))
.setDescription(tr.forLang("en-US", "commands.help.descriptions.args.show_hidden"))
.setDescriptionLocalizations(getLocMap("commands.help.descriptions.args.show_hidden"))
)
)
.addSubcommand(subcommand =>
subcommand
.setName("info")
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.help.names.subcmd.info")))
.setNameLocalizations(getLocMap("commands.help.names.subcmd.info"))
.setDescription(tr.forLang("en-US", "commands.help.descriptions.subcmd.info"))
.setDescriptionLocalizations(getLocMap("commands.help.descriptions.subcmd.info"))
)
Expand All @@ -39,6 +43,9 @@ export class HelpCmd extends SlashCommand {
//#region pb:run

public async run(int: CommandInteraction, opt: CommandInteractionOption) {
if(!HelpCmd.checkInGuild(int))
return;
const locale = await HelpCmd.getGuildLocale(int);
switch(opt.name) {
case "commands": {
let cmdList = "";
Expand Down Expand Up @@ -73,7 +80,8 @@ export class HelpCmd extends SlashCommand {
return int.reply({
embeds: [
embedify(cmdList)
.setTitle("commands.help.embedTitles.commands"),
.setTitle(tr.forLang(locale, "commands.help.embedTitles.commands"))
.setFooter({ text: tr.forLang(locale, "commands.help.embedFooters.commands") }),
],
ephemeral,
});
Expand All @@ -82,16 +90,16 @@ export class HelpCmd extends SlashCommand {
return int.reply({
embeds: [
embedify([
tr("commands.help.info.version", packageJson.version),
tr("commands.help.info.createdBy", packageJson.author.name, packageJson.author.url),
tr.forLang(locale, "commands.help.info.version", packageJson.version),
tr.forLang(locale, "commands.help.info.createdBy", packageJson.author.name, packageJson.author.url),
"",
tr("commands.help.info.globalOptOut"),
tr.forLang(locale, "commands.help.info.globalOptOut"),
"",
tr("commands.help.info.bugsLink", packageJson.bugs.url),
tr("commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
tr("commands.help.info.donationLink", packageJson.funding.url),
tr.forLang(locale, "commands.help.info.bugsLink", packageJson.bugs.url),
tr.forLang(locale, "commands.help.info.supportServerLink", getEnvVar("SUPPORT_SERVER_INVITE_URL")),
tr.forLang(locale, "commands.help.info.donationLink", packageJson.funding.url),
"",
tr("commands.help.info.poweredBy"),
tr.forLang(locale, "commands.help.info.poweredBy"),
])
.setTitle("commands.help.embedTitles.info"),
],
Expand Down
12 changes: 7 additions & 5 deletions src/commands/Invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { getLocMap, tr } from "@lib/translate.ts";
export class InviteCmd extends SlashCommand {
constructor() {
super(new SlashCommandBuilder()
.setName(CmdBase.getCmdName("invite"))
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.invite.names.command")))
.setNameLocalizations(getLocMap("commands.invite.names.command", InviteCmd.cmdPrefix))
.setDescription(tr.forLang("en-US", "commands.invite.description"))
.setDescriptionLocalizations(getLocMap("commands.invite.description"))
);
Expand All @@ -18,9 +19,10 @@ export class InviteCmd extends SlashCommand {
//#region pb:run

public async run(int: CommandInteraction) {
return int.reply({
...useEmbedify(tr("commands.invite.embedContent", getEnvVar("BOT_INVITE_URL")), Col.Info),
ephemeral: true,
});
await int.deferReply({ ephemeral: true });

const locale = await InviteCmd.getGuildLocale(int);

return int.editReply(useEmbedify(tr.forLang(locale, "commands.invite.embedContent", getEnvVar("BOT_INVITE_URL")), Col.Info));
}
}
38 changes: 21 additions & 17 deletions src/commands/Privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import { getLocMap, tr } from "@lib/translate.ts";
export class PrivacyCmd extends SlashCommand {
constructor() {
super(new SlashCommandBuilder()
.setName(CmdBase.getCmdName("privacy"))
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.privacy.names.command")))
.setNameLocalizations(getLocMap("commands.privacy.names.command", PrivacyCmd.cmdPrefix))
.setDescription(tr.forLang("en-US", "commands.privacy.descriptions.command"))
.setDescriptionLocalizations(getLocMap("commands.privacy.descriptions.command"))
.addSubcommand((sub) =>
sub
.setName("info")
.setName(tr.forLang("en-US", "commands.privacy.names.subcmd.info"))
.setNameLocalizations(getLocMap("commands.privacy.names.subcmd.info"))
.setDescription(tr.forLang("en-US", "commands.privacy.descriptions.subcmd.info"))
.setDescriptionLocalizations(getLocMap("commands.privacy.descriptions.subcmd.info"))
)
.addSubcommand((sub) =>
sub
.setName("delete_data")
.setName(tr.forLang("en-US", "commands.privacy.names.subcmd.delete_data"))
.setNameLocalizations(getLocMap("commands.privacy.names.subcmd.delete_data"))
.setDescription(tr.forLang("en-US", "commands.privacy.descriptions.subcmd.delete_data"))
.setDescriptionLocalizations(getLocMap("commands.privacy.descriptions.subcmd.delete_data"))
)
Expand All @@ -32,39 +35,40 @@ export class PrivacyCmd extends SlashCommand {
//#region pb:run

public async run(int: CommandInteraction) {
if(!PrivacyCmd.checkInGuild(int))
return;

await int.deferReply({ ephemeral: true });

const locale = await PrivacyCmd.getGuildLocale(int);
const sub = int.options.data[0].name;

if(sub === "info")
return int.reply({
...useEmbedify(Array.from({ length: 5 }).map((_, i) => tr(`commands.privacy.info.line${i + 1}`))),
ephemeral: true,
});
return int.editReply(useEmbedify(Array.from({ length: 5 }).map((_, i) => tr.forLang(locale, `commands.privacy.info.line${i + 1}`))));

if(sub === "delete_data") {
await int.deferReply({ ephemeral: true });

if(!await em.findOne(UserSettings, { id: int.user.id }))
return int.editReply(useEmbedify(tr("errors.noDataFoundToDelete"), Col.Info));
return int.editReply(useEmbedify(tr.forLang(locale, "errors.noDataFoundToDelete"), Col.Warning));

const confirmBtns = [
new ButtonBuilder()
.setCustomId("confirm-delete-data")
.setStyle(ButtonStyle.Danger)
.setLabel(tr("buttons.delete"))
.setLabel(tr.forLang(locale, "buttons.delete"))
.setEmoji("🗑️"),
new ButtonBuilder()
.setCustomId("cancel-delete-data")
.setStyle(ButtonStyle.Secondary)
.setLabel(tr("buttons.cancel"))
.setLabel(tr.forLang(locale, "buttons.cancel"))
.setEmoji("❌"),
];

const promptSec = 60;

const reply = await int.editReply({
embeds: [
embedify(Array.from({ length: 4 }).map((_, i) => tr(`commands.privacy.delete.confirmLine${i + 1}`)), Col.Warning)
.setFooter({ text: tr("general.promptExpiryNotice", promptSec) }),
embedify(Array.from({ length: 4 }).map((_, i) => tr.forLang(locale, `commands.privacy.delete.confirmLine${i + 1}`)), Col.Warning)
.setFooter({ text: tr.forLang(locale, "general.promptExpiryNotice", promptSec) }),
],
...useButtons([confirmBtns]),
});
Expand All @@ -82,20 +86,20 @@ export class PrivacyCmd extends SlashCommand {
if(conf.customId === "confirm-delete-data") {
await em.removeAndFlush(await em.find(UserSettings, { id: int.user.id }));
return conf.editReply({
...useEmbedify(tr("commands.privacy.delete.success"), Col.Success),
...useEmbedify(tr.forLang(locale, "commands.privacy.delete.success"), Col.Success),
components: [],
});
}
else {
return await conf.editReply({
...useEmbedify(tr("commands.privacy.delete.cancelled"), Col.Secondary),
...useEmbedify(tr.forLang(locale, "commands.privacy.delete.cancelled"), Col.Secondary),
components: [],
});
}
}
catch {
return await (conf ?? int).editReply({
...useEmbedify(tr("commands.privacy.delete.noConfirmation"), Col.Secondary),
...useEmbedify(tr.forLang(locale, "commands.privacy.delete.noConfirmation"), Col.Secondary),
components: [],
});
}
Expand Down
12 changes: 8 additions & 4 deletions src/commands/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { getLocMap, tr } from "@lib/translate.ts";
export class TemplateCmd extends SlashCommand {
constructor() {
super(new SlashCommandBuilder()
.setName(CmdBase.getCmdName("template_command"))
.setName(CmdBase.getCmdName(tr.forLang("en-US", "commands.template_command.name")))
.setNameLocalizations(getLocMap("commands.template_command.name", TemplateCmd.cmdPrefix))
.setDescription(tr.forLang("en-US", "commands.template_command.description"))
.setDescriptionLocalizations(getLocMap("commands.template_command.description"))
);
Expand All @@ -17,9 +18,12 @@ export class TemplateCmd extends SlashCommand {
//#region pb:run

public async run(int: CommandInteraction) {
return int.reply({
...useEmbedify(tr("TEMPLATE")),
ephemeral: true,
await int.deferReply({ ephemeral: true });

const locale = await TemplateCmd.getGuildLocale(int);

return int.editReply({
...useEmbedify(tr.forLang(locale, "TEMPLATE")),
});
}
}
Loading

0 comments on commit 9fbe813

Please # to comment.