Skip to content

Commit f848868

Browse files
authored
refactor!: remove global context, update command/event attachment (#59)
* refactor!: remove global context, update command/event attachment BREAKING CHANGE: The global context module has been removed. Commands and events must now access the client and logger via instance properties after being attached. The `prefix` option is now a function. Update your usage accordingly. * refactor(core): limit export factory recursion depth Add MAX_EXPORT_FACTORY_DEPTH constant and enforce it in registerExport to prevent infinite recursion when registering export factories. Rename messages.ts to lang.ts and update imports. Remove unused prefix log and package.json imports field. * chore(utils): simplify getDefaultLang return logic
1 parent 62bf283 commit f848868

28 files changed

Lines changed: 725 additions & 173 deletions

File tree

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ src
1313
scripts
1414
tsconfig.json
1515
.oxfmtrc.json
16+
.prettierrc
1617
package-lock.json
17-
.swcrc
18+
.swcrc
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const {
2+
Command,
3+
ApplicationCommandBuilder,
4+
} = require("../../../dist/index.cjs");
5+
6+
module.exports = new Command({
7+
data: new ApplicationCommandBuilder()
8+
.setName("ping")
9+
.setDescription("Replies with pong")
10+
.setAliases("p")
11+
.setPrefixSupport(true)
12+
.setSlashSupport(true),
13+
onMessage: (ctx) =>
14+
ctx.message.reply(
15+
ctx.t("test:hello", {
16+
user: ctx.message.author.username,
17+
})
18+
),
19+
onInteraction: (ctx) =>
20+
ctx.interaction.reply(
21+
ctx.t("test:hello", { user: ctx.interaction.user.username })
22+
),
23+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { EventBuilder } = require("../../../dist/index.cjs");
2+
3+
module.exports = new EventBuilder("clientReady", false, (context) => {
4+
context.logger.log("Client connected!");
5+
context.logger.warn(`Current user: ${context.client.user.username}`);
6+
});

examples/basic_client/events/ready.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/basic_client/index.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
1010

1111
const myinstance = i18next.createInstance({
12-
supportedLngs: ["en-US", "tr"],
12+
supportedLngs: ["en-US", "tr"], // Required to be `${Locale}` (LocaleString)
1313
fallbackLng: "en-US",
1414
defaultNS: "translation",
1515
ns: ["translation", "test", "error"],
@@ -28,37 +28,18 @@ const client = new arox.Client({
2828
IntentsBitField.Flags.GuildMessages,
2929
IntentsBitField.Flags.MessageContent,
3030
],
31-
prefix: { enabled: true, prefix: "a!" },
31+
prefix: () => "a!",
32+
includePaths: [
33+
path.join(__dirname, "events"),
34+
path.join(__dirname, "commands"),
35+
],
3236
logger: {
3337
level: arox.LogLevel.Debug,
3438
},
3539
autoRegisterCommands: false,
3640
i18n: myinstance,
3741
});
3842

39-
arox.setClient(client);
40-
const command = new arox.CommandBuilder(
41-
new arox.ApplicationCommandBuilder()
42-
.setName("arox")
43-
.setDescription("Arox Test Command")
44-
.addAliases("a")
45-
);
46-
arox.clearClient();
47-
48-
command
49-
.onMessage(function (ctx) {
50-
const { message, t, author } = ctx;
51-
void message.reply(
52-
t("test:hello", { user: author?.username ?? "Unknown" })
53-
);
54-
})
55-
.onInteraction(function (ctx) {
56-
const { interaction, t, author } = ctx;
57-
void interaction.reply(
58-
t("test:hello", { user: author?.username ?? "Unknown" })
59-
);
60-
});
61-
6243
async function init() {
6344
const token = process.env.DISCORD_TOKEN ?? process.env.BOT_TOKEN;
6445
await client.login(token);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"command.notfound": "Command not found or disabled1",
3-
"command.disabled": "Command not found or disabled2"
2+
"command": {
3+
"notfound": "Command not found or disabled1",
4+
"disabled": "Command not found or disabled2"
5+
}
46
}

0 commit comments

Comments
 (0)