如何将 Discord.js 机器人部署到 Cloud Functions?

Lar*_*nen 6 node.js firebase google-cloud-functions discord.js

我想将在 Discord.js 上运行的 Discord 机器人部署到 Firebase Cloud Functions,但我无法让该机器人在 Firebase 中运行。如果我使用nodemon,它会完美运行,但如果我使用firebase部署,它不会启动机器人。

这是我当前的代码:

const functions = require('firebase-functions');

require('dotenv').config();
const token = process.env.TOKEN

const Discord = require('discord.js')
const Client = new Discord.Client();

Client.on('ready', () => {
    Client.channels.find(x => x.name === 'main-cannel').send('bot is deployed')
    Client.user.setGame(`The Cult | ${Client.guilds.size} servers`)
    Console.log('test')
});

Client.login(token);

//is is not working but de basic
//export.App = functions.... {Client}
exports.app = functions.https.onRequest((request, response) => {
    response.send("Test");
});
Run Code Online (Sandbox Code Playgroud)

小智 6

这可能不是谷歌云平台服务的最佳组合,因为云功能的设计并没有考虑到这一点。您可以将 Discord 机器人托管在计算引擎机器上。

如果您想使用动态扩展,请查看Discord Microservice Bots,其中 DasWolke 描述了微服务是什么。Hey 还包含了他的 javascript 代码,用于为 Discord 划分不同的服务。

具体来说,您可以在 Google 云平台上执行的操作是创建一个运行网关的虚拟机。这需要 24/7 运行并且应该是轻量级的。您可以使用 f1-micro(免费)来完成此任务,尽管 google 推荐使用 g1-small 来完成该任务。

网关应该过滤您正在查找的事件(因为 Discord 发送了很多事件,而您不需要大多数事件)并将数据发送到云功能云运行(您可以通过pub/sub发送数据)。根据我的经验,云运行的启动时间要短得多,所以我选择了它。

在您的函数内,您会收到数据并对其进行您想要的操作。如果您想在 Discord 中进行某些操作(发送消息、管理频道……),您可以使用SnowTransfer。SnowTransfer 只是在不和谐时调用其余 API。