NodeJS 我想在网站上显示Discord机器人的实时输出

4ioopgfo  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(117)

这是机器人代码:

const discord = require("discord");
const commands = require("discord.ext");

const client = commands.Bot(
  (command_prefix = "!"),
  (intents = discord.Intents.default())
);
const requests = require("requests");

// This is a list of the commands that the bot knows
const availableCommands = ["!hello", "!goodbye", "!help"];

client.listen(async (message) => {
  // If the message is from the bot itself, ignore it
  if (message.author === client.user) {
    return;
  }
  if (message.content.startsWith("!hello")) {
    await message.channel.send("Hello!");
  } else if (message.content.startsWith("!goodbye")) {
    await message.channel.send("Goodbye!");
  } else if (message.content.startsWith("!log")) {
    console.log("Goodbye!");
  } else if (message.content.startsWith("!help")) {
    // Build the response message
    let response = "Commands:\n";
    for (const command of commands) {
      response += `${command}\n`;
    }
    response += "\nBot made by Agent_12";
    await message.channel.send(response);
  }

  // Send the message to the server
  requests.post(
    "http://localhost:3000/terminal-output",
    (json = { output: message.content })
  );
});

client.run(
  "bot token"
);

这是网站代码::

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- HTML code for the webpage -->

    <div id="terminal-output"></div>

    <style>
      /* CSS code to style the webpage */

      #terminal-output {
        font-family: monospace;
        background-color: black;
        color: white;
        padding: 20px;
      }
    </style>

    <script>
      // JavaScript code to retrieve the terminal output from the server and display it on the webpage

      async function displayTerminalOutput() {
        const response = await fetch("http://localhost:3000/terminal-output");
        const output = await response.text();
        document.getElementById("terminal-output").innerHTML = output;
      }

      // Update the terminal output every second
      setInterval(displayTerminalOutput, 1000);
    </script>
  </body>
</html>

这是服务器代码:

// Node.js code for the server

const express = require("express");
const app = express();

// This is a list of the commands that the bot knows
const commands = ["!hello", "!goodbye", "!help"];

app.post("/terminal-output", (req, res) => {
  terminalOutput = req.body.output;
  res.sendStatus(200);
});

app.get("/terminal-output", (req, res) => {
  res.send(getTerminalOutput());
});

app.use(express.static("C:\\Users\\shahe\\Desktop\\test"));

app.listen(3000, () => {
  console.log("Server listening on port 3000");
});

function getTerminalOutput() {
  // Return the output that you want to display on the webpage
  return "Hello, World!";
}

我尝试了一切,但我不能弄清楚。有些东西总是出错,输出只是不会显示请帮助我,如果你可以我正在使用express.js来做到这一点。我发送控制台到服务器,然后服务器将发送到网站,从那里它将被显示。我尝试了机器人的Python,但不工作,所以切换到JavaScript。

kse8i1jr

kse8i1jr1#

如果不使用express json for API,我会在www.example.com上使用websocketssockets.io这里有一些很好的视频介绍它,比如fireship https://www.youtube.com/watch?v=1BfCnjr_Vjg结合discord js api真实的地将discord消息发送到站点

相关问题