-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
106 lines (98 loc) · 3.15 KB
/
index.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/// <reference path="./types.js" />
const { Logger, TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const { NewMessage } = require("telegram/events");
const input = require("input");
const fs = require("fs");
const simpleGit = require("simple-git");
const { LogLevel } = require("telegram/extensions/Logger");
const Message = require("./lib/Message");
const {CreateClient } = require("./lib/createClient");
const git = simpleGit();
require("dotenv").config();
const { apiId, apiHash, session, setSudo } = require("./config");
const modules = [];
/**
*
* @type {Module}
*/
function Module(moduleConfig, callback) {
modules.push({ ...moduleConfig, callback });
}
const stringSession = new StringSession(session || "");
(async () => {
console.log("Bot is starting...");
const client = new CreateClient(stringSession, apiId, apiHash, {
connectionRetries: 5,
baseLogger: new Logger(LogLevel.ERROR),
});
client.addEventHandler(async (event) => {
let test = new Message(client, event.message);
const message = event.message.message;
const sender = await event.message.getSender();
if (message) {
for (const module of modules) {
if ((module.fromMe && sender.self) || !module.fromMe) {
const regex = new RegExp(`^\\.\\s*${module.pattern}`);
const match = message.match(regex);
if (match) {
module.callback(test, match);
}
}
}
}
for (const module of modules) {
if (module.on && module.on == "message" && ((module.fromMe && sender.self) || !module.fromMe)) {
module.callback(test);
}
}
}, new NewMessage({}));
await client.start({
phoneNumber: async () => await input.text("number ?"),
password: async () => await input.text("password?"),
phoneCode: async () => await input.text("Code ?"),
onError: (err) => console.log(err),
});
if (session == "") {
let a = client.session.save();
let file = await fs.readFileSync(".env", "utf8");
file += `\nSESSION=${a}`;
fs.writeFileSync(".env", file);
}
console.log("Bot is ready.");
const me = await client.getMe();
setSudo(me.id);
require("./bot/index");
await client.sendMessage("me", { message: "Bot has been started.." });
var commits = await git.log(["main" + "..origin/" + "main"]);
var mss = "";
if (commits.total != 0) {
var changelog = "_Pending updates:_\n\n";
for (var i in commits.all) {
changelog += `${parseInt(i) + 1}• **${commits.all[i].message}**\n`;
}
changelog += `\n_Use ".update start" to start the update_`;
await client.sendMessage("me", { message: changelog });
}
})();
Module(
{ pattern: "start", fromMe: true, desc: "Start command", use: "utility" },
async (m) => {
const sender = await m.message.getSender();
await m.client.sendMessage(sender, {
message: `Hi, your ID is ${m.message.senderId}`,
});
}
);
module.exports = {
Module,
modules,
};
const pluginFolder = "./plugins/";
const files = fs.readdirSync(pluginFolder);
files.forEach((file) => {
if (file.endsWith(".js")) {
const filePath = pluginFolder + file;
require(filePath);
}
});