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

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

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

问题

这是机器人代码:

  1. const discord = require("discord");
  2. const commands = require("discord.ext");
  3. const client = commands.Bot(
  4. (command_prefix = "!"),
  5. (intents = discord.Intents.default())
  6. );
  7. const requests = require("requests");
  8. // 这是机器人知道的命令列表
  9. const availableCommands = ["!hello", "!goodbye", "!help"];
  10. client.listen(async (message) => {
  11. // 如果消息来自机器人自身,请忽略
  12. if (message.author === client.user) {
  13. return;
  14. }
  15. if (message.content.startsWith("!hello")) {
  16. await message.channel.send("Hello!");
  17. } else if (message.content.startsWith("!goodbye")) {
  18. await message.channel.send("Goodbye!");
  19. } else if (message.content.startsWith("!log")) {
  20. console.log("Goodbye!");
  21. } else if (message.content.startsWith("!help")) {
  22. // 构建响应消息
  23. let response = "Commands:\n";
  24. for (const command of commands) {
  25. response += `${command}\n`;
  26. }
  27. response += "\nBot made by Agent_12";
  28. await message.channel.send(response);
  29. }
  30. // 将消息发送到服务器
  31. requests.post(
  32. "http://localhost:3000/terminal-output",
  33. (json = { output: message.content })
  34. );
  35. });
  36. client.run("bot token");

这是网站代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <!-- 网页的HTML代码 -->
  11. <div id="terminal-output"></div>
  12. <style>
  13. /* 用于样式化网页的CSS代码 */
  14. #terminal-output {
  15. font-family: monospace;
  16. background-color: black;
  17. color: white;
  18. padding: 20px;
  19. }
  20. </style>
  21. <script>
  22. // 用于从服务器获取终端输出并在网页上显示的JavaScript代码
  23. async function displayTerminalOutput() {
  24. const response = await fetch("http://localhost:3000/terminal-output");
  25. const output = await response.text();
  26. document.getElementById("terminal-output").innerHTML = output;
  27. }
  28. // 每秒更新一次终端输出
  29. setInterval(displayTerminalOutput, 1000);
  30. </script>
  31. </body>
  32. </html>

这是服务器代码:

  1. // 服务器的Node.js代码
  2. const express = require("express");
  3. const app = express();
  4. // 这是机器人知道的命令列表
  5. const commands = ["!hello", "!goodbye", "!help"];
  6. app.post("/terminal-output", (req, res) => {
  7. terminalOutput = req.body.output;
  8. res.sendStatus(200);
  9. });
  10. app.get("/terminal-output", (req, res) => {
  11. res.send(getTerminalOutput());
  12. });
  13. app.use(express.static("C:\\Users\\shahe\\Desktop\\test"));
  14. app.listen(3000, () => {
  15. console.log("Server listening on port 3000");
  16. });
  17. function getTerminalOutput() {
  18. // 返回要显示在网页上的输出
  19. return "Hello, World!";
  20. }

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

英文:

This is bot code:

  1. const discord = require(&quot;discord&quot;);
  2. const commands = require(&quot;discord.ext&quot;);
  3. const client = commands.Bot(
  4. (command_prefix = &quot;!&quot;),
  5. (intents = discord.Intents.default())
  6. );
  7. const requests = require(&quot;requests&quot;);
  8. // This is a list of the commands that the bot knows
  9. const availableCommands = [&quot;!hello&quot;, &quot;!goodbye&quot;, &quot;!help&quot;];
  10. client.listen(async (message) =&gt; {
  11. // If the message is from the bot itself, ignore it
  12. if (message.author === client.user) {
  13. return;
  14. }
  15. if (message.content.startsWith(&quot;!hello&quot;)) {
  16. await message.channel.send(&quot;Hello!&quot;);
  17. } else if (message.content.startsWith(&quot;!goodbye&quot;)) {
  18. await message.channel.send(&quot;Goodbye!&quot;);
  19. } else if (message.content.startsWith(&quot;!log&quot;)) {
  20. console.log(&quot;Goodbye!&quot;);
  21. } else if (message.content.startsWith(&quot;!help&quot;)) {
  22. // Build the response message
  23. let response = &quot;Commands:\n&quot;;
  24. for (const command of commands) {
  25. response += `${command}\n`;
  26. }
  27. response += &quot;\nBot made by Agent_12&quot;;
  28. await message.channel.send(response);
  29. }
  30. // Send the message to the server
  31. requests.post(
  32. &quot;http://localhost:3000/terminal-output&quot;,
  33. (json = { output: message.content })
  34. );
  35. });
  36. client.run(
  37. &quot;bot token&quot;
  38. );

This is website code::

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot; /&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  7. &lt;title&gt;Document&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;!-- HTML code for the webpage --&gt;
  11. &lt;div id=&quot;terminal-output&quot;&gt;&lt;/div&gt;
  12. &lt;style&gt;
  13. /* CSS code to style the webpage */
  14. #terminal-output {
  15. font-family: monospace;
  16. background-color: black;
  17. color: white;
  18. padding: 20px;
  19. }
  20. &lt;/style&gt;
  21. &lt;script&gt;
  22. // JavaScript code to retrieve the terminal output from the server and display it on the webpage
  23. async function displayTerminalOutput() {
  24. const response = await fetch(&quot;http://localhost:3000/terminal-output&quot;);
  25. const output = await response.text();
  26. document.getElementById(&quot;terminal-output&quot;).innerHTML = output;
  27. }
  28. // Update the terminal output every second
  29. setInterval(displayTerminalOutput, 1000);
  30. &lt;/script&gt;
  31. &lt;/body&gt;
  32. &lt;/html&gt;

This is server code:

  1. // Node.js code for the server
  2. const express = require(&quot;express&quot;);
  3. const app = express();
  4. // This is a list of the commands that the bot knows
  5. const commands = [&quot;!hello&quot;, &quot;!goodbye&quot;, &quot;!help&quot;];
  6. app.post(&quot;/terminal-output&quot;, (req, res) =&gt; {
  7. terminalOutput = req.body.output;
  8. res.sendStatus(200);
  9. });
  10. app.get(&quot;/terminal-output&quot;, (req, res) =&gt; {
  11. res.send(getTerminalOutput());
  12. });
  13. app.use(express.static(&quot;C:\\Users\\shahe\\Desktop\\test&quot;));
  14. app.listen(3000, () =&gt; {
  15. console.log(&quot;Server listening on port 3000&quot;);
  16. });
  17. function getTerminalOutput() {
  18. // Return the output that you want to display on the webpage
  19. return &quot;Hello, World!&quot;;
  20. }

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:

确定