Skip to content

Commit

Permalink
Added afk feature !afk [on off status]
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhinpal committed May 23, 2022
1 parent 92df7f8 commit f5d1a64
Show file tree
Hide file tree
Showing 8 changed files with 1,921 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN . /etc/os-release \
fi

RUN apt update && apt install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

RUN npm install

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*
node_modules/
package-lock.json
npm
.eslintcache
session.json
Expand Down
56 changes: 56 additions & 0 deletions commands/afk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { startAfk, afkStatus, stopAfk } = require("../helpers/afkWrapper");

const execute = async (client, msg, args) => {
msg.delete(true);
try {
let commandType = args.shift();

switch (commandType) {
case "on": {
let getstatus = afkStatus();
if (getstatus.on) throw new Error("Already AFK mode is on.");
let message = args.join(" ");
if (!message) message = "Currently I'm away. I will be back soon!";
startAfk(message);
let msgtosend = `AFK mode is now on.\n\nMessage: ${message}`;
await client.sendMessage(msg.to, msgtosend);
break;
}
case "off": {
let getstatus = afkStatus();
if (!getstatus.on) throw new Error("Already AFK mode is off.");
stopAfk();
let msgtosend = "AFK mode is now off.";
await client.sendMessage(msg.to, msgtosend);
break;
}
case "status": {
let getstatus = afkStatus();
let msgtosend = `AFK mode is ${getstatus.on ? "on" : "off"}.${
getstatus.on ? `\n\nMessage: ${getstatus.message}` : ""
}`;

await client.sendMessage(msg.to, msgtosend);
break;
}
default: {
throw new Error(
"Invalid argument. Valid arguments are: on, off, status"
);
}
}
} catch (error) {
let messagetosend = `Afk command failed.\n\n${error?.message}`;
await client.sendMessage(msg.to, messagetosend);
}
};

module.exports = {
name: "afk", //name of the module
description: "Turn on or off afk mode", // short description of what this command does
command: "!afk", //command with prefix. Ex command: '!test'
commandType: "admin", // admin|info|plugin
isDependent: false, //whether this command is related/dependent to some other command
help: "*Afk*\n\n1. *!afk on Message* to turn on afk.\n2. *!afk off* to turn off afk.\n3. *!afk status* to check current status of afk.", // a string descring how to use this command Ex = help : 'To use this command type !test arguments'
execute,
};
12 changes: 6 additions & 6 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SESSION_KEY="tuhin"
PMPERMIT_ENABLED = 'true'
MONGODB_URL = ''
DEFAULT_TR_LANG = 'en'
ENABLE_DELETE_ALERT = 'true'
OCR_SPACE_API_KEY = ''
SESSION_KEY=""
PMPERMIT_ENABLED="true"
MONGODB_URL="mongodb://tuhin:tuhin@localhost:27017/"
DEFAULT_TR_LANG="en"
ENABLE_DELETE_ALERT="true"
OCR_SPACE_API_KEY=""
41 changes: 41 additions & 0 deletions helpers/afkWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require("fs");
const path = require("path");

function startAfk(message) {
fs.writeFileSync(
path.join(__dirname, `../cache/afk.json`),
JSON.stringify({
on: true,
message: message || "Currently I'm away. I will be back soon!",
})
);
}

function afkStatus() {
try {
return JSON.parse(
fs.readFileSync(path.join(__dirname, `../cache/afk.json`))
);
} catch (error) {
return {
on: false,
message: null,
};
}
}

function stopAfk() {
fs.writeFileSync(
path.join(__dirname, `../cache/afk.json`),
JSON.stringify({
on: false,
message: null,
})
);
}

module.exports = {
startAfk,
afkStatus,
stopAfk,
};
2 changes: 1 addition & 1 deletion logger/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = async (client, text) => {
module.exports = async function logger(client, text) {
try {
await client.sendMessage(client.info.wid._serialized, text);
return true;
Expand Down
27 changes: 26 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const pmpermit = require("./helpers/pmpermit");
const config = require("./config");
const fs = require("fs");
const logger = require("./logger");
const { afkStatus } = require("./helpers/afkWrapper");

const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
Expand Down Expand Up @@ -38,7 +39,7 @@ client.on("ready", () => {
});

client.on("message", async (msg) => {
if (!msg.author && config.pmpermit_enabled == "true") {
if (!msg.author && config.pmpermit_enabled === "true") {
// Pm check for pmpermit module
var checkIfAllowed = await pmpermit.handler(msg.from.split("@")[0]); // get status
if (!checkIfAllowed.permit) {
Expand All @@ -51,6 +52,30 @@ client.on("message", async (msg) => {
} else if (!checkIfAllowed.block) {
msg.reply(checkIfAllowed.msg);
}
} else {
await checkAndApplyAfkMode();
}
}

if (!msg.author && config.pmpermit_enabled !== "true") {
await checkAndApplyAfkMode();
}

async function checkAndApplyAfkMode() {
if (!msg.author) {
try {
let getstatus = await afkStatus();
if (getstatus.on) {
await msg.reply(`${getstatus.message}\n\n_Powered by WhatsBot_`);
}
} catch (e) {
await logger(
client,
`Incoming afk message error from ${msg.from.split("@")[0]}.\n\n${
e?.message
}`
);
}
}
}
});
Expand Down
Loading

0 comments on commit f5d1a64

Please sign in to comment.