File size: 3,150 Bytes
2821330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module.exports.config = {
  name: "chat",
  version: "1.0.0",
  hasPermssion: 1,
  credits: "Jonell Magallanes",
  description: "",
    usePrefix: true,
  commandCategory: "Risk",
  usages: "[on/off]",
  cooldowns: 10
};
async function getUserName(api, senderID) {
try {
  const userInfo = await api.getUserInfo(senderID);
  return userInfo[senderID]?.name || "User";
} catch (error) {
  console.log(error);
  return "User";
}
}
var chat = {};

module.exports.handleEvent = async function({api, event}) {
  if (!Object.keys(chat).includes(String(event.threadID))) return;

  const botID = api.getCurrentUserID();
  if (event.senderID === botID) return;

  const threadInfo = await api.getThreadInfo(event.threadID);

  // Check if the event sender is an admin
  const isAdmin = threadInfo.adminIDs.some(adminInfo => adminInfo.id === event.senderID);

  // Check if the sender is bot admin
  const isBotAdmin = threadInfo.adminIDs.some(adminInfo => adminInfo.id === botID);

  // The bot will not remove users if chat is off and the sender is an admin,
  // or the bot itself has been removed from admins
  if (chat[String(event.threadID)] && !isAdmin && isBotAdmin) {
     api.removeUserFromGroup(event.senderID, event.threadID); api.sendMessage(`${await getUserName(api, event.senderID)} has been removed from the group due the chat off has activated declared by Administrator group.`, event.threadID, event.messageID);
  }
};

module.exports.onLoad = function() {
  const { readFileSync, existsSync, writeFileSync } = require("fs");
  const path = __dirname + "/cache/chat.json";
  if (existsSync(path)) {
      chat = JSON.parse(readFileSync(path));
  } else {
      writeFileSync(path, JSON.stringify({}), 'utf-8');
  }
};

module.exports.run = async function({ api, event, args }) {
  const { writeFileSync } = require("fs");
  const path = __dirname + "/cache/chat.json";

  if (!(String(event.threadID) in chat)) chat[String(event.threadID)] = false;

  const threadInfo = await api.getThreadInfo(event.threadID);
  // Check if the sender is an admin
  const isAdmin = threadInfo.adminIDs.some(adminInfo => adminInfo.id === event.senderID);

  // Only allow admin to change chat settings
  if (isAdmin) {
      if (args[0] === "off") {
          chat[String(event.threadID)] = true;
          writeFileSync(path, JSON.stringify(chat), 'utf-8');
          return api.sendMessage(`🛡️ | Chat off has been Activated. The bot will now remove non-admin members from the group when they chat.`, event.threadID);
      } else if (args[0] === "on") {
          chat[String(event.threadID)] = false;
          writeFileSync(path, JSON.stringify(chat), 'utf-8');
          return api.sendMessage(`✅  | Chat off has been Deactivated. The bot will no longer remove members when they chat.`, event.threadID);
      } else {
          return api.sendMessage('Use the command "chat on" to enable or "chat off" to disable chat.', event.threadID);
      }
  } else {
      return api.sendMessage("Admin privilege is required to change chat settings.", event.threadID);
  }
};