有没有办法让不和谐机器人从其他应用程序执行操作

时间:2021-06-17 06:51:02

标签: node.js discord.js bots

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

>

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

1 个答案:

答案 0 :(得分:1)

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


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 服务器,如果您使用 json 正文项 localhost:5000/motdmotd 发出 post 请求,它将更改 motd。在 discord bot 上运行 !motd 将输出当前的 MOTD