Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Sharding #29

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/signale": "^1.4.2",
"chalk": "4.1.2",
"discord-api-types": "^0.25.2",
"discord-hybrid-sharding": "^1.3.6",
"discord.js": "^13.3.1",
"dotenv": "^10.0.0",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -41,4 +42,4 @@
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
}
}
53 changes: 53 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Client, Intents } from "discord.js";
import dotenv from "dotenv";
import * as commands from "./commands";
import * as events from "./events";
import { ErrorEmbed } from "./lib/discordEmbeds";
import { logger } from "./lib/logger";
import { db } from "./lib/prisma";
import { scheduler } from "./lib/scheduler";
dotenv.config();

// Create a new client instance
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
],
});

const eventList = Object.values(events);

client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
try {
await commands[interaction.commandName].execute(interaction);
} catch (e) {
logger.error(e);
interaction.reply({
embeds: [ErrorEmbed("There was an error while executing this command!")],
ephemeral: true,
});
}
});

// Register event handlers
for (const event of eventList) {
if (event.once) {
client.once(event.name, (...args: any) => event.execute(...args));
} else {
client.on(event.name, (...args: any) => event.execute(...args));
}
}

// Login to Discord with your client's token
client.login(process.env.TOKEN);

// Handle stop signal
process.on("SIGINT", () => {
client.destroy();
scheduler.stop();
db.$disconnect();
logger.info("Bot Stopped");
});
63 changes: 13 additions & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
import { Client, Intents } from "discord.js";
import dotenv from "dotenv";
import * as commands from "./commands";
import * as events from "./events";
import { ErrorEmbed } from "./lib/discordEmbeds";
import { Manager } from "discord-hybrid-sharding";
import { config } from "dotenv";
import { logger } from "./lib/logger";
import { db } from "./lib/prisma";
import { scheduler } from "./lib/scheduler";
dotenv.config();

// Create a new client instance
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
],
});

const eventList = Object.values(events);

client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
try {
await commands[interaction.commandName].execute(interaction);
} catch (e) {
logger.error(e);
interaction.reply({
embeds: [ErrorEmbed("There was an error while executing this command!")],
ephemeral: true,
});
}
config();
const client = new Manager("dist/bot.js", {
totalShards: 2,
totalClusters: 1,
mode: "process",
usev13: true,
token: process.env.TOKEN,
});

// Register event handlers
for (const event of eventList) {
if (event.once) {
client.once(event.name, (...args: any) => event.execute(...args));
} else {
client.on(event.name, (...args: any) => event.execute(...args));
}
}

// Login to Discord with your client's token
client.login(process.env.TOKEN);

// Handle stop signal
process.on("SIGINT", () => {
client.destroy();
scheduler.stop();
db.$disconnect();
logger.info("Bot Stopped");
});
client.on("clusterCreate", (cluster) =>
logger.info(`Cluster ${cluster.id} Has Been Launched, Now Starting Bot`)
);
client.spawn(undefined, undefined, -1);
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ discord-api-types@^0.25.2:
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.25.2.tgz#e50ed152e6d48fe7963f5de1002ca6f2df57c61b"
integrity sha512-O243LXxb5gLLxubu5zgoppYQuolapGVWPw3ll0acN0+O8TnPUE2kFp9Bt3sTRYodw8xFIknOVxjSeyWYBpVcEQ==

discord-hybrid-sharding@^1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/discord-hybrid-sharding/-/discord-hybrid-sharding-1.3.6.tgz#5b2ae3834b85a102f5c11a1fd5681024f03abb3c"
integrity sha512-K+uKa+SFKYnCRfdb3FlkDLnx1uTHBLqBls3oy92relJMooZbNblt3DuY83IabC7gKYPtH8NuWF51ZTlbg2M3hg==
dependencies:
node-fetch "^2.6.1"

discord.js@^13.3.1:
version "13.3.1"
resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-13.3.1.tgz#94fe05bc3ec0a3e4761e4f312a2a418c29721ab6"
Expand Down