Skip to content

Commit

Permalink
fix: fix deepsource errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Dec 11, 2024
1 parent d2ca570 commit 43a2ce6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
36 changes: 21 additions & 15 deletions src/commands/context-menu/deleteMsg.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Constants, { emojis } from '#utils/Constants.js';
import BaseCommand from '#main/core/BaseCommand.js';
import db from '#main/utils/Db.js';
import HubManager from '#main/managers/HubManager.js';
import { HubService } from '#main/services/HubService.js';
import {
findOriginalMessage,
getBroadcasts,
getOriginalMessage,
OriginalMessage,
} from '#main/utils/network/messageUtils.js';
import { isStaffOrHubMod } from '#utils/hub/utils.js';
import Constants, { emojis } from '#utils/Constants.js';
import { logMsgDelete } from '#utils/hub/logger/ModLogs.js';
import { isStaffOrHubMod } from '#utils/hub/utils.js';
import { t } from '#utils/Locale.js';
import { deleteMessageFromHub, isDeleteInProgress } from '#utils/moderation/deleteMessage.js';
import { Hub, HubLogConfig } from '@prisma/client';
import {
ApplicationCommandType,
InteractionContextType,
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class DeleteMessage extends BaseCommand {
private async processMessageDeletion(
interaction: MessageContextMenuCommandInteraction,
originalMsg: OriginalMessage,
hub: Hub & { logConfig: HubLogConfig[] },
hub: HubManager,
): Promise<void> {
const { userManager } = interaction.client;
const locale = await userManager.getUserLocale(interaction.user.id);
Expand All @@ -77,15 +77,15 @@ export default class DeleteMessage extends BaseCommand {
await this.logDeletion(interaction, hub, originalMsg);
}

private async fetchHub(hubId?: string) {
private async fetchHub(hubId?: string): Promise<HubManager | null> {
if (!hubId) return null;
return await db.hub.findUnique({ where: { id: hubId }, include: { logConfig: true } });
return await new HubService().fetchHub(hubId);
}

private async validateMessage(
interaction: MessageContextMenuCommandInteraction,
originalMsg: OriginalMessage | null,
hub: (Hub & { logConfig: HubLogConfig[] }) | null,
hub: HubManager | null,
) {
const { userManager } = interaction.client;
const locale = await userManager.getUserLocale(interaction.user.id);
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class DeleteMessage extends BaseCommand {

private async logDeletion(
interaction: MessageContextMenuCommandInteraction,
hub: Hub & { logConfig: HubLogConfig[] },
hub: HubManager,
originalMsg: OriginalMessage,
): Promise<void> {
if (!isStaffOrHubMod(interaction.user.id, hub)) return;
Expand All @@ -132,11 +132,17 @@ export default class DeleteMessage extends BaseCommand {
targetMessage.embeds.at(0)?.image?.url ??
targetMessage.content.match(Constants.Regex.ImageURL)?.[0];

await logMsgDelete(interaction.client, messageContent, hub, {
userId: originalMsg.authorId,
serverId: originalMsg.guildId,
modName: interaction.user.username,
imageUrl,
});
await logMsgDelete(
interaction.client,
messageContent,
hub.data.name,
await hub.fetchLogConfig(),
{
userId: originalMsg.authorId,
serverId: originalMsg.guildId,
modName: interaction.user.username,
imageUrl,
},
);
}
}
6 changes: 3 additions & 3 deletions src/managers/HubManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ export default class HubManager {
private async storeInCache(key: string, data: BlockWord[] | HubModerator[]) {
const multi = this.cache.multi();
multi.del(key);
data.forEach(async (bw) => multi.sadd(key, JSON.stringify(bw)));
data.forEach((bw) => multi.sadd(key, JSON.stringify(bw)));
multi.expire(key, 60 * 60 * 24); //
await multi.exec();
}

async isManager(userId: string) {
return this.modManager.checkStatus(userId, ['MANAGER', 'OWNER']);
return await this.modManager.checkStatus(userId, ['MANAGER', 'OWNER']);
}

async isMod(userId: string) {
return this.modManager.checkStatus(userId);
return await this.modManager.checkStatus(userId);
}
}
40 changes: 27 additions & 13 deletions src/services/HubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,35 @@ export class HubService {
return null;
}

async fetchHub(where: { name: string; }): Promise<HubManager | null>;
async fetchHub(where: { id: string; }): Promise<HubManager | null>;
async fetchHub(where: { name: string, id: string }): Promise<HubManager | null>;
async fetchHub(id: string): Promise<HubManager | null>;
async fetchHub(where: string | { name?: string, id?: string }) {
const id = typeof where === 'string' ? where : where.id;
const name = typeof where === 'string' ? undefined : where.name;
async fetchHub(where: { id: string }): Promise<HubManager | null>;
async fetchHub(where: { name: string }): Promise<HubManager | null>;
async fetchHub(whereInput: string | { id?: string; name?: string }): Promise<HubManager | null> {
const where: { id?: string; name?: string } = typeof whereInput === 'string'
? { id: whereInput }
: whereInput;

if (!where.id && !where.name) {
return null;
}

// Check cache if we have an ID
if (where.id) {
const fromCache = await this.cache.get(`${this.hubKey}${where.id}`);
if (fromCache) {
return this.createHubManager(fromCache);
}
}

const hub = await this.db.hub.findFirst({ where });

// Cache result if we found something
if (hub) {
await this.cache.set(`${this.hubKey}${hub.id}`, JSON.stringify(hub));
return this.createHubManager(hub);
}

if (!id && !name) return null;

const fromCache = await this.cache.get(`${this.hubKey}${id}`);
if (fromCache) return this.createHubManager(fromCache);

const hub = await this.db.hub.findFirst({ where: { id, name } });
return this.createHubManager(hub);
return null;
}

async createHub(data: HubCreationData): Promise<HubManager> {
Expand Down

0 comments on commit 43a2ce6

Please sign in to comment.