Skip to content

Commit

Permalink
chore: clusterClientInstance replacing shard broadcastEval's
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhuh committed May 25, 2024
1 parent fc47afd commit 86967f0
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
24 changes: 11 additions & 13 deletions commands/general/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'discord.js';
import { AVATAR_URL, INVITE_URL, SUPPORT_URL, VOTE_URL } from '../../src/vars';
import { SlashCommand } from '../command';
import { clusterClientInstance } from '../../src/bot-start';

export class InfoBaseCommand extends SlashCommand {
constructor() {
Expand All @@ -34,38 +35,35 @@ export class InfoBaseCommand extends SlashCommand {
execute = async (interaction: ChatInputCommandInteraction) => {
const embed = new EmbedBuilder();
const [size, memberCount] = await Promise.all([
interaction.client.shard?.fetchClientValues('guilds.cache.size'),
interaction.client.shard?.broadcastEval((c) =>
clusterClientInstance.fetchClientValues('guilds.cache.size'),
clusterClientInstance.broadcastEval((c) =>
c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0),
),
]);

const buttons = this.buttons();
let emoji;

const ping = Math.floor(interaction.client.ws.ping);

if (ping > 0) emoji = ' <:rolebot__goodping:1044464966973001819>';
if (ping > 125) emoji = '<:rolebot__idleping:1044466765117272094>';
if (ping > 250) emoji = '<:rolebot__badping:1044466766270701619>';
// Assume "good" ping.
let emoji = '🟢'
if (ping > 125) emoji = '🟡';
if (ping > 250) emoji = '🔴';

embed
.setTitle('General Info')
.setColor(Colors.Blurple)
.addFields(
{
name: '<:rolebot_people:1044464965618253895> Shard ID',
name: '🫂 Shard ID',
value: `This servers shard is ${interaction.guild?.shardId}`,
},
{
name: '<:rolebot_people:1044464965618253895> Server count',
value: `RoleBot is in ${size?.reduce<number>(
(acc, guildCount) => acc + Number(guildCount),
0,
)} servers.`,
name: '🫂 Server count',
value: `RoleBot is in ${size} servers.`,
},
{
name: '<:rolebot_people:1044464965618253895> Total Member count',
name: '🫂 Total Member count',
value: `RoleBot has ${memberCount?.reduce<number>(
(acc, memberCount) => acc + Number(memberCount),
0,
Expand Down
3 changes: 2 additions & 1 deletion commands/react/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { RolePing } from '../../utilities/utilPings';
import { isValidRolePosition } from '../../utilities/utils';
import { SlashSubCommand } from '../command';
import { clusterClientInstance } from '../../src/bot-start';

const enum CommandOptionNames {
Role = 'role',
Expand Down Expand Up @@ -264,7 +265,7 @@ export class CreateSubcommand extends SlashSubCommand {
) {
try {
const emojis = (
await interaction.client.shard?.broadcastEval((c) =>
await clusterClientInstance.broadcastEval((c) =>
c.guilds.cache.map((g) => g.emojis.cache),
)
)?.flat(3);
Expand Down
7 changes: 2 additions & 5 deletions events/guildUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import RoleBot from '../src/bot';
import { Colors, EmbedBuilder, Guild, WebhookClient } from 'discord.js';
import { CREATE_GUILD_CONFIG } from '../src/database/queries/guild.query';
import { clusterClientInstance } from '../src/bot-start';

export const guildUpdate = async (
guild: Guild,
type: 'Left' | 'Joined',
client: RoleBot,
) => {
const color = type === 'Joined' ? Colors.Green : Colors.Red;
try {
Expand All @@ -21,9 +20,7 @@ export const guildUpdate = async (
url: process.env.GUILD_WEBHOOK,
});

const size = (
await client.shard?.fetchClientValues('guilds.cache.size')
)?.reduce<number>((a, b) => a + Number(b), 0);
const size = await clusterClientInstance.fetchClientValues('guilds.cache.size');

const embed = new EmbedBuilder();

Expand Down
11 changes: 6 additions & 5 deletions src/bot-start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import RB from './bot';
import { RoleBot } from './bot';
import { ClusterClient } from 'discord-hybrid-sharding';

const RoleBot = new RB();
const botInstance = new RoleBot();

RoleBot.start().catch((e) =>
RoleBot.log.error(`RoleBot has encounter an error while starting up. ${e}`),
botInstance.start().catch((e) =>
botInstance.log.error(`RoleBot has encounter an error while starting up. ${e}`),
);

export default RoleBot;
export const clusterClientInstance = new ClusterClient(botInstance);
2 changes: 1 addition & 1 deletion src/services/interactionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from 'discord.js';
import { SUPPORT_URL } from '../vars';
import { LogService } from './logService';
import RoleBot from '../bot';
import { handleInteractionReply } from '../../utilities/utils';
import { ButtonHandler } from './buttonHandler';
import { RoleBot } from '../bot';

export class InteractionHandler {
public static log = new LogService('InteractionHandler');
Expand Down
2 changes: 1 addition & 1 deletion src/services/permissionService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChannelType, PermissionsBitField } from 'discord.js';
import RoleBot from '../bot';
import { CLIENT_ID } from '../vars';
import { LogService } from './logService';
import { RoleBot } from '../bot';

export enum HasPerms {
error = 1,
Expand Down
5 changes: 4 additions & 1 deletion src/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const INVITE_URL = `https://discord.com/oauth2/authorize?client_id=741682
export const TOKEN: string = process.env.TOKEN || '';
export const DB_NAME = process.env.DB_NAME || 'rolebotBeta';
export const POSTGRES_URL = `${process.env.POSTGRES_URL}${DB_NAME}` || '';
export const SHARD_COUNT = Number(process.env.SHARD_COUNT) || 6;
export const POSTGRES_HOST = process.env.POSTGRES_HOST || '';
export const POSTGRES_USER = process.env.POSTGRES_USER || '';
export const POSTGRES_PASSWORD = process.env.POSTGRES_PASSWORD || '';
export const POSTGRES_DATABASE = process.env.POSTGRES_DATABASE || '';
export const SERVER_ID = '567819334852804626';
// Only sync when in dev
export const SYNC_DB = Boolean(Number(process.env.SYNC_DB)) || false;
Expand Down
2 changes: 1 addition & 1 deletion utilities/types/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SlashCommandBuilder, SlashCommandSubcommandsOnlyBuilder } from '@discordjs/builders';
import { ChatInputCommandInteraction } from 'discord.js';
import RoleBot from '../../src/bot';
import { RoleBot } from '../../src/bot';

export enum Category {
general = 'general',
Expand Down

0 comments on commit 86967f0

Please sign in to comment.