我有一个在节点服务器上运行的 discord.js bot,我希望它在其他应用程序中发生其他操作时执行一些操作(不仅仅是消息,而是所有 api 可能性),而其他应用程序并非必须在同一服务器上。
有没有办法用 http 来调用机器人?
我不完全确定你想要做什么。但是...如果您想设置某种形式的服务器,以便您可以与发布请求进行通信。
const { Client } = require("discord.js");
const Config = require("./config");
const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
// ----
const client = new Client();
var motd = "nothing bro"
const app = express()
//========== EXPRESS HTTP SERVER
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded());
app.get("/", (req, res) => {
res.send("Hello World")
})
app.post("/motd", (req, res) => {
if (!req.body.motd) return res.status(400).send("missing motd")
motd = req.body.motd
res.send("completed")
})
app.listen(5000, () => {
console.log("Listening at http://localhost:5000")
})
//========== DISCORD BOT
client.on("ready", () => {
console.log("Bot Listening")
})
client.on("message", (message) => {
if (message.author.bot || !message.guild) return;
// command handler
if (!message.content.toLowerCase().startsWith(Config.prefix)) return;
const [command, ...args] = message.content.slice(Config.prefix.length).split(/\s+/g);
if (command == "motd") {
message.channel.send(motd)
}
})
client.login(Config.token)
这是一个非常简单的 HTTP 服务器,如果您localhost:5000/motd
使用 json 正文项发出 post 请求,motd
它将更改 motd。在 discord bot 上运行 !motd 将输出当前的 MOTD
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句