Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality: .sudo -> Add/remove members from sudo #45

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const env = {
DEBUG: process.env.DEBUG,
DATABASE: process.env.DATABASE_URL === './BotsApp.db' ? new Sequelize({ dialect: "sqlite", storage: process.env.DATABASE_URL, logging: convertToLogLevel(process.env.DEBUG) }) : new Sequelize({ dialect: "postgres", storage: process.env.DATABASE_URL}),
WORK_TYPE: process.env.WORK_TYPE === undefined ? "private" : process.env.WORK_TYPE,
SUDO: process.env.SUDO === undefined ? "+917838204238,+17052308534" : process.env.SUDO,
SUDO: process.env.SUDO === undefined ? "" : process.env.SUDO,
}

module.exports = env
100 changes: 100 additions & 0 deletions modules/sudo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const { MessageType } = require("@adiwajshing/baileys");
const config = require("../config");
const fs = require("fs");
const Strings = require("../lib/db");
const sudo = Strings.sudo;
const os = require("os");
const path = require("path");
const envFilePath = path.resolve("./config.env");

const readEnvVars = () => fs.readFileSync(envFilePath, "utf-8").split(os.EOL);

module.exports = {
name: "sudo",
description: sudo.DESCRIPTION,
extendedDescription: sudo.EXTENDED_DESCRIPTION,
async handle(client, chat, BotsApp, args) {
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 number;
var SUDOString = config.SUDO;
if (args.length === 2) {
number = args[1];
} else if (args.length === 1) {
if (!(args[0] === "add" || args[0] === "remove")) {
return client.sendMessage(
BotsApp.chatId,
sudo.ACTION_NOT_SPECIFIED,
MessageType.text
);
}
if (BotsApp.isReply) {
const JID =
chat.message.extendedTextMessage.contextInfo.participant;

number = JID.substring(0, JID.indexOf("@"));
} else {
return client.sendMessage(
BotsApp.chatId,
sudo.NUMBER_NOT_FOUND,
MessageType.text
);
}
} else {
return client.sendMessage(
BotsApp.chatId,
sudo.ACTION_NOT_SPECIFIED,
MessageType.text
);
}
if (args[0] === "add") {
if (config.SUDO.includes(number)) {
return client.sendMessage(
BotsApp.chatId,
sudo.ADD_ERROR,
MessageType.text
);
}
SUDOString = SUDOString.concat(number);
setEnvValue("SUDO", SUDOString);
config.SUDO = SUDOString;
return;
} else if (args[0] === "remove") {
if (!config.SUDO.includes(number)) {
return client.sendMessage(
BotsApp.chatId,
sudo.REMOVE_ERROR,
MessageType.text
);
}
var re = new RegExp(number, "g");
SUDOString = SUDOString.replace(re, "");
setEnvValue("SUDO", SUDOString);
config.SUDO = SUDOString;
return;
} else {
console.log("Incorrect attribute");
return client.sendMessage(
BotsApp.chatId,
"```Unrecognised action for .sudo command. Try add/remove.```",
MessageType.text
);
}
},
};
14 changes: 7 additions & 7 deletions sidekick/input-sanitization.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ exports.performanceTime = async (startTime) => {
}

exports.isMember = async (chatId, groupMembers) => {
var isMember = false;
for (const index in groupMembers) {
if (chatId == groupMembers[index].id.split("@")[0]) {
isMember = true;
}
var isMember = false;
for (const index in groupMembers) {
if (chatId == groupMembers[index].id.split("@")[0]) {
isMember = true;
}
return isMember;
}
return isMember;
}

exports.adminCommands = [
Expand All @@ -87,4 +87,4 @@ exports.adminCommands = [
"welcome",
];

exports.sudoCommands = ["block", "unblock", "setdp"];
exports.sudoCommands = ["block", "unblock", "setdp", "sudo"];