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",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Start Telegram bot dev",
"program": "${workspaceFolder}/bin/telegram_bot.js"
},
{
"type": "node",
"request": "launch",
@ -11,17 +17,6 @@
"Docker"
],
"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",
"op": "op \"{{Player}}\"",
"deop": "op \"{{Player}}\"",
"ban": "tp \"{{Player}}\" ~ ~99999 ~",
"kick": "kick \"{{Player}}\" \"{{Text}}\"",
"tp": "tp \"{{Player}}\" {{CCO}}"
"tp": "tp \"{{Player}}\" {{X}} {{Y}} {{X}}"
},
"java": {
"stop": "stop",
"op": "op \"{{Player}}\"",
"deop": "op \"{{Player}}\"",
"ban": "ban 0 0 0",
"kick": "kick \"{{Player}}\" {{Text}}",
"tp": "tp {{Player}} {{CCO}}"
"op": "op {{Player}}",
"deop": "op {{Player}}",
"ban": "ban {{Player}}",
"kick": "kick {{Player}} {{Text}}",
"tp": "tp {{Player}} {{X}} {{Y}} {{X}}"
},
"pocketmine": {
"stop": "stop",
@ -34,7 +35,7 @@
"deop": "op {{Player}}",
"ban": "ban {{Player}}",
"kick": "kick {{Player}}",
"tp": "tp {{Player}} {{CCO}}"
"tp": "tp \"{{Player}}\" {{X}} {{Y}} {{X}}"
}
},
"contributors": [

View File

@ -1,8 +1,10 @@
const fs = require("fs");
const { Telegraf, Markup } = require("telegraf");
const bds = require("../index");
const { GetPlatform } = require("../lib/BdsSettings");
const { GetPlatform, GetPaths } = require("../lib/BdsSettings");
const { GetKernel, arch, system } = require("../lib/BdsSystemInfo");
const { Detect } = require("../src/CheckKill");
const { Servers } = require("../lib/ServerURL");
// Bot Start And Help messages
const HelpAndStart = [
@ -12,9 +14,12 @@ const HelpAndStart = [
"Options:",
" /start or /help: This message!",
" /basic",
" start, stop",
" start, stop, backup",
" /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.help((ctx)=>ctx.reply(HelpAndStart.join("\n")));
// Basic server
bot.command("basic", ctx => {
const text = ctx.message.text.replace("/basic", "").trim();
if (/start/.test(text)) {
if (Detect()) ctx.reply("Stop Server");
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());
}
// User
bot.command("player", ctx => {
const Server = global.ServerExec;
const CtxOption = ctx.message.text.replace("/player", "").trim();
const CtxContext = CtxOption.replace(/^kick|^deop|^ban|^op/, "").trim();
if (CtxOption) {
const Players = CtxContext.split(/, |,/gi).filter(a => a.trim());
console.log(Players);
if (/kick/.test(CtxOption)){
if (Players.length >= 1) {
Players.forEach(Player => {
Server.kick(Player);
ctx.reply(`${Player} was kicked`);
});
} else ctx.reply("and the Players?")
}
} 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")
} else return ctx.reply("Invalid option, they are just: start, stop")
});
// Select Platform
bot.command("platform", ctx => {
const text = ctx.message.text.replace("/platform", "").trim();
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 if (/deop/.test(CtxOption)){
if (Players.length >= 1) {
Players.forEach(Player => {
Server.deop(Player);
ctx.reply(`${Player} was deopped`);
});
} else ctx.reply("and the Players?")
}
else if (/ban/.test(CtxOption)){
if (Players.length >= 1) {
Players.forEach(Player => {
Server.ban(Player);
ctx.reply(`${Player} was banned`);
});
} else ctx.reply("and the Players?")
}
else if (/op/.test(CtxOption)){
if (Players.length >= 1) {
Players.forEach(Player => {
Server.op(Player);
ctx.reply(`${Player} was opped`);
});
} 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 => {
const Keyboard = Markup.keyboard([
"/platform bedrock",
"/platform java",
"/platform pocketmine",
"/platform jsprismarine"
]).oneTime().resize();
ctx.reply("Select Platform", Keyboard)
// Basic server
bot.command("basic", async ctx => {
const text = ctx.message.text.replace("/basic", "").trim();
if (text) {
// Start Server
if (/start/.test(text)) {
if (Detect()) ctx.reply("Stop Server");
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
@ -84,49 +221,57 @@ bot.command("info", ctx => {
const config = bds.get_config();
const InfoRes = [
`Bds Maneger core version: ${bds.package_json.version}`,
"",
"* System Info:",
` Kernel ------ ${GetKernel()}`,
` Arch -------- ${arch}`,
` System ----- ${system}`,
"",
"* Server:",
` platform ---- ${GetPlatform()}`,
` world_name -- ${config.world}`,
` running ----- ${bds.detect()}`,
` port_V4 ----- ${config.portv4}`,
` port_V6 ----- ${config.portv6}`,
` max_players - ${config.players}`,
` whitelist --- ${config.whitelist}`,
`Kernel: ${GetKernel()}`,
`Arch: ${arch}`,
`System: ${system}`,
`Platform: ${GetPlatform()}`,
`World_name: ${config.world}`,
`Running: ${bds.detect()}`,
`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
global.LiveLog = [];
bot.command("live_log", ctx => {
bot.command("live_log", async ctx => {
const option = ctx.message.text.replace("/live_log", "").trim();
if (/enable/.test(option)) {
global.LiveLog.push(ctx);
return ctx.reply("Sucess");
} else if (/disable/.test(option)) {
// ctx.from.id
for (let ctx_Logs in global.LiveLog) {
if (global.LiveLog[ctx_Logs].from.id === ctx.from.id) {
delete global.LiveLog[ctx_Logs];
global.LiveLog = global.LiveLog.filter(a=>a);
return ctx.reply("Ok");
if (option) {
if (/enable/.test(option)) {
global.LiveLog.push(ctx);
return ctx.reply("Sucess");
} else if (/disable/.test(option)) {
// ctx.from.id
for (let ctx_Logs in global.LiveLog) {
if (global.LiveLog[ctx_Logs].from.id === ctx.from.id) {
delete global.LiveLog[ctx_Logs];
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
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
bot.catch(console.log);
// 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");
module.exports = {
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(),
GoogleDriver: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/external_files/main/Credentials/Google.json").json()
PHPBin: fetchSync("https://raw.githubusercontent.com/The-Bds-Maneger/Php_Static_Binary/main/binarys.json").json()
}

2
package-lock.json generated
View File

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

View File

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

View File

@ -167,6 +167,34 @@ function start() {
});
ServerExec.stdout.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]});