Skip to content

Commit

Permalink
feature: enable sharding (#133)
Browse files Browse the repository at this point in the history
* feat: basic sharding implementation

suffering from success.

* style: format with prettier
  • Loading branch information
RalphORama authored Oct 6, 2024
1 parent 4918565 commit a155452
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
31 changes: 30 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { error, info, initLogger, loggerAvailable } from "../logging";
import { getEnvironmentMode } from "../environment";
import { initI18n } from "../i18n";
import { createCommands } from "../commands";

import { Client, Collection, Events, GatewayIntentBits } from "discord.js";

import { CustomCommand } from "../@types/CustomCommand";
import { replacements } from "../replacements";
import { error, info } from "../logging";

export function createClient(commands: CustomCommand[]): Client {
const replacementsEntries = Object.entries(replacements);
Expand Down Expand Up @@ -103,3 +107,28 @@ export function createClient(commands: CustomCommand[]): Client {

return client;
}

async function main(): Promise<void> {
const environmentMode = getEnvironmentMode();
initLogger(environmentMode, "bot");

const locale = process.env.LOCALE ?? "";
await initI18n(locale);

const commands = createCommands();
const client = createClient(commands);

await client.login(process.env.DISCORD_BOT_TOKEN);
}

main()
.then()
.catch((e: Error) => {
if (loggerAvailable()) {
// TODO: Refactor `error()` parameters to accept an `Error` object
error(`Exception thrown from main:\n ${e.name}: ${e.message}!`);
return;
}

console.error(`Exception thrown from main:\n ${e.name}: ${e.message}!`);
});
37 changes: 12 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import dotenv from "dotenv";

import { error, initLogger, loggerAvailable } from "./logging";
import { initI18n } from "./i18n";
import { createCommands } from "./commands";
import { createClient } from "./client";
import { info, initLogger } from "./logging";
import { getEnvironmentMode } from "./environment";

async function main(): Promise<void> {
dotenv.config();
import { ShardingManager } from "discord.js";

const environmentMode = getEnvironmentMode();
initLogger(environmentMode, "bot");
dotenv.config();

const locale = process.env.LOCALE ?? "";
await initI18n(locale);
const manager = new ShardingManager("./dist/client/index.js", {
token: process.env.DISCORD_BOT_TOKEN,
});

const commands = createCommands();
const client = createClient(commands);
const environmentMode = getEnvironmentMode();
initLogger(environmentMode, "bot");

await client.login(process.env.DISCORD_BOT_TOKEN);
}
manager.on("shardCreate", (shard) => {
info(`Launched shard ${shard.id}`);
});

main()
.then()
.catch((e: Error) => {
if (loggerAvailable()) {
// TODO: Refactor `error()` parameters to accept an `Error` object
error(`Exception thrown from main:\n ${e.name}: ${e.message}!`);
return;
}

console.error(`Exception thrown from main:\n ${e.name}: ${e.message}!`);
});
void manager.spawn();
6 changes: 0 additions & 6 deletions src/replacements/BaseReplacement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { debug } from "../logging";

export default class BaseReplacement {
protected newDomain: string;

Expand Down Expand Up @@ -64,10 +62,6 @@ export default class BaseReplacement {
c = c.replace(/\?\w+=.*$/gm, "");
}

if (process.env.LINKFIX_DEBUG) {
debug(`replaceURLs()\t${url}\t${c}`, this.constructor.name);
}

return c;
})
.join("\n");
Expand Down

0 comments on commit a155452

Please sign in to comment.