Discord bot 和 Node 服务器之间的通信 [英] Communicate between Discord bot and Node server
问题描述
我正在开发一个带有互补性 Discord 机器人的网络应用程序.目前,因为我对两者都使用 Node,所以它们在同一个项目中并与 Mongoose 共享相同的数据库连接.我可以通过将它作为一个模块来访问网络应用程序的路由中的机器人,但它相当笨重.
I am developing a web app with a complementary Discord bot. Currently, because I use Node for both, they are in the same project and share the same database connection with Mongoose. I can access the bot in the routes of the web app by requiring it as a module but it's rather clunky.
有没有一种方法可以在机器人程序和网络服务器之间来回发送信息?我只需要来回发送 { "action": "send_message", "text": "hello world", etc.}
之类的数据,它们将在同一台计算机上运行.
Is there a way I can send information back and forth between the bot program and the web server? I would just need to be send data like { "action": "send_message", "text": "hello world", etc.}
back and forth, and they would be running on the same computer.
推荐答案
你可以在里面设置一个REST Api您的 Node 应用程序并让您的不和谐机器人通过它进行通信.乍一看,API 可能有一个路由:
You can set up a REST Api in your Node app and have your discord bot communicate through it. At a glance, the API might have a route:
POST /v1/messages
接受包含 { "text": "hello world" }
等的 HTTP 正文.
which accepts an HTTP body containing { "text": "hello world" }
etc.
Node 有很多 HTTP 服务器模块,express
是最受欢迎的.编码愉快!
There are many HTTP server modules for Node, express
being the most popular. Happy coding!
这篇关于Discord bot 和 Node 服务器之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!