在Discord bot和Node服务器之间进行通信 [英] Communicate between Discord bot and Node server

查看:41
本文介绍了在Discord bot和Node服务器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用补充的Discord机器人开发一个Web应用程序.当前,因为我都使用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.

有没有办法在bot程序和Web服务器之间来回发送信息?我只需要来回发送数据,例如 {"action":"send_message","text":"hello world"等} ,它们将在同一台计算机上运行.

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应用程序,并让不和谐的bot通过它进行通信.乍一看,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆