Skip to content

Commit d81419b

Browse files
committed
feat: bug fixes and improvements
1 parent 1e5aa54 commit d81419b

4 files changed

Lines changed: 73 additions & 16 deletions

File tree

src/commands/phishing.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Message } from "../../types/message";
2+
import { phishingSet } from "..";
3+
import { normalize } from "../components/utils/url";
4+
import log from "../components/utils/log";
5+
6+
export const info = {
7+
command: "phishing",
8+
description: "A simple phishtank test command.",
9+
usage: "phishing",
10+
example: "phishing",
11+
role: "user",
12+
cooldown: 5000,
13+
};
14+
15+
export default async function (msg: Message) {
16+
if (!/^phishing/i.test(msg.body)) return;
17+
18+
if (!msg.hasQuotedMsg) {
19+
const testMessage = `
20+
\`Phishtank\`
21+
22+
Found ${phishingSet.size} phishing pages.
23+
Hint: You can reply this command to messages.
24+
`;
25+
26+
await msg.reply(testMessage);
27+
return;
28+
}
29+
30+
const qouted = await msg.getQuotedMessage();
31+
const extractUrls = qouted.body.match(/(https?:\/\/[^\s]+)/g) || [];
32+
const urls = extractUrls
33+
.map((url) => normalize(url))
34+
.filter((u): u is string => Boolean(u));
35+
const spamUrls = urls.filter((url) => phishingSet.has(url));
36+
if (spamUrls.length == 0) {
37+
const testMessage = `
38+
\`Phishtank\`
39+
40+
This message doesn't contain any links or so.
41+
`;
42+
43+
await msg.reply(testMessage);
44+
return;
45+
}
46+
47+
const text = `
48+
\`Phishing Alert\`
49+
50+
We've found that this url(s): \`${spamUrls.join(", ")}\`
51+
to be phishing site/page.
52+
Proceed with caution.
53+
`;
54+
await msg.reply(text);
55+
}

src/components/events/message.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ export default async function (msg: Message, type: string) {
4646

4747
const lid = msg.author ? msg.author.split("@")[0] : msg.from.split("@")[0];
4848

49-
// process normalization
50-
msg.body = msg.body
51-
.normalize("NFKC")
52-
.replace(/[\u0300-\u036f\u00b4\u0060\u005e\u007e]/g, "")
53-
.trim();
54-
5549
/*
5650
* Block users from running commands.
5751
*/
5852
const isBlockedUser = await isBlocked(lid);
5953
if (isBlockedUser) return;
6054

55+
const prefix = !msg.body.startsWith(commandPrefix);
56+
57+
/*
58+
* Prefix
59+
*/
60+
if (!commandPrefixLess && prefix) return;
61+
if (msg.fromMe && prefix) return;
62+
6163
/*
6264
*
6365
* Check for scam urls
@@ -84,6 +86,12 @@ export default async function (msg: Message, type: string) {
8486

8587
if (msg.isForwarded) return;
8688

89+
// process normalization
90+
msg.body = msg.body
91+
.normalize("NFKC")
92+
.replace(/[\u0300-\u036f\u00b4\u0060\u005e\u007e]/g, "")
93+
.trim();
94+
8795
/*
8896
*
8997
* Quiz command validation
@@ -95,14 +103,6 @@ export default async function (msg: Message, type: string) {
95103
});
96104
}
97105

98-
const prefix = !msg.body.startsWith(commandPrefix);
99-
100-
/*
101-
* Prefix
102-
*/
103-
if (!commandPrefixLess && prefix) return;
104-
if (msg.fromMe && prefix) return;
105-
106106
/*
107107
*
108108
* Override the default function

src/components/services/user.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ export async function findOrCreateUser(msg: Message): Promise<boolean> {
4343
commandCount: 1,
4444
},
4545
});
46+
return true;
4647
} catch (error) {
4748
log.error("Database", `Failed to find or create user.`, error);
4849
}
49-
return true;
50+
return false;
5051
}
5152

5253
export async function getUserbyLid(lid: string) {

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ const monitor = new MemoryMonitor({
1818
thresholdMB: parseInt(process.env.PROJECT_THRESHOLD_MEMORY || "1024", 10),
1919
});
2020
const phishtank = new PhishTankClient();
21-
const phishingSet: Set<string> = phishtank.getPhishingSet();
21+
let phishingSet: Set<string>;
2222

2323
async function main() {
2424
checkRequirements();
2525
monitor.start();
2626
phishtank.startAutoUpdateLoop();
2727
await phishtank.init();
28+
phishingSet = phishtank.getPhishingSet();
2829
await client.initialize();
2930

3031
mapCommands();

0 commit comments

Comments
 (0)