[go: up one dir, main page]

Skip to content

Commit

Permalink
afk pre final done
Browse files Browse the repository at this point in the history
  • Loading branch information
AnshumanMahato committed Oct 29, 2022
1 parent 16d3af3 commit 52a7dd5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
49 changes: 25 additions & 24 deletions commands/afk.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
const logger = require("../logger");
const { setAfk, setOnline, getAFKData } = require("../helpers/afkhandler");

const formatTime = (time) =>
new Intl.DateTimeFormat("en-IN", {
dateStyle: "medium",
timeStyle: "short",
timeZone: "Asia/Kolkata",
}).format(time);

const afkOn = async (client, reason) => {
let data = await setAfk(reason);
if (data?.afk) {
const time = new Intl.DateTimeFormat("en-IN", {
dateStyle: "medium",
timeStyle: "short",
timeZone: "Asia/Kolkata",
}).format(data.time);
await logger(
client,
`You've already marked yourself offline at ${time}. If you want to set yourself back online, use !online`
`You've already marked yourself offline at ${formatTime(
data.time
)}. If you want to set yourself back online, use *!afk off*`
);
} else if (data?.set) {
await logger(
client,
`You've marked yourself offline! To mark yourself back online use !online`
`You've marked yourself offline! To mark yourself back online use *!afk off*`
);
} else {
await logger(client, `Some error occured.`);
Expand All @@ -28,31 +32,28 @@ const afkOff = async (client) => {
let data = await setOnline();
if (data) {
let msg = `You're now back online. `;
if (data.chats.length)
msg += `While you were offline you recieved messages from \`\`\`${data.chats}\`\`\``;
if (data.chats.length) console.log(data.chats);
msg += `While you were offline you recieved messages from \n\n${data.chats.reduce(
(list, chat) => list + `${chat[0]} --> ${formatTime(chat[1])}\n`,
""
)}`;
await logger(client, msg);
} else {
await logger(client, `Your aren't afk.`);
}
};

const afkStatus = async () => {
const afkStatus = async (client) => {
const data = await getAFKData();
if(data){
const time = new Intl.DateTimeFormat("en-IN", {
dateStyle: "medium",
timeStyle: "short",
timeZone: "Asia/Kolkata",
}).format(data.time);
await logger(
client,
`You've marked yourself offline at ${time}.\nReason: ${data.reason}\n\nIf you want to set yourself back online, use !online`
);
}else {
if (data) {
await logger(
client,
`You're online.`
`You've marked yourself offline at ${formatTime(data.time)}.\nReason: ${
data.reason
}\n\nIf you want to set yourself back online, use *!afk aff*`
);
} else {
await logger(client, `You're online.`);
}
};

Expand All @@ -67,7 +68,7 @@ const execute = async (client, msg, args) => {
await afkOff(client);
break;
case "status":
await afkStatus();
await afkStatus(client);
break;
default:
await logger(client, `Invalid option provide. Please refer to help.`);
Expand All @@ -81,6 +82,6 @@ module.exports = {
command: "!afk",
commandType: "admin",
isDependent: false,
help: "Use !afk to mark yourself as offline. Recipients will be replied with an automated message when you're offline. You can also provide further information with the command like this -\n\n!afk [info]\n\nTo mark yourself back online, use !online",
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.",
execute,
};
28 changes: 14 additions & 14 deletions helpers/afkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ async function read() {
}
}

async function getAFKData() {
let data;
try {
data = JSON.parse(fs.readFileSync(path.join(__dirname, `../cache/AFK.json`)));
} catch (error) {
data = await read();
}

return data;
}

async function updateChatList(chat) {
const {conn,coll} = await database('afk');
try {
Expand All @@ -59,6 +48,7 @@ async function updateChatList(chat) {

}

/* Userge inspired AFK message */
function getAfkString(){
const afkStrings =[
"I'm busy right now. Please talk in a bag and when I come back you can just give me the bag!",
Expand Down Expand Up @@ -87,6 +77,16 @@ function getAfkString(){
return afkStrings[Math.floor(Math.random()*afkStrings.length)];
}

async function getAFKData() {
let data;
try {
data = JSON.parse(fs.readFileSync(path.join(__dirname, `../cache/AFK.json`)));
} catch (error) {
data = await read();
}
return data;
}

async function setAfk(reason) {

let data = await getAFKData();
Expand Down Expand Up @@ -130,7 +130,7 @@ async function setOnline() {
try {
await coll.deleteOne({ afk: true });
return {
chats: data.chats.map((ele) => ele[0]),
chats: data.chats,
timediff
};
} catch (error) {
Expand All @@ -147,7 +147,7 @@ async function setOnline() {

}

async function handler(sender) {
async function afkHandler(sender) {
let data = await getAFKData();

if(data) {
Expand Down Expand Up @@ -175,5 +175,5 @@ module.exports = {
setAfk,
setOnline,
getAFKData,
handler
afkHandler
};
28 changes: 12 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require("./config");
const fs = require("fs");
const logger = require("./logger");
const { afkStatus } = require("./helpers/afkWrapper");
const { handler: getAfk } = require("./helpers/afkhandler");
const { afkHandler } = require("./helpers/afkhandler");

const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
Expand Down Expand Up @@ -69,24 +69,20 @@ client.on("message", async (msg) => {

async function checkAndApplyAfkMode() {
const contact = await msg.getContact();
const afkData = await getAfk(contact?.name || contact?.pushname);
if (afkData) {
const afkData = await afkHandler(contact?.name || contact?.pushname);
if (afkData?.notify) {
//if user is afk
const chat = await msg.getChat();
await chat.sendSeen();

if (afkData.notify) {
const { reason, timediff } = afkData;
let lastseen = "";
lastseen += timediff[0] ? `${timediff[0]} days ` : "";
lastseen += timediff[1] ? `${timediff[1]} hrs ` : "";
lastseen += timediff[2] ? `${timediff[2]} min ` : "";
lastseen += `${timediff[3]} sec ago`;
await msg.reply(
`${afkData.msg}\n\n😊😊😊\n\nI am currently offline...\n\n*Reason*: ${reason}\n*Last Seen*:${lastseen}`
);
}

const { reason, timediff } = afkData;
let lastseen = "";
lastseen += timediff[0] ? `${timediff[0]} days ` : "";
lastseen += timediff[1] ? `${timediff[1]} hrs ` : "";
lastseen += timediff[2] ? `${timediff[2]} min ` : "";
lastseen += `${timediff[3]} sec ago`;
await msg.reply(
`${afkData.msg}\n\n😊😊😊\n\nI am currently offline...\n\n*Reason*: ${reason}\n*Last Seen*:${lastseen}`
);
await chat.markUnread();
}
}
Expand Down

0 comments on commit 52a7dd5

Please sign in to comment.