Advertisement
nonogamer9

BasicBOT Source Code

Jul 29th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.95 KB | Source Code | 0 0
  1. const prefix = "!";
  2. const botname = "BasicBOT playing: !help";
  3. var help = "__BasicBOT Commands__\n!help, !basic {BASIC_CODE}, !basic help";
  4.  
  5. function sendMsg(msg) {
  6.     setTimeout(() => {
  7.         socket.emit("talk", { text: msg });
  8.     }, 1100);
  9. }
  10.  
  11. setTimeout(() => { socket.emit("command", { list: ["name", "BasicBOT playing: !help"] }) }, 1000);
  12. setTimeout(() => { socket.emit("command", { list: ["name", "BasicBOT playing: !help"] }) }, 2100);
  13. setTimeout(() => {
  14.     sendMsg("BasicBOT is online. Type " + prefix + "help to see commands.");
  15.     setInterval(() => { sendMsg("BasicBOT is online. Type " + prefix + "help to see commands."); }, 60000);
  16. }, 3200);
  17.  
  18. socket.on("talk", function (message) {
  19.     if (message.text == prefix + "help") {
  20.         sendMsg(help);
  21.     } else if (message.text.startsWith(prefix + "say")) {
  22.         sendMsg(message.text.substring(prefix.length + 4));
  23.     } else if (message.text == prefix + "joke") {
  24.         setTimeout(() => { socket.emit("command", { list: ["joke"] }); }, 1100);
  25.     } else if (message.text == prefix + "fact") {
  26.         setTimeout(() => { socket.emit("command", { list: ["fact"] }); }, 1100);
  27.     } else if (message.text == prefix + "image") {
  28.         setTimeout(() => { socket.emit("command", { list: ["message.text.substring(prefix.length + 6)"] }); }, 1100);
  29.     } else if (message.text.startsWith(prefix + "basic")) {
  30.         const command = message.text.substring(prefix.length + 6).trim();
  31.         if (command === "help") {
  32.             sendBasicHelp();
  33.         } else {
  34.             const basicCode = command.replace(/"/g, '"');
  35.             try {
  36.                 executeBasicCode(basicCode);
  37.             } catch (error) {
  38.                 sendMsg("Error executing BASIC code: " + error.message);
  39.             }
  40.         }
  41.     } else if (message.text == prefix + "break") {
  42.         stopExecution();
  43.     }
  44. });
  45.  
  46. function sendBasicHelp() {
  47.     const basicHelpText = `BasicBOT - Basic Help\nTo execute BASIC code, use: !basic {BASIC_CODE}`;
  48.     sendMsg(basicHelpText);
  49. }
  50.  
  51. let shouldExecute = true;
  52. let currentLine = 0;
  53. let statements = [];
  54. let variables = {};
  55.  
  56. function executeBasicCode(inputCode) {
  57.     shouldExecute = true;
  58.     currentLine = 0;
  59.     statements = inputCode.split(";").map((s) => s.trim());
  60.     variables = {};
  61.  
  62.     executeNextLine();
  63. }
  64.  
  65. function stopExecution() {
  66.     shouldExecute = false;
  67.     currentLine = 0;
  68.     statements = [];
  69. }
  70.  
  71. function evalExpression(expr) {
  72.     const cleanExpr = expr.replace(/[a-zA-Z]+/g, (match) => variables[match] || match);
  73.     return eval(cleanExpr);
  74. }
  75.  
  76. function executeNextLine() {
  77.     if (!shouldExecute || currentLine >= statements.length) {
  78.         sendMsg("BASIC program execution completed.");
  79.         return;
  80.     }
  81.  
  82.     const statement = statements[currentLine];
  83.     currentLine++;
  84.  
  85.     if (statement.startsWith("PRINT")) {
  86.         const expression = statement.substring(5).trim();
  87.         if (expression.startsWith('"') && expression.endsWith('"')) {
  88.             const output = expression.substring(1, expression.length - 1);
  89.             sendMsg(output);
  90.             setTimeout(executeNextLine, 1000); // Repeat after 1 second
  91.         } else {
  92.             const result = evalExpression(expression);
  93.             sendMsg(result.toString());
  94.             setTimeout(executeNextLine, 1000); // Repeat after 1 second
  95.         }
  96.     } else if (statement.startsWith("LET")) {
  97.         const assignment = statement.substring(3).split("=");
  98.         const variable = assignment[0].trim();
  99.         const expression = assignment[1].trim();
  100.         variables[variable] = evalExpression(expression);
  101.         setTimeout(executeNextLine, 1000); // Repeat after 1 second
  102.     } else if (statement.startsWith("INPUT")) {
  103.         const variable = statement.substring(5).trim();
  104.         const userInput = prompt("Enter value for " + variable);
  105.         if (!isNaN(userInput)) {
  106.             variables[variable] = parseFloat(userInput);
  107.         } else {
  108.             sendMsg("Invalid INPUT statement: " + statement);
  109.             stopExecution();
  110.             return;
  111.         }
  112.         setTimeout(executeNextLine, 1000); // Repeat after 1 second
  113.     } else if (statement.startsWith("GOTO")) {
  114.         const lineNum = statement.substring(4).trim();
  115.         if (!isNaN(lineNum)) {
  116.             const lineNumber = parseInt(lineNum);
  117.             if (lineNumber > 0 && lineNumber <= statements.length) {
  118.                 currentLine = lineNumber - 1; // Jump to the specified line (line numbers are 1-based)
  119.                 executeNextLine();
  120.             } else {
  121.                 sendMsg("Invalid GOTO statement: " + statement);
  122.                 stopExecution();
  123.             }
  124.         } else {
  125.             sendMsg("Invalid GOTO statement: " + statement);
  126.             stopExecution();
  127.         }
  128.     } else if (statement === "BREAK") {
  129.         stopExecution();
  130.     } else {
  131.         sendMsg("Invalid statement: " + statement);
  132.         stopExecution();
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement