Skip to content

Commit

Permalink
fix: get and post data from and to connection cache wherever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 18, 2024
1 parent e09127a commit 6741e9f
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 303 deletions.
2 changes: 1 addition & 1 deletion locales
Submodule locales updated 1 files
+1 −1 src/locales/en.yml
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"type": "module",
"dependencies": {
"@prisma/client": "^5.13.0",
"@sentry/node": "^7.114.0",
"@prisma/client": "^5.14.0",
"@sentry/node": "7",
"@tensorflow/tfjs-node": "^4.19.0",
"@top-gg/sdk": "^3.1.6",
"common-tags": "^1.8.2",
Expand All @@ -37,7 +37,7 @@
"lz-string": "^1.5.0",
"nsfwjs": "3.0.0",
"parse-duration": "^1.1.0",
"sharp": "^0.33.3",
"sharp": "^0.33.4",
"source-map-support": "^0.5.21",
"winston": "^3.13.0"
},
Expand All @@ -46,18 +46,18 @@
"@types/common-tags": "^1.8.4",
"@types/express": "^4.17.21",
"@types/js-yaml": "^4.0.9",
"@types/lodash": "^4.17.1",
"@types/node": "^20.12.11",
"@types/lodash": "^4.17.4",
"@types/node": "^20.12.12",
"@types/source-map-support": "^0.5.10",
"cz-conventional-changelog": "^3.3.0",
"eslint": "9.2.0",
"eslint": "9.3.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"prisma": "^5.13.0",
"prisma": "^5.14.0",
"standard-version": "^9.5.0",
"tsc-watch": "^6.2.0",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
"typescript-eslint": "^7.9.0"
},
"config": {
"commitizen": {
Expand Down
10 changes: 5 additions & 5 deletions src/api/routes/dbl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import 'dotenv/config';
// NOTE: testing this against top.gg only works in the production server
// to test locally use postman or something similar to send a POST request to http://localhost:443/dbl
const TopggWebhook = new Webhook(process.env.TOPGG_AUTH);
const router = Router();

export default (voteManager: VoteManager) => {
Router().post(
router.post(
'/dbl',
TopggWebhook.listener((vote) => {
// emit the vote event to use in other files
voteManager?.emit('vote', vote);
TopggWebhook.listener((payload) => {
voteManager.emit('vote', payload);
}),
);
return Router();
return router;
};
6 changes: 4 additions & 2 deletions src/commands/context-menu/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { simpleEmbed } from '../../utils/Utils.js';
import { stripIndents } from 'common-tags';
import { logBlacklist } from '../../utils/HubLogger/ModLogs.js';
import { deleteConnections } from '../../utils/ConnectedList.js';

export default class Blacklist extends BaseCommand {
readonly data: RESTPostAPIApplicationCommandsJSONBody = {
Expand Down Expand Up @@ -265,8 +266,9 @@ export default class Blacklist extends BaseCommand {
);
}

await db.connectedList.deleteMany({
where: { serverId: originalMsg.serverId, hubId: originalMsg.hubId },
await deleteConnections({
serverId: originalMsg.serverId,
hubId: originalMsg.hubId,
});

if (server) {
Expand Down
10 changes: 6 additions & 4 deletions src/commands/context-menu/messageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export default class MessageInfo extends BaseCommand {

const components = MessageInfo.buildButtons(target.id, interaction.user.locale);

const guildConnected = await db.connectedList.findFirst({
where: { serverId: originalMsg.serverId, hubId: originalMsg.hub?.id },
});
const guildConnected = interaction.client.connectionCache.find(
(c) => c.serverId === originalMsg.serverId && c.hubId === originalMsg.hub?.id,
);

if (guildConnected?.invite) {
components[1].addComponents(
Expand Down Expand Up @@ -124,7 +124,9 @@ export default class MessageInfo extends BaseCommand {

const author = await interaction.client.users.fetch(originalMsg.authorId);
const server = await interaction.client.fetchGuild(originalMsg.serverId);
const guildConnected = await db.connectedList.findFirst({ where: { serverId: server?.id } });
const guildConnected = interaction.client.connectionCache.find(
(c) => c.serverId === server?.id,
);

if (interaction.isButton()) {
// component builders taken from the original message
Expand Down
3 changes: 2 additions & 1 deletion src/commands/slash/Main/blacklist/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import parse from 'parse-duration';
import Logger from '../../../../utils/Logger.js';
import { t } from '../../../../utils/Locale.js';
import { logBlacklist, logUnblacklist } from '../../../../utils/HubLogger/ModLogs.js';
import { deleteConnections } from '../../../../utils/ConnectedList.js';

export default class UserBlacklist extends BlacklistCommand {
async execute(interaction: ChatInputCommandInteraction) {
Expand Down Expand Up @@ -135,7 +136,7 @@ export default class UserBlacklist extends BlacklistCommand {
.catch(() => null);

// delete all connections from db so they can't reconnect to the hub
await db.connectedList.deleteMany({ where: { serverId: server.id, hubId: hubInDb.id } });
await deleteConnections({ serverId: server.id, hubId: hubInDb.id });

// send log to hub's log channel
await logBlacklist(hubInDb.id, {
Expand Down
43 changes: 15 additions & 28 deletions src/commands/slash/Main/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from '../../../utils/Utils.js';
import { t } from '../../../utils/Locale.js';
import { connectedList } from '@prisma/client';
import { modifyConnection } from '../../../utils/ConnectedList.js';

export default class Connection extends BaseCommand {
readonly data: RESTPostAPIApplicationCommandsJSONBody = {
Expand Down Expand Up @@ -116,10 +117,7 @@ export default class Connection extends BaseCommand {
const channelExists = await interaction.guild?.channels.fetch(channelId).catch(() => null);

if (!channelExists) {
await db.connectedList.update({
where: { channelId: channelId },
data: { connected: !isInDb.connected },
});
await modifyConnection({ channelId }, { connected: !isInDb.connected });
await interaction.followUp({
content: t(
{ phrase: 'connection.channelNotFound', locale: interaction.user.locale },
Expand Down Expand Up @@ -198,10 +196,7 @@ export default class Connection extends BaseCommand {

// button interactions
if (interaction.isButton() && customId.suffix === 'toggle') {
const toggleRes = await db.connectedList.update({
where: { channelId },
data: { connected: !isInDb.connected },
});
const toggleRes = await modifyConnection({ channelId }, { connected: !isInDb.connected });

await interaction.update({
embeds: [await buildEmbed(interaction, channelId)],
Expand All @@ -211,7 +206,6 @@ export default class Connection extends BaseCommand {
],
});
}

else if (interaction.isStringSelectMenu()) {
Connection.executeStringSelects(interaction, channelId, isInDb);
}
Expand All @@ -230,7 +224,7 @@ export default class Connection extends BaseCommand {
const channelId = customId.args[0];

if (!invite) {
await db.connectedList.update({ where: { channelId }, data: { invite: { unset: true } } });
await modifyConnection({ channelId }, { invite: { unset: true } });
await interaction.followUp({
content: t(
{ phrase: 'connection.inviteRemoved', locale: interaction.user.locale },
Expand All @@ -254,7 +248,7 @@ export default class Connection extends BaseCommand {
return;
}

await db.connectedList.update({ where: { channelId }, data: { invite } });
await modifyConnection({ channelId }, { invite });

await interaction.followUp({
content: t(
Expand All @@ -279,10 +273,10 @@ export default class Connection extends BaseCommand {
return;
}

await db.connectedList.update({
where: { channelId: customId.args[0] },
data: { embedColor: embedColor ? embedColor : { unset: true } },
});
await modifyConnection(
{ channelId: customId.args[0] },
{ embedColor: embedColor ? embedColor : { unset: true } },
);

await interaction.reply({
content: t(
Expand All @@ -305,17 +299,11 @@ export default class Connection extends BaseCommand {
) {
switch (interaction.values[0]) {
case 'compact':
await db.connectedList.update({
where: { channelId },
data: { compact: !connection.compact },
});
await modifyConnection({ channelId }, { compact: !connection.compact });
break;

case 'profanity':
await db.connectedList.update({
where: { channelId },
data: { profFilter: !connection.profFilter },
});
await modifyConnection({ channelId }, { profFilter: !connection.profFilter });
break;

case 'invite': {
Expand Down Expand Up @@ -402,7 +390,6 @@ export default class Connection extends BaseCommand {
: await interaction.update({ embeds: [newEmbeds] });
}


static async executeChannelSelects(
interaction: ChannelSelectMenuInteraction,
channelId: string,
Expand Down Expand Up @@ -442,10 +429,10 @@ export default class Connection extends BaseCommand {
}

const newWebhook = await getOrCreateWebhook(newChannel as TextChannel | ThreadChannel);
await db.connectedList.update({
where: { channelId },
data: { channelId: newChannel?.id, webhookURL: newWebhook?.url },
});
await modifyConnection(
{ channelId },
{ channelId: newChannel?.id, webhookURL: newWebhook?.url },
);

await interaction.editReply({
content: t(
Expand Down
21 changes: 10 additions & 11 deletions src/commands/slash/Main/hub/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { stripIndents } from 'common-tags';
import BlacklistManager from '../../../../managers/BlacklistManager.js';
import { t } from '../../../../utils/Locale.js';
import { logServerJoin } from '../../../../utils/HubLogger/JoinLeave.js';
import { connectChannel } from '../../../../utils/ConnectedList.js';

export default class Browse extends Hub {
async execute(interaction: ChatInputCommandInteraction<CacheType>): Promise<void> {
Expand Down Expand Up @@ -374,17 +375,15 @@ export default class Browse extends Hub {
if (!webhook) return;

// finally make the connection
await db.connectedList.create({
data: {
serverId: channel.guildId,
channelId: channel.id,
parentId: channel.isThread() ? channel.parentId : undefined,
webhookURL: webhook.url,
hub: { connect: { id: hubDetails.id } },
connected: true,
compact: false,
profFilter: true,
},
await connectChannel({
serverId: channel.guildId,
channelId: channel.id,
parentId: channel.isThread() ? channel.parentId : undefined,
webhookURL: webhook.url,
hub: { connect: { id: hubDetails.id } },
connected: true,
compact: false,
profFilter: true,
});

await interaction.editReply({
Expand Down
21 changes: 10 additions & 11 deletions src/commands/slash/Main/hub/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { showOnboarding } from '../../../../scripts/network/onboarding.js';
import { stripIndents } from 'common-tags';
import { t } from '../../../../utils/Locale.js';
import { logServerJoin } from '../../../../utils/HubLogger/JoinLeave.js';
import { connectChannel } from '../../../../utils/ConnectedList.js';

export default class JoinSubCommand extends Hub {
async execute(interaction: ChatInputCommandInteraction): Promise<unknown> {
Expand Down Expand Up @@ -157,17 +158,15 @@ export default class JoinSubCommand extends Hub {
}

// finally make the connection
await db.connectedList.create({
data: {
serverId: channel.guildId,
channelId: channel.id,
parentId: channel.isThread() ? channel.parentId : undefined,
webhookURL: webhook.url,
hub: { connect: { id: hub.id } },
connected: true,
compact: false,
profFilter: true,
},
await connectChannel({
serverId: channel.guildId,
channelId: channel.id,
parentId: channel.isThread() ? channel.parentId : undefined,
webhookURL: webhook.url,
hub: { connect: { id: hub.id } },
connected: true,
compact: false,
profFilter: true,
});

await interaction.editReply({
Expand Down
3 changes: 2 additions & 1 deletion src/commands/slash/Main/hub/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { emojis } from '../../../../utils/Constants.js';
import { simpleEmbed, setComponentExpiry } from '../../../../utils/Utils.js';
import { t } from '../../../../utils/Locale.js';
import { logServerLeave } from '../../../../utils/HubLogger/JoinLeave.js';
import { deleteConnection } from '../../../../utils/ConnectedList.js';

export default class Leave extends Hub {
async execute(interaction: ChatInputCommandInteraction<CacheType>) {
Expand Down Expand Up @@ -103,7 +104,7 @@ export default class Leave extends Hub {
return;
}

await db.connectedList.delete({ where: { channelId } });
await deleteConnection({ channelId });
await interaction.update({
content: t(
{ phrase: 'hub.leave.success', locale },
Expand Down
32 changes: 9 additions & 23 deletions src/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ClusterClient, getInfo } from 'discord-hybrid-sharding';
import { commandsMap, interactionsMap } from './BaseCommand.js';
import db from '../utils/Db.js';
import Sentry, { captureException } from '@sentry/node';
import Sentry from '@sentry/node';
import Scheduler from '../services/SchedulerService.js';
import CooldownService from '../services/CooldownService.js';
import BlacklistManager from '../managers/BlacklistManager.js';
Expand All @@ -20,16 +20,14 @@ import { isDevBuild } from '../utils/Constants.js';
import { ActivityType } from 'discord.js';
import 'dotenv/config';
import { loadLocales, supportedLocaleCodes } from '../utils/Locale.js';
import { connectedList } from '@prisma/client';
import Logger from '../utils/Logger.js';
import loadCommandFiles from '../utils/LoadCommands.js';
import { connectionCache as _connectionCache, syncConnectionCache } from '../utils/ConnectedList.js';

export default class SuperClient<R extends boolean = boolean> extends Client<R> {
// A static instance of the SuperClient class to be used globally.
public static instance: SuperClient;

private _connectionCache: Collection<string, connectedList> = new Collection();
private _cachePopulated = false;
private _connectionCachePopulated = false;
private readonly scheduler = new Scheduler();

readonly description = 'The only cross-server chatting bot you\'ll ever need.';
Expand Down Expand Up @@ -108,31 +106,19 @@ export default class SuperClient<R extends boolean = boolean> extends Client<R>
// load commands
await loadCommandFiles();

await this.populateConnectionCache();
this.getScheduler().addRecurringTask('populateConnectionCache', 60_000 * 5, async () => {
await this.populateConnectionCache().catch(captureException);
});
await syncConnectionCache();
this._connectionCachePopulated = true;

await this.login(process.env.TOKEN);
}
this.getScheduler().addRecurringTask('populateConnectionCache', 60_000 * 5, syncConnectionCache);

protected async populateConnectionCache() {
Logger.debug('[InterChat]: Populating connection cache.');
const connections = await db.connectedList.findMany({ where: { connected: true } });

// populate all at once without time delay
this._connectionCache = new Collection(connections.map((c) => [c.channelId, c]));
this._cachePopulated = true;
Logger.debug(
`[InterChat]: Connection cache populated with ${this._connectionCache.size} entries.`,
);
await this.login(process.env.TOKEN);
}

public get connectionCache() {
return this._connectionCache;
return _connectionCache;
}
public get cachePopulated() {
return this._cachePopulated;
return this._connectionCachePopulated;
}

static resolveEval = <T>(value: T[]) =>
Expand Down
Loading

0 comments on commit 6741e9f

Please sign in to comment.