node.js - 有没有办法让不和谐的机器人从其他应用程序执行操作
问题描述
我有一个在节点服务器上运行的 discord.js 机器人,我希望它执行一些操作(不仅仅是消息,而是所有 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)
localhost:5000/motd
这是一个非常简单的 HTTP 服务器,如果您使用 json 正文项向它发出 post 请求,motd
它将更改 motd。在 discord bot 上运行 !motd 将输出当前的 MOTD
推荐阅读
- flutter - 在flutterr中解析复杂的json
- java - JavaFx如何将文本写入特定位置
- three.js - 我的纹理没有正确显示 Three.js(已解决)
- r-exams - 使用exams2nops 创建可填写的PDF 表格
- c - 双指针跟踪出现在句子中的所有单词
- reactjs - 当来自 redux 商店的 prop 更改时,React DOM 未更新
- python-3.x - 使用 Python 的 docx 库时如何配置页眉(和页脚)的“顶部页眉”值?
- xamarin - 什么时候需要 InvalidateSurface()?
- wordpress - 从 WordPress 读取和写入 Firebase 实时数据库/身份验证?
- sql - SQL - 包括最近拥有的组