Skip to content

Commit

Permalink
feat(blacklist): blacklist media
Browse files Browse the repository at this point in the history
  • Loading branch information
sdip15fa committed Apr 16, 2024
1 parent c9e1ce4 commit b11c7a9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
33 changes: 31 additions & 2 deletions commands/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,43 @@ const execute = async (client: Client, msg: Message, args: string[]) => {
if (!msg.fromMe) {
return;
}
if (!args[1]) {
if (!args[1] && !msg.hasQuotedMsg) {
return await client.sendMessage(
chatId,
`Please provide a word to add to the blacklist.`,
);
}
const quotedMsg = await msg.getQuotedMessage();
if (quotedMsg.hasMedia && quotedMsg.mediaKey) {
const chatDoc = await chatsCollection.findOne({ chatId });
if (!chatDoc) {
await chatsCollection.insertOne({
chatId,
blacklist_media: [quotedMsg.mediaKey],
});
} else {
await chatsCollection.updateOne(
{ chatId },
{ $push: { blacklist_media: quotedMsg.mediaKey } },
);
}
await client.sendMessage(
chatId,
`The media has been added to the blacklist.`,
);
break;
}
args.shift();
const word = args.join(" ").toLowerCase();
let word = args.join(" ").toLowerCase();
if (!word.trim()) {
word = quotedMsg.body.toLowerCase();
}
if (!word.trim()) {
return await client.sendMessage(
chatId,
`Please provide a word to add to the blacklist.`,
);
}
const chatDoc = await chatsCollection.findOne({ chatId });
if (!chatDoc) {
await chatsCollection.insertOne({
Expand Down
18 changes: 14 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,28 @@ export default async function main() {

wtsClient.on("message", async (msg) => {
const chatId = (await msg.getChat()).id._serialized;
const blacklist: string[] = (await db("chats").coll.findOne({ chatId }))
?.blacklist;
if (blacklist?.length) {
const chatDoc = await db("chats").coll.findOne({ chatId });

if (chatDoc?.blacklist?.length) {
const converted = msg.body?.replaceAll(" ", "")?.toLowerCase();
if (
blacklist.some((v) => converted?.includes?.(v?.replaceAll(" ", "")))
chatDoc?.blacklist.some(
(v: string) => converted?.includes?.(v?.replaceAll(" ", "")),
)
) {
try {
await msg.delete(true);
} catch {}
}
}

if (chatDoc?.blacklist_media?.length && msg.hasMedia && msg.mediaKey) {
if (chatDoc?.blacklist_media?.includes(msg.mediaKey)) {
try {
await msg.delete(true);
} catch {}
}
}
});

wtsClient.on("message_create", async (msg) => {
Expand Down

0 comments on commit b11c7a9

Please sign in to comment.