What I’m trying to do:
I’m researching if anvil can host a discord bot that is always online, waiting for specific user interactions on my discord server, to then perform actions. I read some of the documentation and see that anvil can handle scheduled tasks in intervals down to a minute, but I’d like the bot always running in realtime waiting for specific commands to invoke its functionality. Is a service like that possible on anvil?
If your discord bot can interact with an API through http requests, then you can create endpoints in the Anvil server to serve responses from anvil on demand.
Hmmm this might work. It relies on the discord api to route calls based on user input. I’m not quite sure if it’s a REST-style api on the backend.
Here it explains (I think, I haven’t read it) how to trigger a request to your HTTP endpoint when something happens in Discord.
You can post messages to discord using Web hooks from anvil. I have a few of these myself.
However, you can’t use discord.py to run a full bot that responds to commands at anvil. That’s because an async bot of that sort needs its own event loop.
Actually, I was able to do that with Anvil.
After heroku shut down their free tier plan, I was looking for a reliable place to host my bot. Since I was already paying for Anvil, I thought to myself - why not use Anvil for that?
The basic logic is to create a scheduled task that runs every minute (but since that background task will be up forever typically, the next scheduled will not run).
After a week or two, when the background task vanishes, you’ll have the bot back online in less than a minute. My bot is able to handle almost 4000 servers that way without much issues.
It’s not the best way to host the bot but it does work. Although, you’ll need atleast a Personal Plan (so don’t go for this unless you are already paying Anvil).
I’ve made exactly this, but its late. I’ll write up an example in the morning
Is there a way to keep the background task running longer than two weeks?
There is no exact duration of 2 weeks. It just kind of disappears after a while due to any infrastructure issues.
But if you use a scheduled task which runs every minute, you’ll get an almost “always-on” service
How do i do the schedule task every minute?
Run a new backrgound process every minute or?
My bot shouldnt go offline or reatart at all since it looses some settings if it does.
You have to design your bot to be able to recover settings on restart. There is no “always on” service on the Internet. Every machine will have restarts for one reason or another. Every process will crash for one reason or another. The key is to design for the inevitable restart and load in the settings you need from a persistent store.