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

huangapple go评论64阅读模式
英文:

I want to show live output of my discord bot on a website

问题

这是机器人代码:

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

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

// 这是机器人知道的命令列表
const availableCommands = ["!hello", "!goodbye", "!help"];

client.listen(async (message) => {
  // 如果消息来自机器人自身,请忽略
  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")) {
    // 构建响应消息
    let response = "Commands:\n";
    for (const command of commands) {
      response += `${command}\n`;
    }
    response += "\nBot made by Agent_12";
    await message.channel.send(response);
  }

  // 将消息发送到服务器
  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代码 -->

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

    <style>
      /* 用于样式化网页的CSS代码 */

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

    <script>
      // 用于从服务器获取终端输出并在网页上显示的JavaScript代码

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

      // 每秒更新一次终端输出
      setInterval(displayTerminalOutput, 1000);
    </script>
  </body>
</html>

这是服务器代码:

// 服务器的Node.js代码

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

// 这是机器人知道的命令列表
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 "Hello, World!";
}

如果您遇到问题,可以详细描述您的问题,以便我能提供更多帮助。

英文:

This is bot code:

const discord = require(&quot;discord&quot;);
const commands = require(&quot;discord.ext&quot;);

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

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

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

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

client.run(
  &quot;bot token&quot;
);

This is website code::

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

    &lt;div id=&quot;terminal-output&quot;&gt;&lt;/div&gt;

    &lt;style&gt;
      /* CSS code to style the webpage */

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

    &lt;script&gt;
      // JavaScript code to retrieve the terminal output from the server and display it on the webpage

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

      // Update the terminal output every second
      setInterval(displayTerminalOutput, 1000);
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

This is server code:

// Node.js code for the server

const express = require(&quot;express&quot;);
const app = express();

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

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

app.get(&quot;/terminal-output&quot;, (req, res) =&gt; {
  res.send(getTerminalOutput());
});

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

app.listen(3000, () =&gt; {
  console.log(&quot;Server listening on port 3000&quot;);
});

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

I tried everything but i cant figure it out. Something keeps going wrong and the output just wont show
Please help me if you can i am using express.js to do this. i am sending the console to the server and then the server is going to send it to the website from where it will be displayed. i tried python for the bot but doesnt work so switched to javascript.

答案1

得分: 1

我会使用 websockets 和 sockets.io 而不是使用 express json 用于 API。有一些关于这方面的好视频,比如来自 Fireship 的视频 https://www.youtube.com/watch?v=1BfCnjr_Vjg,结合 Discord JS API 可以实时将 Discord 消息发送到网站上。

英文:

Instead of using express json for api I would use websockets with sockets.io there are some good videos about it like from fireship https://www.youtube.com/watch?v=1BfCnjr_Vjg in conjunction with the discord js api to send the discord messages to the site in real time

huangapple
  • 本文由 发表于 2023年1月6日 20:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75030702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定