2 Discord bot instances but I'm only hosting 1 - node.js

As the title states, I'm only hosting 1 instance but my bot is responding twice for every command/output as if it were being hosted on 2 instances. I can only find one and I've tried everything I can think of.
I tried restarting my PC, shutting down all hosting, and restarting my bot token.

Related

Setup secondary redundant failover Node.JS server

Introduction
So I made a discord bot and hosted it on Heroku server. The bot is basic, it just listens to new messages and responds to them.
I am using a free tier of heroku and discovered that there are so called "free dyno hours". When it is end of the month, all the free hours are consumed and my bot will go offline until next month.
I own a Raspberry pi and so I thought it would be a good idea to host it there as well in case Heroku goes offline.
What I want to achieve
Host discord bot on hosting platform(Heroku).
use my Raspberry pi as a failover server.
Ensure that only one instance of the bot is running at a time.
If both servers are online Heroku(chosen as the main server) has priority.
If the application on Heroku somehow crashes or goes offline the bot on my Raspberry gets activated.
And vice versa.
How would that work if I had 3 and more servers?
How would I approach this if I had the bot connected to database?
This is just an example I would appreciate if more general solution would be provided.
NginX(load balancer)
When searching for help, I came across load balancers and NginX.
If I am right, it basically functions like a gateway, all requests go to the server NginX is hosted on and then they get redirected to one of many servers(depending on how loaded they are etc.).
I have few problems with this:
my bot doesn't receive any requests, it just listens to the discord api.
if the server with Nginx fails it all falls apart?
My solution 1
My first approach was to let the bot running on both Heroku and Raspberry at the same time.
It guarantees that the bot runs on at least one server if the other one fails.
But it is not ideal as the bot responds 2 times when both servers are up.
My solution 2
Heroku:
I added a simple Rest api endpoint with express.js to the bot app.
This way I can check if the bot on Heroku is running.
Raspberry pi:
I wrapped the bot app with this Node program that basically functions like a switch.
Link to a git repo: https://github.com/Matyanson/secondary-node-server.git
Every 3 minutes the program calls the endpoint and then by looking at the status code it checks whether the app is down.
Then it either starts the app or shuts down the app on raspberry(using pm2).
This kinda works but I am sure there is a better solution!
There is also a problem with Heroku(you can skip this):
Heroku uses Dynos(containers) to run the apps. There are different configurations of dynos including 'Web' and 'Worker'.
Worker is used for background job and never is never put to sleep.
Web dyno is the only dyno to receive HTTP traffic. It is put to sleep after 30min of receiving no web traffic.
I can switch on/off either of these.
both: there are 2 instances of my bot.
Worker: Can't connect to the endpoint.
Web: have to 'ping' the api at least every 30min or the bot sleeps.
Why am I asking?
Not because I need to solve this exact problem but because I want to learn a good way of doing this to use it in the future projects.
I also think this would be a good way to not 100% rely on external Hosting providers.

Discord.py differentiating between two different servers

I would like to ask about deploying a bot in 2 different servers. Is there any way to differentiate the two bot instances? For example, I would like to run a bot for serverA and another instance of the same bot in serverB (testing server). I would like to have it so that the bot would respond to a command like "!ping" only once in serverB when testing even if serverA and serverB both have running instances of my bot (meaning there are 2 different running scripts of the same code). My dilemma is that if I have two of the same code running in 2 different terminals, when I try to call for !ping command in serverB, it performs the action twice since there are 2 instances.
Running a bot in 2 terminals is not the same as having your bot join 2 servers. You'll only have 1 instance of the bot running but it's basically listening to both servers.
Differentiating between different servers is generally not needed because the bot will be responding in the context of the server it got a command from. However, if you want to keep track of certain data with files you can do so by including the server name in the file name. This way you can store data for each server separately.

How to successfully deploy to bot service through app service local git deploy?

I've developed a simple chatbot based on the sample python bot (24. ms graph auth bot) and was deploying it through the CLI - it worked like a charm. However, the deployment was taking ages (at least 7 minutes per deployment) and I've tried to use other deployment methods because I have to prototype authorization on the Azure side (I'm not allowed to install ngrok...). I've had a few problems with git deployment to Azure App Service but finally it seemed like it started to work, the problem is - once I thought it'll work, instead of errors I'm receiving empty log.
Last line in the botname.scm.azurewebsites.net/api/vfs/LogFiles/date_code_docker.log:
2020-03-03 07:41:41.123 INFO - Container botname_0_letters for site botname initialized successfully and is ready to serve requests.
It usually indicated that everything works correctly. However, there is nothing in the botname.scm.azurewebsites.net/api/vfs/LogFiles/date_code_default_docker.log - as if there was nothing running. Bot service through webchat receives Internal Server Error. When I was debugging it there was data in the default_docker.log with errors about gunicorn app:app, but since I've solved it and container started to be initialized successfully - the default_docker.log doesn't get any new lines, it looks like the app wasn't working.
Any clue what might be a reason of it? Why is the log empty and bot doesn't work? Deployment from .zip file through Azure CLI worked correctly, it has AppID and AppPassword set up (and it worked through the .zip deployment), is there anything I should take care of when deploying from local git which is done automatically when deploying .zip through CLI?

slackbot replies with duplicate message, when its running on HA

I have developed a slack bot (which is part of my an existing node.js app) which works fine on my dev environment.
But as soon as I deployed this on my staging instance of the app, it started responding to me with duplicate message.
My staging server (example.com) consists of 2 machines for HA, and code gets deployed on both the machine. So I dobut, since bot is running on both the machine, its giving me duplicate replies.
Can anyone please let me know how to ask bot to reply from either of the server. Or let the bot reply only once.
I hope my question is clear, let me know if you need any mroe info.

Heroku Single User Node App, do I need Redis

I'm building a simple node app for broadcast messaging using socket.io. It will have 3 users at a time
user 1: Moderator (gets a stream of social media comments from various APIs, picks messages to send to user 2 and user 3)
user 2: Graphics (displays messages pushed from user 1 as graphics in OpenBroadcastSoftware)
user 3: Host (displays messages pushed from user one on an iPad (to field viewer questions)
This is all for realtime use, nothing needs to get saved or logged.
I know all the basics of sending and receiving socket.io messages.
My question is, for portability, I want to host this App on Heroku
If this was a single Free/Hobby dyno app, would I need any sort of backend like Redis? Or would it work as if i was hosting the app on a local server since there is only once instance?
This app is never going to have more than the 3 users described above, so I'm not looking to implement any scaling what-so-ever
Thanks!
No You don't. The websocket can work with just the app, tested it out.

Resources