Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Dec 12, 2024
1 parent 793f4ce commit 2338575
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/commands/slash/Main/hub/blockwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export default class BlockWordCommand extends HubCommand {
private async fetchHub({ id, name }: { id?: string; name?: string }) {
if (id) return await this.hubService.fetchHub(id);
else if (name) return (await this.hubService.findHubsByName(name)).at(0);
return null;
}

private async replyWithNotFound(interaction: RepliableInteraction) {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/InfractionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ export default class InfractionManager {
}

public isExpiredInfraction(infraction: Infraction | null) {
return !infraction || (!!infraction.expiresAt && infraction.expiresAt <= new Date());
return !infraction?.expiresAt || infraction.expiresAt <= new Date();
}
}
13 changes: 5 additions & 8 deletions src/services/HubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class HubService {
this.cache = cache;
}

protected serializeHub(hub: string): Hub {
protected parseHubStringToObject(hub: string): Hub {
const parsedHub = JSON.parse(hub) as ConvertDatesToString<Hub>;

return {
Expand All @@ -36,13 +36,10 @@ export class HubService {
};
}

private createHubManager(hub: Hub): HubManager;
private createHubManager(hub: string): HubManager;
private createHubManager(hub: Hub | string | null): HubManager | null;
private createHubManager(hub: Hub | string | null) {
if (typeof hub === 'string') return new HubManager(this.serializeHub(hub), this);
if (hub) return new HubManager(hub);
return null;

private createHubManager(hub: Hub | string): HubManager {
if (typeof hub === 'string') return new HubManager(this.parseHubStringToObject(hub), this);
return new HubManager(hub);
}

async fetchHub(whereInput: string | { id?: string; name?: string }): Promise<HubManager | null> {
Expand Down
57 changes: 25 additions & 32 deletions src/utils/network/blockwordsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ interface BlockResult {
reason?: string;
}

export async function checkBlockedWords(
message: Message<true>,
msgBlockList: BlockWord[],
): Promise<CheckResult> {
if (msgBlockList.length === 0) return { passed: true };

for (const rule of msgBlockList) {
const { shouldBlock, reason } = await checkRule(message, rule);
if (shouldBlock) {
return {
passed: false,
reason,
};
}
}

return { passed: true };
}

async function executeAction(
action: keyof typeof actionHandlers,
message: Message<true>,
Expand All @@ -98,13 +117,13 @@ async function executeAction(

async function processActions(
message: Message<true>,
rule: BlockWord,
triggeredRule: BlockWord,
matches: RegExpMatchArray,
): Promise<BlockResult> {
if (!rule.actions?.length) return { shouldBlock: false };
if (!triggeredRule.actions.length) return { shouldBlock: false };

for (const action of rule.actions) {
const result = await executeAction(action, message, rule, matches);
for (const actionToTake of triggeredRule.actions) {
const result = await executeAction(actionToTake, message, triggeredRule, matches);
if (result.success && result.shouldBlock) {
return {
shouldBlock: true,
Expand All @@ -113,40 +132,14 @@ async function processActions(
}
}

// Default block if actions were configured but none explicitly blocked
return {
shouldBlock: true,
reason: `Your message contains blocked words from the rule: ${rule.name}`,
};
return { shouldBlock: false };
}

async function checkRule(
message: Message<true>,
rule: BlockWord,
): Promise<BlockResult> {
async function checkRule(message: Message<true>, rule: BlockWord): Promise<BlockResult> {
const regex = createRegexFromWords(rule.words);
const matches = message.content.match(regex);

if (!matches) return { shouldBlock: false };

return await processActions(message, rule, matches);
}

export async function checkBlockedWords(
message: Message<true>,
msgBlockList: BlockWord[],
): Promise<CheckResult> {
if (msgBlockList.length === 0) return { passed: true };

for (const rule of msgBlockList) {
const { shouldBlock, reason } = await checkRule(message, rule);
if (shouldBlock) {
return {
passed: false,
reason,
};
}
}

return { passed: true };
}

0 comments on commit 2338575

Please sign in to comment.