首页 > 解决方案 > 有没有办法让不和谐的机器人从其他应用程序执行操作

问题描述

我有一个在节点服务器上运行的 discord.js 机器人,我希望它执行一些操作(不仅仅是消息,而是所有 api 可能性),当其他应用程序发生在其他应用程序中时,这些操作不是必须在同一服务器上。

有没有办法用 http 来调用机器人?

标签: node.jsdiscord.jsbots

解决方案


我不确定您要做什么。但是...如果您想设置某种形式的服务器,以便您可以与发布请求进行通信。


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


推荐阅读