Telegram bot #150

Merged
Sirherobrine23 merged 14 commits from Telegram_bot into main 2021-08-11 02:47:35 +00:00
18 changed files with 534 additions and 359 deletions
Showing only changes of commit c105f10c74 - Show all commits

17
.vscode/launch.json vendored
View File

@ -1,6 +1,12 @@
{ {
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"type": "node",
"request": "launch",
"name": "Start Telegram bot dev",
"program": "${workspaceFolder}/bin/telegram_bot.js"
},
{ {
"type": "node", "type": "node",
"request": "launch", "request": "launch",
@ -11,17 +17,6 @@
"Docker" "Docker"
], ],
"port": 9229 "port": 9229
},
{
"type": "node",
"request": "launch",
"name": "Telegram BOT",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"dev:telegram"
],
"port": 9229
} }
] ]
} }

View File

@ -17,16 +17,17 @@
"stop": "stop", "stop": "stop",
"op": "op \"{{Player}}\"", "op": "op \"{{Player}}\"",
"deop": "op \"{{Player}}\"", "deop": "op \"{{Player}}\"",
"ban": "tp \"{{Player}}\" ~ ~99999 ~",
"kick": "kick \"{{Player}}\" \"{{Text}}\"", "kick": "kick \"{{Player}}\" \"{{Text}}\"",
"tp": "tp \"{{Player}}\" {{CCO}}" "tp": "tp \"{{Player}}\" {{X}} {{Y}} {{X}}"
}, },
"java": { "java": {
"stop": "stop", "stop": "stop",
"op": "op \"{{Player}}\"", "op": "op {{Player}}",
"deop": "op \"{{Player}}\"", "deop": "op {{Player}}",
"ban": "ban 0 0 0", "ban": "ban {{Player}}",
"kick": "kick \"{{Player}}\" {{Text}}", "kick": "kick {{Player}} {{Text}}",
"tp": "tp {{Player}} {{CCO}}" "tp": "tp {{Player}} {{X}} {{Y}} {{X}}"
}, },
"pocketmine": { "pocketmine": {
"stop": "stop", "stop": "stop",
@ -34,7 +35,7 @@
"deop": "op {{Player}}", "deop": "op {{Player}}",
"ban": "ban {{Player}}", "ban": "ban {{Player}}",
"kick": "kick {{Player}}", "kick": "kick {{Player}}",
"tp": "tp {{Player}} {{CCO}}" "tp": "tp \"{{Player}}\" {{X}} {{Y}} {{X}}"
} }
}, },
"contributors": [ "contributors": [

View File

@ -1,8 +1,10 @@
const fs = require("fs");
const { Telegraf, Markup } = require("telegraf"); const { Telegraf, Markup } = require("telegraf");
const bds = require("../index"); const bds = require("../index");
const { GetPlatform } = require("../lib/BdsSettings"); const { GetPlatform, GetPaths } = require("../lib/BdsSettings");
const { GetKernel, arch, system } = require("../lib/BdsSystemInfo"); const { GetKernel, arch, system } = require("../lib/BdsSystemInfo");
const { Detect } = require("../src/CheckKill"); const { Detect } = require("../src/CheckKill");
const { Servers } = require("../lib/ServerURL");
// Bot Start And Help messages // Bot Start And Help messages
const HelpAndStart = [ const HelpAndStart = [
@ -12,9 +14,12 @@ const HelpAndStart = [
"Options:", "Options:",
" /start or /help: This message!", " /start or /help: This message!",
" /basic", " /basic",
" start, stop", " start, stop, backup",
" /live_log", " /live_log",
" enabler,disabler", " enabler, disabler",
" /live_log",
" /download",
" Version",
" ", " ",
] ]
@ -25,58 +30,190 @@ const bot = new Telegraf(bds.telegram_token);
bot.start((ctx)=>ctx.reply(HelpAndStart.join("\n"))); bot.start((ctx)=>ctx.reply(HelpAndStart.join("\n")));
bot.help((ctx)=>ctx.reply(HelpAndStart.join("\n"))); bot.help((ctx)=>ctx.reply(HelpAndStart.join("\n")));
// Basic server // User
bot.command("basic", ctx => { bot.command("player", ctx => {
const text = ctx.message.text.replace("/basic", "").trim(); const Server = global.ServerExec;
if (/start/.test(text)) { const CtxOption = ctx.message.text.replace("/player", "").trim();
if (Detect()) ctx.reply("Stop Server"); const CtxContext = CtxOption.replace(/^kick|^deop|^ban|^op/, "").trim();
else { if (CtxOption) {
try { const Players = CtxContext.split(/, |,/gi).filter(a => a.trim());
const Server = bds.start(); console.log(Players);
Server.log(function (data){ if (/kick/.test(CtxOption)){
for (let stx of global.LiveLog) stx.reply(data); if (Players.length >= 1) {
}); Players.forEach(Player => {
global.ServerExec = Server; Server.kick(Player);
return ctx.reply("Server Started") ctx.reply(`${Player} was kicked`);
} catch (err) { });
console.log(err) } else ctx.reply("and the Players?")
ctx.reply("We couldn't start the server")
ctx.reply(err.toString());
}
} }
} else if (/stop/.test(text)) { else if (/deop/.test(CtxOption)){
if (Detect()) { if (Players.length >= 1) {
try { Players.forEach(Player => {
bds.stop() Server.deop(Player);
ctx.reply("Stopping your server") ctx.reply(`${Player} was deopped`);
} catch (err) { });
ctx.reply("We had an error for your server"); } else ctx.reply("and the Players?")
ctx.reply(err.toString()); }
} else if (/ban/.test(CtxOption)){
} else ctx.reply("Your server is stopped") if (Players.length >= 1) {
} else return ctx.reply("Invalid option, they are just: start, stop") Players.forEach(Player => {
}); Server.ban(Player);
ctx.reply(`${Player} was banned`);
// Select Platform });
bot.command("platform", ctx => { } else ctx.reply("and the Players?")
const text = ctx.message.text.replace("/platform", "").trim(); }
try { else if (/op/.test(CtxOption)){
bds.BdsSettigs.UpdatePlatform(text); if (Players.length >= 1) {
return ctx.reply(`Platform update to ${text}`) Players.forEach(Player => {
} catch (err) { Server.op(Player);
ctx.reply("We were unable to change the platform") ctx.reply(`${Player} was opped`);
return ctx.reply(err.toString()) });
} else ctx.reply("and the Players?")
}
else if (/list/.test(CtxOption)){
const Player_Json_path = GetPaths("player");
let Players_Json = JSON.parse(fs.readFileSync(Player_Json_path, "utf8"))[GetPlatform()];
const new_players = {};
Players_Json.forEach(Player => {
console.log(Player);
if (new_players[Player.Player]) {
new_players[Player.Player].push([`Action: ${Player.Action}`, `Date: ${Player.Date}`].join("\n"));
} else {
new_players[Player.Player] = [
[`Player: ${Player.Player}`, `Action: ${Player.Action}`, `Date: ${Player.Date}`].join("\n")
]
}
});
console.log(new_players);
Object.getOwnPropertyNames(new_players).forEach(Player_Array => {
let Length = Math.abs(new_players[Player_Array].length - 5);
let Player = new_players[Player_Array].slice(0, Length).join("\n")
ctx.reply(Player);
});
}
else ctx.reply("Invalid option")
} else {
const ReplyOption = Markup.keyboard([
"/player kick",
"/player deop",
"/player ban",
"/player op",
"/player list",
]).oneTime().resize();
ctx.reply("Player Options:", ReplyOption);
} }
}); });
bot.command("platform_beta", async ctx => { // Basic server
const Keyboard = Markup.keyboard([ bot.command("basic", async ctx => {
"/platform bedrock", const text = ctx.message.text.replace("/basic", "").trim();
"/platform java", if (text) {
"/platform pocketmine", // Start Server
"/platform jsprismarine" if (/start/.test(text)) {
]).oneTime().resize(); if (Detect()) ctx.reply("Stop Server");
ctx.reply("Select Platform", Keyboard) else {
try {
const Server = bds.start();
Server.log(function (data){
for (let stx of global.LiveLog) stx.reply(data);
});
global.ServerExec = Server;
return ctx.reply("Server Started")
} catch (err) {
console.log(err)
ctx.reply("We couldn't start the server")
ctx.reply(err.toString());
}
}
}
// Stop Server
else if (/stop/.test(text)) {
if (Detect()) {
try {
bds.stop()
ctx.reply("Stopping your server")
} catch (err) {
ctx.reply("We had an error for your server");
ctx.reply(err.toString());
}
} else ctx.reply("Your server is stopped")
}
// Backup
else if (/backup/.test(text)) {
const Backup = bds.backup();
ctx.replyWithDocument({
source: Backup.Buffer,
filename: Backup.file_name,
})
}
// Invalid option
else return ctx.reply("Invalid option, they are just: start, stop")
} else {
await ctx.deleteMessage();
const Options = Markup.keyboard([
"/basic start",
"/basic stop",
"/basic backup",
]).oneTime().resize();
ctx.reply("Basic Options", Options);
}
});
// Select Platform
bot.command("platform", async ctx => {
const text = ctx.message.text.replace("/platform", "").trim();
if (text) {
try {
bds.BdsSettigs.UpdatePlatform(text);
return ctx.reply(`Platform update to ${text}`)
} catch (err) {
ctx.reply("We were unable to change the platform")
return ctx.reply(err.toString())
}
} else {
await ctx.deleteMessage();
const Keyboard = Markup.keyboard([
"/platform bedrock",
"/platform java",
"/platform pocketmine",
"/platform jsprismarine"
]).oneTime().resize();
ctx.reply("Select Platform", Keyboard)
}
});
// Download Server
bot.command("download", async ctx => {
const version = ctx.message.text.replace(/\/download|[a-zA-Z]/gi, "").trim();
if (version) {
await bds.download(version, true);
ctx.reply(`Sucess install ${GetPlatform()} with version ${version}`);
} else {
await ctx.deleteMessage();
const KeyboardVersion = Markup.keyboard(Object.getOwnPropertyNames(Servers[GetPlatform()]).map(version => {
return {
text: `/download ${version}`
}
})).oneTime().resize();
ctx.reply("Select Version to Install", KeyboardVersion);
}
});
// Command
bot.command("command", async ctx => {
const text = ctx.message.text.replace("/command", "").trim();
if (!(Detect())) return ctx.reply("Your server is stopped");
if (text) {
try {
global.ServerExec.command(text);
} catch (err) {
ctx.reply("We couldn't execute the command");
ctx.reply(`${err}`);
}
} else {
await ctx.deleteMessage();
return ctx.reply("/command <command>");
}
}); });
// Send Info // Send Info
@ -84,49 +221,57 @@ bot.command("info", ctx => {
const config = bds.get_config(); const config = bds.get_config();
const InfoRes = [ const InfoRes = [
`Bds Maneger core version: ${bds.package_json.version}`, `Bds Maneger core version: ${bds.package_json.version}`,
"", `Kernel: ${GetKernel()}`,
"* System Info:", `Arch: ${arch}`,
` Kernel ------ ${GetKernel()}`, `System: ${system}`,
` Arch -------- ${arch}`, `Platform: ${GetPlatform()}`,
` System ----- ${system}`, `World_name: ${config.world}`,
"", `Running: ${bds.detect()}`,
"* Server:", `Port_V4: ${config.portv4}`,
` platform ---- ${GetPlatform()}`, `Port_V6: ${config.portv6}`,
` world_name -- ${config.world}`, `Max_players: ${config.players}`,
` running ----- ${bds.detect()}`, `Whitelist: ${config.whitelist}`,
` port_V4 ----- ${config.portv4}`,
` port_V6 ----- ${config.portv6}`,
` max_players - ${config.players}`,
` whitelist --- ${config.whitelist}`,
] ]
return ctx.reply(InfoRes.join("\n")); ctx.reply(InfoRes.join("\n\n"));
}); });
// Live Log User // Live Log User
global.LiveLog = []; global.LiveLog = [];
bot.command("live_log", ctx => { bot.command("live_log", async ctx => {
const option = ctx.message.text.replace("/live_log", "").trim(); const option = ctx.message.text.replace("/live_log", "").trim();
if (/enable/.test(option)) { if (option) {
global.LiveLog.push(ctx); if (/enable/.test(option)) {
return ctx.reply("Sucess"); global.LiveLog.push(ctx);
} else if (/disable/.test(option)) { return ctx.reply("Sucess");
// ctx.from.id } else if (/disable/.test(option)) {
for (let ctx_Logs in global.LiveLog) { // ctx.from.id
if (global.LiveLog[ctx_Logs].from.id === ctx.from.id) { for (let ctx_Logs in global.LiveLog) {
delete global.LiveLog[ctx_Logs]; if (global.LiveLog[ctx_Logs].from.id === ctx.from.id) {
global.LiveLog = global.LiveLog.filter(a=>a); delete global.LiveLog[ctx_Logs];
return ctx.reply("Ok"); global.LiveLog = global.LiveLog.filter(a=>a);
return ctx.reply("Ok");
}
} }
return ctx.reply("You are not in the list");
} }
return ctx.reply("You are not in the list"); }
} else return ctx.reply("Invalid option"); await ctx.deleteMessage();
const ReplyOption = Markup.keyboard([
"/live_log enable",
"/live_log disable",
]).oneTime().resize();
ctx.reply("Enable/Disabled?", ReplyOption);
}); });
// text // text
bot.on("message", ctx => global.ServerExec.command(`say ${ctx.message.text}`)); bot.on("text", ctx => {
console.log(ctx.message.text);
if (!(/\/.*/gi.test(ctx.message.text))) global.ServerExec.command(`say ${ctx.message.text}`)
});
// catch // catch
bot.catch(console.log); bot.catch(console.log);
// End And Lauch // End And Lauch
bot.launch(); bot.launch();
console.log("Telegram was started");

View File

@ -1,6 +1,5 @@
const fetchSync = require("@the-bds-maneger/fetchsync"); const fetchSync = require("@the-bds-maneger/fetchsync");
module.exports = { module.exports = {
Servers: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/external_files/main/Server.json").json(), Servers: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/external_files/main/Server.json").json(),
PHPBin: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/Php_Static_Binary/main/binarys.json").json(), PHPBin: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/Php_Static_Binary/main/binarys.json").json()
GoogleDriver: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/external_files/main/Credentials/Google.json").json()
} }

2
package-lock.json generated
View File

@ -8398,7 +8398,7 @@
"docker-run_build": { "docker-run_build": {
"version": "git+ssh://git@github.com/Sirherobrine23/Docker-Run_Build.git#dca7037ac2175352485e61e7e565ec1d81caa062", "version": "git+ssh://git@github.com/Sirherobrine23/Docker-Run_Build.git#dca7037ac2175352485e61e7e565ec1d81caa062",
"dev": true, "dev": true,
"from": "docker-run_build@https://github.com/Sirherobrine23/Docker-Run_Build.git", "from": "docker-run_build@github:Sirherobrine23/Docker-Run_Build",
"requires": { "requires": {
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"minimist": "^1.2.5" "minimist": "^1.2.5"

View File

@ -4,27 +4,29 @@ function getProcess(){
const MountProcess = []; const MountProcess = [];
var getList = "" var getList = ""
if (process.platform === "win32") { if (process.platform === "win32") {
console.info("Getting the process list in Windows is too slow"); getList = execSync("wmic path win32_process get Processid,Commandline,WorkingSetSize").toString().split(/\n/gi).filter(a => a.trim()).map(Line => {
getList = execSync("tasklist").toString("utf8").split("\r").join("\n").split("\n").filter(d => {return !(d === "" || d.includes("====="))}) try {
delete getList[0]; Line = Line.split(/\r/gi).filter(a => a).join("").trim();
getList = getList.filter(d=>{return (d !== undefined)}) const line_split = Line.split(/\s+/gi);
for (let _line of getList) {
_line = _line.split(/\s+/) // Ignore empty lines
// Get argument: wmic process where "ProcessID=4152" get commandline if (line_split.length <= 2) return false
const pidNumber = (_line.length - 5)
MountProcess.push({ let pid = line_split[Math.abs(line_split.length - 2)].toString();
command: (function(){ let mem = line_split[Math.abs(line_split.length - 1)].toString();
try { let command = Line.slice(0, - Math.abs(pid.length)).trim().slice(0, - Math.abs(mem.length)).trim();
return execSync(`wmic process where "ProcessID=${_line[pidNumber]}" get commandline`).toString("utf8").split("\r").join("\n").split("\n").filter(d=>{return !(d.trim() === "" || d.trim() === "CommandLine")}).join(" ").trim().split("\"").join("").trim() pid = parseInt(pid);
} catch (err) { mem = parseInt(mem);
return null if (command && pid && mem) return {
} command,
})(), pid,
pid: parseInt(_line[pidNumber]), mem,
cpu: _line[(_line.length - 3)], }; else return false
mem: (_line[(_line.length - 2)].split(".").join("")), } catch (err) {
}) console.log(err);
} return false
}
}).filter(a => a).forEach(a => MountProcess.push(a));
} else { } else {
getList = execSync("ps -aux").toString("utf8").split("\n").filter(d=>{return !(/USER\s+/.test(d) || d === "")}) getList = execSync("ps -aux").toString("utf8").split("\n").filter(d=>{return !(/USER\s+/.test(d) || d === "")})
for (let _line of getList) { for (let _line of getList) {
@ -32,7 +34,6 @@ function getProcess(){
MountProcess.push({ MountProcess.push({
command: (function(){var command = _line[10];const argvLenght = (_line.length - 11);for (let index = 0; index < argvLenght; index++) {command += ` ${_line[11 + index]}`;} return command})(), command: (function(){var command = _line[10];const argvLenght = (_line.length - 11);for (let index = 0; index < argvLenght; index++) {command += ` ${_line[11 + index]}`;} return command})(),
pid: parseInt(_line[1]), pid: parseInt(_line[1]),
cpu: _line[2],
mem: _line[3], mem: _line[3],
}) })
} }

View File

@ -167,6 +167,34 @@ function start() {
}); });
ServerExec.stdout.on("data", data); ServerExec.stdout.on("data", data);
ServerExec.stderr.on("data", data); ServerExec.stderr.on("data", data);
},
op: function (player = "Steve") {
let command = BdsInfo.Servers[GetPlatform()].op.replace("{{Player}}", player);
ServerExec.stdin.write(command+"\n");
return command;
},
deop: function (player = "Steve") {
let command = BdsInfo.Servers[GetPlatform()].deop.replace("{{Player}}", player);
ServerExec.stdin.write(command+"\n");
return command;
},
ban: function (player = "Steve") {
let command = BdsInfo.Servers[GetPlatform()].ban.replace("{{Player}}", player);
ServerExec.stdin.write(command+"\n");
return command;
},
kick: function (player = "Steve", text = "you got kicked") {
let command = BdsInfo.Servers[GetPlatform()].kick.replace("{{Player}}", player).replace("{{Text}}", text);
ServerExec.stdin.write(command+"\n");
return command;
},
tp: function (player = "Steve", cord = {x: 0, y: 128, z: 0}) {
let command = BdsInfo.Servers[GetPlatform()].tp.replace("{{Player}}", player);
if (cord.x) command = command.replace("{{X}}", cord.x); else command = command.replace("{{X}}", 0);
if (cord.y) command = command.replace("{{Y}}", cord.y); else command = command.replace("{{Y}}", 128);
if (cord.y) command = command.replace("{{Z}}", cord.y); else command = command.replace("{{Z}}", 0);
ServerExec.stdin.write(command+"\n");
return command;
} }
} }
ServerExec.on("exit", ()=>{delete global.BdsExecs[returnFuntion.uuid]}); ServerExec.on("exit", ()=>{delete global.BdsExecs[returnFuntion.uuid]});