Skip to content

Commit

Permalink
send logs to owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhamrawat5 committed Apr 29, 2024
1 parent 944d8ea commit 37b5716
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"heroku-prebuild": "npm install --only=dev",
"build": "rimraf dist && tsc",
"start": "npm run build && node dist/index.js",
"heroku-prebuild": "npm install --only=dev",
"dev": "ts-node src/index.ts",
"start-prod": "npm run build && node dist/index.js",
"db": "ts-node ./src/utils/dbTables.ts",
Expand Down
10 changes: 2 additions & 8 deletions src/connectionUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import fs from "fs";
import { cache } from "./utils/cache";
import { deleteAuth } from "./db/authDB";
import { Bot } from "./interfaces/Bot";
import { loggerBot, loggerTg } from "./utils/logger";
import { ownerNumberWithJid } from "./utils/config";
import { loggerBot, loggerTg, sendLogToOwner } from "./utils/logger";
import updateQr from "./index";

let startCount = 1;
Expand All @@ -27,12 +26,7 @@ export const connectionUpdate = async (
console.log("Connected");

// await loggerTg(`[STARTED BOT]: ${startCount}`);

if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: `[BOT STARTED] - ${startCount}`,
});
}
await sendLogToOwner(bot, `[BOT STARTED] - ${startCount}`);

// SET GROUP META DATA
const chats = await bot.groupFetchAllParticipating();
Expand Down
22 changes: 10 additions & 12 deletions src/functions/addMemberCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { prefix, pvxgroups } from "../utils/constants";
import { getBlacklist } from "../db/blacklistDB";
import { Bot } from "../interfaces/Bot";

import { loggerBot } from "../utils/logger";
import { ownerNumberWithJid } from "../utils/config";
import { loggerBot, sendLogToOwner } from "../utils/logger";

const addMemberCheck = async (
bot: Bot,
Expand All @@ -30,11 +29,11 @@ const addMemberCheck = async (
await bot.sendMessage(from, {
text: `*─「 🔥 <{PVX}> BOT 🔥 」─* \n\nNumber is blacklisted !!!!\nReason: ${reason}\nGiven by ${adminname}`,
});
if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: `${numSplit} is removed from ${groupSubject}. Blacklisted!`,
});
}

await sendLogToOwner(
bot,
`${numSplit} is removed from ${groupSubject}. Blacklisted!`
);
return;
}

Expand All @@ -54,11 +53,10 @@ const addMemberCheck = async (
});
await bot.groupParticipantsUpdate(from, [numJid], "remove");

if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: `${numSplit} is removed from ${groupSubject}. Not 91!`,
});
}
await sendLogToOwner(
bot,
`${numSplit} is removed from ${groupSubject}. Not 91!`
);
} else if (from === pvxmemes) {
await bot.sendMessage(
from,
Expand Down
12 changes: 6 additions & 6 deletions src/functions/pvxFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import checkTodayBday from "./checkTodayBday";
import { getOldIndianDateTime } from "./getIndianDateTime";
import postStudyInfo from "./postStudyInfo";
import { postTechNewsHeadline, postTechNewsList } from "./postTechNews";
import { sendLogToOwner } from "../utils/logger";

// 0 0 * * * - Every day at 12:00 AM
// 0 */30 * * * * - Every 30 mins
Expand Down Expand Up @@ -53,19 +54,18 @@ export const postBdayCron = async (bot: Bot): Promise<cron.ScheduledTask> => {
true
);
if (!checkTodayBdayRes && ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: "❌ THERE IS SOME PROBLEM WITH BDAY INFO!",
});
await sendLogToOwner(bot, "❌ THERE IS SOME PROBLEM WITH BDAY INFO!");
}

const daysSubtract = 2;
const oldDate = getOldIndianDateTime(daysSubtract);

const deleteOldNewsRes = await deleteOldNews(oldDate);
if (!deleteOldNewsRes && ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: "❌ THERE IS SOME PROBLEM WITH DELETING OLD NEWS!",
});
await sendLogToOwner(
bot,
"❌ THERE IS SOME PROBLEM WITH DELETING OLD NEWS!"
);
}
},
{
Expand Down
16 changes: 4 additions & 12 deletions src/groupParticipantsUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import addMemberCheck from "./functions/addMemberCheck";
import { Bot } from "./interfaces/Bot";
import { getUsernames } from "./db/membersDB";
import { pvxgroups, stats } from "./utils/constants";
import { loggerBot } from "./utils/logger";
import { loggerBot, sendLogToOwner } from "./utils/logger";
import { cache } from "./utils/cache";
import { ownerNumberWithJid, pvxFunctionsEnabled } from "./utils/config";

Expand All @@ -25,11 +25,7 @@ export const groupParticipantsUpdate = async (
const numSplit = `${numJid.split("@s.whatsapp.net")[0]}`;
if (numJid === botNumberJid && msg.action === "remove") {
// bot is removed
if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: `Bot is removed from group.`,
});
}
await sendLogToOwner(bot, `Bot is removed from group.`);
return;
}

Expand All @@ -41,17 +37,13 @@ export const groupParticipantsUpdate = async (
await addMemberCheck(bot, from, numSplit, numJid, groupSubject);

const text = `${groupSubject} [ADD] ${numSplit}`;
if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, { text });
}
await sendLogToOwner(bot, text);

console.log(text);
stats.memberJoined += 1;
} else if (msg.action === "remove") {
const text = `${groupSubject} [REMOVE] ${numSplit}`;
if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, { text });
}
await sendLogToOwner(bot, text);

console.log(text);
stats.memberLeft += 1;
Expand Down
8 changes: 2 additions & 6 deletions src/groupsUpsert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GroupMetadata } from "@whiskeysockets/baileys";
import { cache } from "./utils/cache";
// import { prefix } from "./utils/constants";
import { loggerBot } from "./utils/logger";
import { loggerBot, sendLogToOwner } from "./utils/logger";
import { Bot } from "./interfaces/Bot";
import { ownerNumberWithJid } from "./utils/config";

Expand All @@ -18,11 +18,7 @@ export const groupsUpsert = async (msgs: GroupsUpsert, bot: Bot) => {
// text: `*─「 🔥 <{PVX}> BOT 🔥 」─* \n\nSEND ${prefix}help FOR BOT COMMANDS`,
// });

if (ownerNumberWithJid) {
await bot.sendMessage(ownerNumberWithJid, {
text: `Bot is added to new group.`,
});
}
await sendLogToOwner(bot, `Bot is added to new group.`);
});
} catch (err) {
await loggerBot(bot, "groups.upsert", err, msgs);
Expand Down
13 changes: 8 additions & 5 deletions src/messagesUpsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Bot } from "./interfaces/Bot";
import getGroupAdmins from "./functions/getGroupAdmins";
import { getGroupsData } from "./db/groupsDB";
import { MsgInfoObj } from "./interfaces/msgInfoObj";
import { loggerBot } from "./utils/logger";
import { loggerBot, loggerTg } from "./utils/logger";
import { addUnknownCmd } from "./db/unknownCmdDB";
import { CommandsObj } from "./interfaces/CommandsObj";
import { MilestonesDefault } from "./functions/addDefaultMilestone";
Expand Down Expand Up @@ -271,12 +271,15 @@ export const messagesUpsert = async (
cache.del(`${from}:resDisabled`);
}

// send every command info to my whatsapp, won't work when i send something for bot
// send every command info to my whatsapp number, won't work when I send something to bot
if (ownerNumberWithJid !== sender && ownerNumberWithJid) {
stats.commandExecuted += 1;
await bot.sendMessage(ownerNumberWithJid, {
text: `${stats.commandExecuted}) [${prefix}${command}] [${groupName}]`,
});
// await bot.sendMessage(ownerNumberWithJid, {
// text: `${stats.commandExecuted}) [${prefix}${command}] [${groupName}]`,
// });
await loggerTg(
`${stats.commandExecuted}) [${prefix}${command}] [${groupName}]`
);
}

switch (command) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ export const loggerTg = async (message: string) => {
console.log(error);
}
};

export const sendLogToOwner = async (bot: Bot, message: string) => {
try {
// if (ownerNumberWithJid) {
// await bot.sendMessage(ownerNumberWithJid, { text: message });
// }
if (botTG) await botTG.sendMessage(kryptonChatId, message);
console.log(message);
} catch (error) {
console.log(error);
}
};

0 comments on commit 37b5716

Please sign in to comment.