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

Commit

Permalink
feat: sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlolDev committed Apr 2, 2023
1 parent d9a0553 commit 20fd99e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
20 changes: 20 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Require the necessary discord.js classes
import { Client, Collection, GatewayIntentBits } from "discord.js";
import "dotenv/config";
import eventHandler from "./handlers/events.js";
import commandHandler from "./handlers/commands.js";
import interactionsHandler from "./handlers/interactions.js";
import { REST, Routes } from "discord.js";
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

const client: any = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
client.interactions = new Collection();
client.guildsVoice = [];

// Handlers
eventHandler(client);
commandHandler(client);
interactionsHandler(client);

client.login(process.env.TOKEN);
31 changes: 23 additions & 8 deletions src/commands/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ export default {
const __dirname = path.dirname(__filename);

await interaction.deferReply();
var shard = client.shard.client.options.shards[0] + 1;

var usersCount = 0;
var users = client.guilds.cache.map((guild) => guild.memberCount);
for (var i = 0; i < users.length; i++) {
usersCount += users[i];
}

var totalGuildsR = await client.shard.fetchClientValues(
"guilds.cache.size"
);
const totalGuilds = totalGuildsR.reduce(
(acc, guildCount) => acc + guildCount,
0
);
var totalMembersR = await client.shard.broadcastEval((c) =>
c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)
);
const totalMembers = totalMembersR.reduce(
(acc, memberCount) => acc + memberCount,
0
);
var embed = new EmbedBuilder()
.setColor("#347d9c")
.setTimestamp()
Expand All @@ -42,14 +51,15 @@ export default {
},
{
name: "Servers",
value: `${client.guilds.cache.size}`,
value: `${totalGuilds}`,
inline: true,
},
{
name: "Users",
value: `${usersCount}`,
value: `${totalMembers}`,
inline: true,
},

{
name: "Created At",
value: `${timeString}`,
Expand All @@ -67,6 +77,11 @@ export default {
)} MB`,
inline: true,
},
{
name: "Shard",
value: `${shard}/${client.shard.count}`,
inline: true,
},
{
name: "Version",
value: `v0.0.7`,
Expand Down
24 changes: 6 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
// Require the necessary discord.js classes
import { Client, Collection, GatewayIntentBits } from "discord.js";
import { ShardingManager } from "discord.js";
import "dotenv/config";
import eventHandler from "./handlers/events.js";
import commandHandler from "./handlers/commands.js";
import interactionsHandler from "./handlers/interactions.js";
import { REST, Routes } from "discord.js";
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
const manager = new ShardingManager("./dist/bot.js", {
token: process.env.TOKEN,
});

const client: any = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
client.interactions = new Collection();
client.guildsVoice = [];

// Handlers
eventHandler(client);
commandHandler(client);
interactionsHandler(client);

client.login(process.env.TOKEN);
manager.on("shardCreate", (shard) => console.log(`Launched shard ${shard.id}`));
manager.spawn();

0 comments on commit 20fd99e

Please sign in to comment.