Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat: working i18n!
Browse files Browse the repository at this point in the history
  • Loading branch information
janleigh committed Jul 4, 2023
1 parent 9edd8ad commit 28ff42e
Show file tree
Hide file tree
Showing 21 changed files with 875 additions and 40 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "yarn build:clean && yarn compile",
"build:production": "yarn build:clean && yarn format:check && yarn lint && yarn compile",
"build:clean": "rm -rvf ./build/",
"compile": "npx sucrase src -d build --transforms typescript,imports",
"compile": "npx sucrase src -d build --transforms typescript,imports && cp -rvf src/languages/ build/",
"format": "prettier --write src",
"format:check": "prettier --check src",
"lint": "eslint src --ext .ts",
Expand All @@ -32,6 +32,7 @@
"dependencies": {
"@sapphire/framework": "^4.2.0",
"@sapphire/pieces": "^3.6.3",
"@sapphire/plugin-i18next": "^5.0.3",
"@sapphire/plugin-logger": "^3.0.1",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
Expand Down
23 changes: 18 additions & 5 deletions src/commands/core/PingCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isMessageInstance } from "@sapphire/discord.js-utilities";
import { ChatInputCommand, Command, RegisterBehavior } from "@sapphire/framework";
import { BaseEmbedBuilder } from "../../libraries/structures/components/BaseEmbedBuilder";
import { BaseEmbedBuilder } from "../../libraries/structures/components";
import { resolveKey } from "@sapphire/plugin-i18next";
import { LanguageKeys } from "../../libraries/language";

export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
Expand All @@ -18,17 +20,28 @@ export class PingCommand extends Command {
}

public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const msg = await interaction.reply({ content: "> Ping?", ephemeral: false, fetchReply: true });
const embed = new BaseEmbedBuilder().isErrorEmbed().setDescription(":(, something went wrong.");
const msg = await interaction.reply({
content: `> ${await resolveKey(interaction, LanguageKeys.Commands.Ping.PING_WAITING)}`,
ephemeral: false,
fetchReply: true
});
const embed = new BaseEmbedBuilder()
.isErrorEmbed()
.setDescription(await resolveKey(interaction, LanguageKeys.Commands.Ping.PING_FAILED));

if (isMessageInstance(msg)) {
const diff = msg.createdTimestamp - interaction.createdTimestamp;
const ping = Math.round(this.container.client.ws.ping);

embed.isSuccessEmbed(false);
embed.setDescription(`**Discord API**: \`${ping}\`ms\n**Websocket**: \`${diff}\`ms`);
embed.setDescription(
await resolveKey(interaction, LanguageKeys.Commands.Ping.PING_SUCCESS_DESCRIPTION, { ping, diff })
);

return interaction.editReply({ content: "Pong 🏓!", embeds: [embed] });
return interaction.editReply({
content: `${await resolveKey(interaction, LanguageKeys.Commands.Ping.PING_SUCCESS)}`,
embeds: [embed]
});
}

return interaction.editReply({ content: "", embeds: [embed] });
Expand Down
3 changes: 1 addition & 2 deletions src/commands/developer/EvaluateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ChatInputCommand, Command, RegisterBehavior } from "@sapphire/framework";
import { BaseEmbedBuilder } from "../../libraries/structures/components/BaseEmbedBuilder";
import { ComponentType } from "discord.js";
import { deleteBtn } from "../../libraries/structures/components/Buttons";
import { clean } from "../../libraries/utils/common/text";
import { BaseEmbedBuilder, deleteBtn } from "../../libraries/structures/components";

export class EvalCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
Expand Down
9 changes: 8 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InternationalizationContext } from "@sapphire/plugin-i18next";
import { ClientOptions, GatewayIntentsString } from "discord.js";

/**
Expand Down Expand Up @@ -36,5 +37,11 @@ export const CLIENT_OPTIONS: ClientOptions = {
defaultCooldown: { delay: 3000 },
defaultPrefix: "/",
loadMessageCommandListeners: false,
enableLoaderTraceLoggings: false
enableLoaderTraceLoggings: false,
i18n: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fetchLanguage(context: InternationalizationContext) {
return "en-US";
}
}
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "@sapphire/plugin-logger/register";
import "@sapphire/plugin-i18next/register";

import "dotenv/config";
import { BaseClient } from "./libraries/structures/BaseClient";
import { BaseClient } from "./libraries/structures";

const main = (): void => {
if (!process.env.DISCORD_TOKEN) {
Expand Down
6 changes: 6 additions & 0 deletions src/languages/en-US/commands/ping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"PING_WAITING": "Ping?",
"PING_SUCCESS": "Pong 🏓!",
"PING_SUCCESS_DESCRIPTION": "**Discord API**: `{{ping}}`ms\n**Websocket**: `{{diff}}`ms",
"PING_FAILED": ":(, something went wrong."
}
3 changes: 3 additions & 0 deletions src/languages/en-US/listeners/interactionCreate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"USER_BARRED": "You are barred from using this command or the bot."
}
3 changes: 3 additions & 0 deletions src/languages/en-US/preconditions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"DEVELOPER_ONLY": "This command is only available to developers."
}
1 change: 1 addition & 0 deletions src/libraries/language/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as LanguageKeys from "./keys/index";
1 change: 1 addition & 0 deletions src/libraries/language/keys/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as Ping from "./ping";
4 changes: 4 additions & 0 deletions src/libraries/language/keys/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const PING_WAITING = "commands/ping:PING_WAITING";
export const PING_SUCCESS = "commands/ping:PING_SUCCESS";
export const PING_SUCCESS_DESCRIPTION = "commands/ping:PING_SUCCESS_DESCRIPTION";
export const PING_FAILED = "commands/ping:PING_FAILED";
3 changes: 3 additions & 0 deletions src/libraries/language/keys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as Commands from "./commands";
export * as Listeners from "./listeners";
export * as Preconditions from "./preconditions";
1 change: 1 addition & 0 deletions src/libraries/language/keys/listeners/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as InteractionCreate from "./interactionCreate";
1 change: 1 addition & 0 deletions src/libraries/language/keys/listeners/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USER_BARRED = "listeners/interactionCreate:USER_BARRED";
1 change: 1 addition & 0 deletions src/libraries/language/keys/preconditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEVELOPER_ONLY = "preconditions:DEVELOPER_ONLY";
2 changes: 2 additions & 0 deletions src/libraries/structures/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Buttons";
export * from "./BaseEmbedBuilder";
1 change: 1 addition & 0 deletions src/libraries/structures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./BaseClient";
8 changes: 5 additions & 3 deletions src/listeners/interaction/InteractionCreateListener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Listener } from "@sapphire/framework";
import { BaseInteraction, Events } from "discord.js";
import { parsers } from "../../libraries/utils";
import { BaseEmbedBuilder } from "../../libraries/structures/components/BaseEmbedBuilder";
import { BaseEmbedBuilder } from "../../libraries/structures/components";
import { resolveKey } from "@sapphire/plugin-i18next";
import { LanguageKeys } from "../../libraries/language";

export class InteractionCreateListener extends Listener {
public constructor(ctx: Listener.Context) {
Expand All @@ -11,14 +13,14 @@ export class InteractionCreateListener extends Listener {
});
}

public run(interaction: BaseInteraction) {
public async run(interaction: BaseInteraction) {
if (!interaction.isChatInputCommand()) return;

const barredUsers = parsers.parseBarredUserIDByFile(`${process.cwd()}/public/barredUsers.txt`);
if (barredUsers.includes(interaction.user.id)) {
const embed = new BaseEmbedBuilder()
.isErrorEmbed()
.setDescription("You are barred from using this command.");
.setDescription(await resolveKey(interaction, LanguageKeys.Listeners.InteractionCreate.USER_BARRED));

return interaction.reply({ content: "", embeds: [embed] });
}
Expand Down
13 changes: 9 additions & 4 deletions src/preconditions/DeveloperOnlyPrecondition.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Command, Precondition } from "@sapphire/framework";
import { DEV_USER_IDS } from "../config";
import { resolveKey } from "@sapphire/plugin-i18next";
import { LanguageKeys } from "../libraries/language";

export class DeveloperOnlyPrecondition extends Precondition {
public chatInputRun(interaction: Command.ChatInputCommandInteraction) {
return this.checkDev(interaction.user.id);
return this.checkDev(interaction, interaction.user.id);
}

public contextMenuRun(interaction: Command.ContextMenuCommandInteraction) {
return this.checkDev(interaction.user.id);
return this.checkDev(interaction, interaction.user.id);
}

checkDev(userId: string) {
async checkDev(
interaction: Command.ChatInputCommandInteraction | Command.ContextMenuCommandInteraction,
userId: string
) {
return DEV_USER_IDS.includes(userId)
? this.ok()
: this.error({ message: "You are not allowed to use this command." });
: this.error({ message: `${await resolveKey(interaction, LanguageKeys.Preconditions.DEVELOPER_ONLY)}` });
}
}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
".git/",
".github/",
".vscode/",
"dist/"
"build/"
],
"include": [ "src/**/*" ]
"include": [
"src/**/*"
]
}
Loading

0 comments on commit 28ff42e

Please sign in to comment.