Skip to content

Commit

Permalink
fix: fixed deletion of old messages function
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Jan 30, 2024
1 parent 9d09f06 commit 41cff8b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/scripts/tasks/deleteExpiredInvites.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import db from '../../utils/Db.js';

const deleteExpiredInvites = async () => {
export default async () => {
const olderThan1h = new Date(Date.now() - 60 * 60 * 1_000);
await db.hubInvites.deleteMany({ where: { expires: { lte: olderThan1h } } }).catch(() => null);
};
export default deleteExpiredInvites;
18 changes: 9 additions & 9 deletions src/scripts/tasks/deleteOldMessages.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { captureException } from '@sentry/node';
import db from '../../utils/Db.js';
import { deleteMsgsFromDb } from '../../utils/Utils.js';

// Delete all network messages from db that are older than 24 hours old.
const deleteOldMessages = async () => {
export default async () => {
const olderThan24h = Date.now() - 60 * 60 * 24_000;

db.broadcastedMessages
.findMany({ where: { createdAt: { lte: olderThan24h } } })
.then(async (m) => deleteMsgsFromDb(m.map(({ messageId }) => messageId)))
.catch(captureException);
};
const data = await db.broadcastedMessages.findMany({
where: { createdAt: { lte: olderThan24h } },
});
await db.broadcastedMessages.deleteMany({ where: { createdAt: { lte: olderThan24h } } });

export default deleteOldMessages;
await db.originalMessages.deleteMany({
where: { messageId: { in: data.map((d) => d.originalMsgId) } },
});
};
5 changes: 1 addition & 4 deletions src/scripts/tasks/syncBotlistStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Logger from '../../utils/Logger.js';
import { updateTopGGStats } from '../../updater/StatsUpdater.js';
import { ClusterManager } from 'discord-hybrid-sharding';

// other jobs
const syncBotlistStats = async (manager: ClusterManager) => {
export default async (manager: ClusterManager) => {
const count = (await manager.fetchClientValues('guilds.cache.size')) as number[];
Logger.info(
`Updated top.gg stats with ${count.reduce((p, n) => p + n, 0)} guilds and ${
Expand All @@ -16,5 +15,3 @@ const syncBotlistStats = async (manager: ClusterManager) => {
manager.totalShards,
);
};

export default syncBotlistStats;
7 changes: 1 addition & 6 deletions src/scripts/tasks/updateBlacklists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { blacklistedServers, userData } from '@prisma/client';
import BlacklistManager from '../../managers/BlacklistManager.js';
import Scheduler from '../../services/SchedulerService.js';

const updateBlacklists = async (
blacklists: (blacklistedServers | userData)[],
scheduler: Scheduler,
) => {
export default async (blacklists: (blacklistedServers | userData)[], scheduler: Scheduler) => {
if (blacklists.length === 0) return;

const blacklistManager = new BlacklistManager(scheduler);
Expand Down Expand Up @@ -33,5 +30,3 @@ const updateBlacklists = async (
}
}
};

export default updateBlacklists;
34 changes: 23 additions & 11 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import {
TextChannel,
ThreadChannel,
} from 'discord.js';
import { DeveloperIds, REGEX, StaffIds, SupporterIds, LINKS, colors, emojis, SUPPORT_SERVER_ID } from './Constants.js';
import {
DeveloperIds,
REGEX,
StaffIds,
SupporterIds,
LINKS,
colors,
emojis,
SUPPORT_SERVER_ID,
} from './Constants.js';
import { randomBytes } from 'crypto';
import { t } from './Locale.js';
import 'dotenv/config';
Expand Down Expand Up @@ -386,13 +395,16 @@ export const modifyUserRole = async (
guildId: Snowflake,
roleId: Snowflake,
) => {
await cluster.broadcastEval(async (client, ctx) => {
const guild = client.guilds.cache.get(ctx.guildId);
const voterRole = guild?.roles.cache.find(({ id }) => id === ctx.roleId);
const member = await guild?.members.fetch(ctx.userId).catch(() => null);
if (!guild || !voterRole) return;

// add or remove role
await member?.roles[ctx.action](voterRole).catch(() => null);
}, { guildId: SUPPORT_SERVER_ID, context: { userId, roleId, guildId, action } });
};
await cluster.broadcastEval(
async (client, ctx) => {
const guild = client.guilds.cache.get(ctx.guildId);
const voterRole = guild?.roles.cache.find(({ id }) => id === ctx.roleId);
const member = await guild?.members.fetch(ctx.userId).catch(() => null);
if (!guild || !voterRole) return;

// add or remove role
await member?.roles[ctx.action](voterRole).catch(() => null);
},
{ guildId: SUPPORT_SERVER_ID, context: { userId, roleId, guildId, action } },
);
};

0 comments on commit 41cff8b

Please sign in to comment.