英文:
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("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"
);
This is website code::
<!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>
This is server code:
// 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!";
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论