Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ const data = {
ERROR:
"```Woops, something went wrong. Try again later, or proabaly not with this again...```",
},
sudo: {
DESCRIPTION: "Module to add or remove members from sudo",
EXTENDED_DESCRIPTION: "```Use this module to add or remove members from sudo. For example use``` *.sudo add @<member-to-add>* ```to add a member to sudo.```. Also *.sudo info* to get a list about the current sudo members.",
ACTION_NOT_SPECIFIED: "```Action not found or not specified.```",
ADD_ERROR: "```Number already in sudo.```",
REMOVE_ERROR: "```Number to remove not found.```",
NUMBER_NOT_FOUND: "```There was an error while processing the number.```",
NO_SUDO_USERS: "```There are no sudo users.```",
},
tr: {
DESCRIPTION: "Language Translator",
EXTENDED_DESCRIPTION:
Expand Down
123 changes: 123 additions & 0 deletions modules/sudo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import config from "../config";
// const fs = require("fs");
// const os = require("os");
// const path = require("path");
// const envFilePath = path.resolve("./config.env");
// const readEnvVars = () => fs.readFileSync(envFilePath, "utf-8").split(os.EOL);

import Strings from "../lib/db";
import { MessageType } from "../sidekick/message-type";
import Client from "../sidekick/client";
import { proto } from "@adiwajshing/baileys";
import BotsApp from "../sidekick/sidekick";
import inputSanitization from "../sidekick/input-sanitization";
const sudo = Strings.sudo;

module.exports = {
name: "sudo",
description: sudo.DESCRIPTION,
extendedDescription: sudo.EXTENDED_DESCRIPTION,
async handle(client: Client, chat: proto.IWebMessageInfo, BotsApp: BotsApp, args: string[]): Promise<void> {
try {
// const setEnvValue = async (key, value) => {
// const envVars = readEnvVars();
// const targetLine = envVars.find(
// (line) => line.split("=")[0] === key
// );
// if (targetLine !== undefined) {
// // update existing line
// const targetLineIndex = envVars.indexOf(targetLine);
// // replace the key/value with the new value
// envVars.splice(targetLineIndex, 1, `${key}="${value}"`);
// } else {
// // create new key value
// envVars.push(`${key}="${value}"`);
// }
// // write everything back to the file system
// await fs.writeFileSync(envFilePath, envVars.join(os.EOL));
// client.sendMessage(BotsApp.chatId, sudo.SUCCESS, MessageType.text);
// };
var phone_number: string;
var SUDOString: string = config.SUDO;
let isReplayAction = false;

if (args.length === 1) { // If the user only specifies the action, it will take the number from the reply
if (args[0] === "list" || args[0] === "info") {

let userCount: number = SUDOString.split(",").filter((item) => item !== "").length; //filter empty strings
client.sendMessage(
BotsApp.chatId,
SUDOString === "" ?
sudo.NO_SUDO_USERS :
"```Sudo Users:\n" + SUDOString.split(",").join("\n") + "\n\nTotal: " + userCount + "```",
MessageType.text
);
return
}

//if replying to a message
if (BotsApp.isTextReply) {
const JID = chat.message.extendedTextMessage.contextInfo.participant;
phone_number = JID.substring(0, JID.indexOf("@"));
isReplayAction = true;
} else {
client.sendMessage(
BotsApp.chatId,
sudo.ACTION_NOT_SPECIFIED,
MessageType.text
);
return
}
}

if (args.length === 2 || isReplayAction) { // If the user specifies the action and the number, it will take the number from the args
phone_number = isReplayAction ? phone_number : args[1];
let SUDOStringArray = SUDOString.split(",");

if (args[0] === "add") {
// If the number is already in the SUDO list, it will send an error message
if (SUDOStringArray.includes(phone_number)) {
client.sendMessage(
BotsApp.chatId,
sudo.ADD_ERROR,
MessageType.text
);
return
}
//add to array
SUDOStringArray.push(phone_number);
SUDOString = SUDOStringArray.join(",");
// setEnvValue("SUDO", SUDOString);
config.SUDO = SUDOString;
return;

} else if (args[0] === "remove") {
if (!SUDOStringArray.includes(phone_number)) {
client.sendMessage(
BotsApp.chatId,
sudo.REMOVE_ERROR,
MessageType.text
);
return
}
//remove from array
SUDOStringArray = SUDOStringArray.filter((item) => item !== phone_number);
SUDOString = SUDOStringArray.join(",");
// setEnvValue("SUDO", SUDOString);
config.SUDO = SUDOString;
return;

}
}
// If the user does not specify the action, it will send an error message
client.sendMessage(
BotsApp.chatId,
sudo.ACTION_NOT_SPECIFIED,
MessageType.text
);
return
} catch (error) {
await inputSanitization.handleError(error, client, BotsApp);
}
},
};
5 changes: 3 additions & 2 deletions sidekick/input-sanitization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export const adminCommands = [
"setdp",
"tagall",
"abl",
"rbl"
"rbl",
"sudo"
];

export const sudoCommands = ["block", "unblock"];
export const sudoCommands = ["block", "unblock", "sudo"];