Hello Steemians, hope you are all wonderful. For the past two days, I have been trying really hard to host a discord bot in Firebase. I thought I would share my experiences and get it registered in the blockchain. This can be beneficial for people who try the same thing in future. I was initially running the bot in Heroku. It was a wonderful experience to use Heroku for hosting. I will definitely recommend it to people who wish to have a professional bot. You can purchase a Hobby account and deploy your bot. I will try to share my experiences with Heroku in another article. Coming back to the topic, there was a problem in Heroku that I was able to use only 550 free hours in a month. That is the reason why I'm looking for a free hosting option where I can keep the bot on forever.
When exploring I came across Firebase hosting service. I was attracted to this because it is Google company. There are lots of documentation and videos available detailing how this service can be used. Not just hosting but Firebase offers a variety of other services as well. The services are both paid and free. Initially, I was surprised to see that it was offering options to host only static HTML pages. Later I came across this wonderful video that explains how a dynamic node app can be deployed to Firebase.
I followed the instructions in the video and understood that it was possible to host node applications in Firebase account. The crash course also provided lots of other useful information as well. The video explained how to deploy a basic node js app in Firebase. After seeing the video I was confident that I will be able to somehow deploy my discord bot in Firebase without any problem. This is how I started.
My trial with the actual bot code
To make it more interesting for a Firebase beginner, I straight away jumped to deploy my complete code into Firebase. If you had seen the above video you will know the significance of public folder and functions folder. Assuming you already knew about it. Well, I should I have mentioned this in the start of the article itself as a disclaimer, I'm not a big expert in coding but I'm a full stack developer with experience in working on multiple frameworks and platforms. Back to the code. I copied all my code to the index.js file and started it off with the below lines initially.
const functions = require('firebase-functions');
const express = require('express');
const app = express();
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.json({ version: packageInfo.version });
});
exports.app = functions.https.onRequest(app);
bot.on("ready", async() => {
console.log(`Bot is ready ${bot.user.username}`);
});
bot.on("message", async message => {
});
The above are some fragments from the full code. I then tried to host it in my local environment to do some testing. I ran the below code and ended up getting an error message. The error messages were two different types. I have mentioned both the error messages below:
firebase serve --only functions,hosting --debug
Error 1:
Worker for app closed due to file changes
Error 2:
error: code=500,
status=INTERNAL,
message=Function worker killed by signal: SIGTERM,
errors=[Function worker killed by signal: SIGTERM]
! functions: Error from emulator. [object Object]
I have been breaking my head continuously trying out various things after these error messages. What surprises me is that, the bot is successfully running in debugger mode. I'm able to use the functionalities of the bot very well when I'm in debugger mode in Visual Studio Code. Later in one of the article, I read that it can be because of a version mismatch as in the console I get an warning saying:
Warning: You're using Node.js v9.9.0 but Google Cloud Functions only supports v6.11.5.
I have not even planned to deploy my code to cloud yet, but before that I have started to receive warning messages. When exploring further I came across an article that mentioned that the problem can only be local environment when trying to deploy it locally but the application can work when deployed to cloud environment. I took that advice seriously and tried to deploy the code to cloud directly using the below command.
firebase deploy --debug
In the above commands i'm using the --debug
to understand what exactly is being done by the deploy command. Unfortunately I again ended up with an error message. But this time the error message was strange and different.
! functions[app]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:48
bot.on("ready", async() => { console.log(`Bot is ready ${bot.user.username}`);
^
SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at getUserFunction (/var/tmp/worker/worker.js:388:24)
Functions deploy had errors. To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
This error is quite strange to me. It almost mentions a SyntaxError in the code. I'm unable to find any clue online regarding this error message. Could it be because it is not yet possible to use discord js functions in Firebase apps? I'm going to continue exploring options to deploy this bot to Firebase somehow finding a way.
I must commend this awesome explorable composition.
excellent excllent progress ... PS: I am not of help with JS
Congratulations! This post has been upvoted from the communal account, @minnowsupport, by bala41288 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.
you are doing perfect!!
Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase
https://
3. Type
re
Get Featured Instantly & Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness
This post has been rewarded with 100% upvote from @indiaunited-bot account.