Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const prefix = "!";
- const botname = "BasicBOT playing: !help";
- var help = "__BasicBOT Commands__\n!help, !basic {BASIC_CODE}, !basic help";
- function sendMsg(msg) {
- setTimeout(() => {
- socket.emit("talk", { text: msg });
- }, 1100);
- }
- setTimeout(() => { socket.emit("command", { list: ["name", "BasicBOT playing: !help"] }) }, 1000);
- setTimeout(() => { socket.emit("command", { list: ["name", "BasicBOT playing: !help"] }) }, 2100);
- setTimeout(() => {
- sendMsg("BasicBOT is online. Type " + prefix + "help to see commands.");
- setInterval(() => { sendMsg("BasicBOT is online. Type " + prefix + "help to see commands."); }, 60000);
- }, 3200);
- socket.on("talk", function (message) {
- if (message.text == prefix + "help") {
- sendMsg(help);
- } else if (message.text.startsWith(prefix + "say")) {
- sendMsg(message.text.substring(prefix.length + 4));
- } else if (message.text == prefix + "joke") {
- setTimeout(() => { socket.emit("command", { list: ["joke"] }); }, 1100);
- } else if (message.text == prefix + "fact") {
- setTimeout(() => { socket.emit("command", { list: ["fact"] }); }, 1100);
- } else if (message.text == prefix + "image") {
- setTimeout(() => { socket.emit("command", { list: ["message.text.substring(prefix.length + 6)"] }); }, 1100);
- } else if (message.text.startsWith(prefix + "basic")) {
- const command = message.text.substring(prefix.length + 6).trim();
- if (command === "help") {
- sendBasicHelp();
- } else {
- const basicCode = command.replace(/"/g, '"');
- try {
- executeBasicCode(basicCode);
- } catch (error) {
- sendMsg("Error executing BASIC code: " + error.message);
- }
- }
- } else if (message.text == prefix + "break") {
- stopExecution();
- }
- });
- function sendBasicHelp() {
- const basicHelpText = `BasicBOT - Basic Help\nTo execute BASIC code, use: !basic {BASIC_CODE}`;
- sendMsg(basicHelpText);
- }
- let shouldExecute = true;
- let currentLine = 0;
- let statements = [];
- let variables = {};
- function executeBasicCode(inputCode) {
- shouldExecute = true;
- currentLine = 0;
- statements = inputCode.split(";").map((s) => s.trim());
- variables = {};
- executeNextLine();
- }
- function stopExecution() {
- shouldExecute = false;
- currentLine = 0;
- statements = [];
- }
- function evalExpression(expr) {
- const cleanExpr = expr.replace(/[a-zA-Z]+/g, (match) => variables[match] || match);
- return eval(cleanExpr);
- }
- function executeNextLine() {
- if (!shouldExecute || currentLine >= statements.length) {
- sendMsg("BASIC program execution completed.");
- return;
- }
- const statement = statements[currentLine];
- currentLine++;
- if (statement.startsWith("PRINT")) {
- const expression = statement.substring(5).trim();
- if (expression.startsWith('"') && expression.endsWith('"')) {
- const output = expression.substring(1, expression.length - 1);
- sendMsg(output);
- setTimeout(executeNextLine, 1000); // Repeat after 1 second
- } else {
- const result = evalExpression(expression);
- sendMsg(result.toString());
- setTimeout(executeNextLine, 1000); // Repeat after 1 second
- }
- } else if (statement.startsWith("LET")) {
- const assignment = statement.substring(3).split("=");
- const variable = assignment[0].trim();
- const expression = assignment[1].trim();
- variables[variable] = evalExpression(expression);
- setTimeout(executeNextLine, 1000); // Repeat after 1 second
- } else if (statement.startsWith("INPUT")) {
- const variable = statement.substring(5).trim();
- const userInput = prompt("Enter value for " + variable);
- if (!isNaN(userInput)) {
- variables[variable] = parseFloat(userInput);
- } else {
- sendMsg("Invalid INPUT statement: " + statement);
- stopExecution();
- return;
- }
- setTimeout(executeNextLine, 1000); // Repeat after 1 second
- } else if (statement.startsWith("GOTO")) {
- const lineNum = statement.substring(4).trim();
- if (!isNaN(lineNum)) {
- const lineNumber = parseInt(lineNum);
- if (lineNumber > 0 && lineNumber <= statements.length) {
- currentLine = lineNumber - 1; // Jump to the specified line (line numbers are 1-based)
- executeNextLine();
- } else {
- sendMsg("Invalid GOTO statement: " + statement);
- stopExecution();
- }
- } else {
- sendMsg("Invalid GOTO statement: " + statement);
- stopExecution();
- }
- } else if (statement === "BREAK") {
- stopExecution();
- } else {
- sendMsg("Invalid statement: " + statement);
- stopExecution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement