How to host your Discord bot 24/7 on an Android Device

Introduction

As most of us know that Glitch permanently banned all pinging services and everything related to them. Most of us also can’t afford buying Glitch subscription or using other services. However there is an great solution for that. So let’s get started.

What You Will Need

  • Android device (Needed to handle Termux).
  • Internet access (Needed to host our bot).
  • Termux (Needed to enable us to host our bot).
  • Code editor like QuickEdit and anWriter free. (Optional, but very useful for coding your bot).

Step By Step Instructions

  1. Open your File Manager.
  2. Create a folder named DiscordBOT.
  3. Create a file named index.js, and place it in the DiscordBOT folder.
  4. Write the following code to the index.js.
console.log("index.js has been executed");

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

client.on("ready", () => {
  console.log("bot has logged into the account");
});

client.on("message", (message) => {
  if (message.author.bot) return;
});

client.login("YOUR-DISCORD-TOKEN");
  1. Save and close the index.js file.
  2. Open Google Play.
  3. Search for Termux.
  4. Find and install Termux.
  5. Once Termux has been installed on your device, open it.
  6. Enter the following command apt upgrade then execute it.
  7. Enter the following command y then execute it.
  8. Enter the following command apt install coreutils then execute it.
  9. Enter the following command apt install nodejs then execute it.
  10. Enter the following command y then execute it.
  11. Enter the following command termux-setup-storage then execute it and allow the storage permission.
  12. Enter the following command npm install -g nodemon then execute it.
  13. Enter the following command cd /storage/emulated/0/DiscordBOT then execute it.
  14. Enter the following command node index.js then execute it.
  15. If you did everything correctly, message index.js has been executed should appear.
  16. If you provided the correct token, message bot has logged into the account should appear.

FAQ

How do I start the bot?
You can start the bot by doing the two commands, first command will be cd /storage/emulated/0/DiscordBOT, then second command will be node index.js, that’s it.

How do I stop the bot?
You can stop the bot, by tapping and holding your finger on the screen to get the more popup, which then you will click it and then press kill process, or you can just press ctrl-c shortcut to stop it.

How do I update the bot?
You can update your bot, by creating a shutdown command for your bot, once you will have it, run the bot command and go to Termux and then enter the following command node index.js.

How long my bot will run for?
The bot will run 24/7 unless your device runs out of the battery or once you lose the internet connection.

How much RAM, MEMORY, and CPU my bot will have?
This all depends on your device and internet. The better device is, the better your bot hosting is.

:warning: WARNING :warning:

Do NOT share your bot token, as it will allow anyone to access your bot.

If you have any questions, feel free to ask one :slightly_smiling_face:

13 Likes

I don’t have android, so i can’t do it =(

1 Like

You can always use your computer or a raspberry pi

4 Likes

installs android on raspberry pi to do the tutorial
I’ve done it and it’s not that bad

4 Likes

Yeah, I am hosting my bot 24/7 on it, I didn’t have to pay a single penny for it :joy:

2 Likes

Besides the internet, phone bill.

2 Likes

Most isps provide unlimited bandwidth.

2 Likes

You still have to pay for the phone. Ain’t nothing in the world is free.

1 Like

image
You should place the message event outside of the ready scope, nesting events is a horrible idea and can cause unexpected behaviour, for example ready event being emitted twice for some reason will make the bot respond twice to any commands.

1 Like

Thanks! I fixed it…

2 Likes

Yeah but most of us, has a phone (with unlimited data), so there is no additional costs for hosting the bot.

This is so helpful! I’m trying it now!

2 Likes

Glitch should get some self-hosted servers for people who make bandwith intensive servers with a good ISP

What do you mean?

like they also have physical access to the servers and get to customize like everything. Basically they buy the hardware themselves

2 Likes

I started to self-host on my phone a few days ago. But theres a lot of latency delays (when the phone is sleeping), and a lot of reconnections.

I turned off battery optimization to keep termux running in the background even the app is not in the app switcher.

1 Like

This method likely isn’t practical, it may just be more of a proof of concept.

1 Like

(Reply to @Nicsena). Somehow I don’t have that problem, my phone is sleeping for almost 12 hours each day, and the bot as no visible delay, and it works 24/7, and I am happy cause I don’t have to pay additional money. Probably because something related to your device or your internet is making the trouble. I don’t really know, but for me it works perfectly. Hosting bot especially on one server shouldn’t be a trouble.

1 Like

If you’re rooted you could probably go way beyond and completely prevent the OS from killing it for whatever reason and start the bot instantly on device restart.

Discord bots are mostly idle and spend much time waiting for events(which you can also ask discord to filter out), it’s mostly just leaving a websocket gateway connection open

Keeping it open in a browser is just additional workload. The more resources you add, you use up more ram or cpu power.

1 Like

if yall don’t care about sharding and music bots, you could try discord.js in the browser :smiley: https://github.com/discordjs/discord.js/blob/master/docs/topics/web.md

4 Likes

Idea💡comes to my life…

1 Like

ok so it keeps saying “module discord.js not found”

Edit: I fixed it

2 Likes

Umm how do I create a command!

You should probably learn discordjs.

1 Like

@ApartSquare3916 You just leaked your bots token, it can be used maliciously by people. Please re-generate your bots token at: https://discord.com/developers/applications

Hey @ApartSquare3916, you leaked your Discord bot token, I’ve flagged it for moderation, would be nice if you removed those images and regenerated your bot token as quickly as possible.

I have regenerated my token can someone Atlest please help me with those errors?

Could you share your code without the token or with a fake token?

Help me how do I make a command it sends errors!!! :frowning:

Maybe tell us the errors

help…

message is not defined

Define it

4 Likes

How to define that huh?

Hey there it seems like you are fairly new to discordjs and javascript, below i have provided some resources that can help solve your question

https://www.w3schools.com/js/js_variables.asp
https://discordjs.guide/creating-your-bot/#listening-for-messages

If you have any more questions after reading these, make sure to ask!

4 Likes

Can I see your code that uses the variable message?

I am dumb I needed to use msg lamo

You can use Bluestacks.

Was about to say this lol.

Why would you use an emulator to host a discord bot when you can do it natively?

7 Likes

If you were hell-bent on hosting on Android, you could use an emulator. Otherwise, no reason ¯_ツ)_/¯

2 Likes

When i use cd /storage/emulated/0/DiscordBOT
It says its not a directory, and i cant figure out why it says that

You need to make the directory first!

mkdir -p /storage/emulated/0/DiscordBOT
3 Likes

(I know this is 8 days old, but still)

Would this belong in #tutorials?

1 Like

Defiantly it’s not a glitch project. Also it would be cool if there was a way to sync the android version with the glitch version using git

Well, you could turn your phone into a git repo (via command line or an app like this) and then use git hooks to restart your bot.

I have been struggling for about 2 hours and I’m extremely frustrated I have done all the steps properly and I’m extremely confused EVERYTIME I do node index.js this displays I’ve tried to do mkdir -p /storage/emulated/0/DiscordBOT and cd /storage/emulated/0/DiscordBOT then bot NOTHING IS WORKING PLEASE HELP

npm i discord.js

1 Like

You want me to try npm i discord.js ?

1 Like

Omg npm install discord.js worked thanks so much

1 Like

One thing that could be added is how to set it up with PM2 for easier restarts, starts, reloads, etc.

How would I do that? I’m a rookie to javascript and ofc discordjs but what is pm2 and how do I use it?

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Also, I was asking if @Daw588 might add this to his post.

3 Likes

Oh that sounds cool I’ll look it up to see if I can add it to my bot thank you

No, sorry for the confusion, I wasn’t meaning to talk to you that’s my bad and I should have made that more clear.

I was meaning that JavaScript (the user) could use PM2 instead of using only node so processes could be managed more easily.

Edit: I accidentally said “cleaning” instead of “meaning” lol.

1 Like

Ummm is there anyway to install sequelize and sqlite3 on termux let me know Cuse I am making a currency bot please help

Helpful Note: Android uses Linux which means you should be able to run tons of different commands and if you ever wanna take a look at the Android source code (and contribute if you want to) go here: https://source.android.com/

But when I do
cd ~ then npm install sequelize it works when i return to my bot file using cd /storage/emulated/0/DiscordBOT and use nodemon it says

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node bot.js`
internal/modules/cjs/loader.js:1032
  throw err;
  ^

Error: Cannot find module 'sequelize'
Require stack:
- /storage/emulated/0/discordBOT/dbObjects.js
- /storage/emulated/0/discordBOT/server.js
- /storage/emulated/0/discordBOT/bot.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
    at Function.Module._load (internal/modules/cjs/loader.js:898:27)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.<anonymous> (/storage/emulated/0/discordBOT/dbObjects.js👍19)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/storage/emulated/0/discordBOT/dbObjects.js',
    '/storage/emulated/0/discordBOT/server.js',
    '/storage/emulated/0/discordBOT/bot.js'
  ]
}
[nodemon] app crashed - waiting for file changes before starting...```

You have to install it in the same directory, otherwise, it will not work.

So I’m back and since then I haven’t made any progress I’m new to discord.js and JavaScript in a whole since my last ordeal so here’s the question how do I make ANY commands?? So here’s what I’m specifically trying to do I’m trying to make a dm all members command for owner only I’ve googled it to see if I could copy and paste it but I have no clue how I would go about this please explain to me in full details how do make and organize new commands specifically if I want to copy and paste code

I’d recommend you take a look at this.

1 Like

what does this mean I’m gonna cry (jk)

Did you save it, from what I know an asterisk near the name of the file means you didn’t save it.

Im using an app called code editor and when I try to save the index.js file it shows that

Did you give it permissions to write files?

Yes but I can double check

Uploading: Screenshot_20200917-115036.png… yep all permissions

I can’t click the screenshot. Is it still uploading or something?

I think he didn’t wait for it to upload? and it sorta did that?

sorry here you go

1 Like

Can you try a different code editor?

1 Like

Yeah I deleted it and got a better one it works now that ks

2 Likes

I’m sorry for all the issues I’m having and having to bother people about them but I can’t install nodejs for some reason when I run apt install nodejs it just displays unable to locate package nodejs

Since your on linux, you need to install NVM.

Hey sorry for late response. Yes I will try to add more to the tutorial like on how to install packages and other useful things for the hosting.

1 Like

raspberry pi runs chromium instead of duckduckgo, (which is a website) same with many linux devices. Then from there add the easiest to use android os and host a discord bot. And @javaarchive, Raspbian was already in my pi at the very beginning. I can’t believe chromeos added linux tbh too.

I’ll just create a simple discord bot, take a couple o days away before I actually go on discord, bc I know only just a little discord.js.

This topic might come in handy!

???

This is for android, which is linux? And duckduckgo is a website, not a broswer.

3 Likes

Finally got everything to work now on to how to add cmds

You mean npm?

No, to install npm on linux/unix, you need Node Version Manager (NVM)

I’ve never had an issue with this (even on a Raspberry Pi). npm is automatically installed when you install node

2 Likes

Actually you don’t. NVM is a helper to managing versions but isn’t needed at all for npm which is installed alongside node

1 Like

But I’m pretty sure you need nvm to get node.

The thing is… you don’t

It’s recommended not needed

using either a Node version manager or a Node installer. We strongly recommend using a Node version manager to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm […]

2 Likes

NVM is a community made helper to handling versions, but you don’t need it.

1 Like

Oh, interesting.

By installer, it means using:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
and
sudo apt install nodejs
Which is my preferred way of installing node and npm. I have never used nvm and the problems outlined in the npm docs, I have never faced

2 Likes

if raspberry pi runs chromium then I assume you have installed chromium os onto your raspberry pi with something like flint os. In that case just enable linux and look for a general chromeos discord hosting guide

Ummm how to I install sequlize and sqlite3

@ApartSquare3916

npm i sequelize sqlite3 --save

Someone tell me how to install sqlite3 it sends Errors PLEASE

Please don’t be rude. No one wants to help you when you are rude.

2 Likes

Try npm rebuild sqlite3, yeah installing packages with native dependcies is always annoying when it doesn’t work or when it takes very long(cough cough sodium).

:point_up:

1 Like

I am using https://repl.it/ to host my bot cuse i am not able to download sqlite3 or enmap or sequelize

Please be aware that some packages may not install because they require some dependencies. For example if you try to install brain.js it will error with python not found or something like that, that means you will need to install python, you can install it by doing npm install --global node-gyp. However for Windows users you will need to do npm install --global --production windows-build-tools before that other command, also keep in mind that you will need to execute that command through PowerShell with administrator privileges.

2 Likes

Just like on Mac, if you want to start your build process you have to download Xcode Developer Tools thru the command line before npm or brew or whatever will work.
Edit: now you need an Apple ID and log into Apple Developer to download Xcode Dev Tools. xcode-select now throws errors.

can sombody please explain to me what this means and how to fix it??

Uh your bot has something wrong going on in index.js

Can you try to help me to see what the issue is?

The error means that you don’t have read and/or write permissions to the file at /storage/emulated/0/DiscordBOT/index.js.

Need Helps with discord.js at Termux? Mention me :>

Hello. As you need to know, Executing file in Internal Memory is Forbidden in Android. The solution is you need to Move your Bot File to $HOME, By executing mv botfolder $HOME .

Hey I fixed everything turns out I didn’t have temrux allowed storage perms lol

3 Likes

Ill make a tutorial on how to build it with the web, which will be static! Ill do that ASAP, which means when I am home.

2 Likes

I just use the NodeJS website.

2 Likes

Well, Let’s go back to topic.

Yeah. If you’re Grant it, Then, You can do it at your Internal Memory.

You should install Python because sqlite3 need it. Execute:
pkg install python python2

Please keep this on-topic. It gets annoying when people go way off-topic.

alright. Sorry bout that

It says Cannot find module ‘discord.js’

Open your terminal and run npm i discord.js

I am not exactly what plan I have cause I am not the one who buys internet lol. But around WiFi 75MB/s unlimited (no limit). He won’t get banned unless your making some sort of massive attack lol not just a simple discord bot.

1 Like

Open your Termux and Execute npm i discord.js.

Make sure you have package.json file. If you have no one, Create it by executing npm init -y

Or you can take off the y switch and have more options.

2 Likes

Let me be honest with you. You shouldn’t use your device’s data plan. Use WiFi. Your device may frequently connect and disconnect if you are moving

4 Likes

I think on Glitch, there should be a template (like posting to #the-gallery) that says "If your package is not found, try npm i YOUR_PACKAGE_NAME

2 Likes

I would recommend adding the --save flag at the end to save it to your package.json

2 Likes

How I change my bot’s Status?

Doesn’t Work…

Maybe ask this in a separate topic or read the documentation because this is a little off topic.

1 Like

It’s not off-topic, it relates the words “hosting, discord, bot, 24/7, android, nodejs, javascript”

Yes, it is off-topic, as the tutorial is how to host your discord bot on a phone, not how to fix this, or how to fix that. The latter would go in a separate topic in #discord-help.

5 Likes

Also some notice,

If you want make your Termux Run at Background, execute termux-wake-lock.

1 Like

I am using WiFi, I even mentioned it

Hey, I did everything correctly but it still doesn’t allow me to host, if you think you can help, add CMG Sync#0666 on Discord so I can screenshare the steps I had done, etc.

Please make a post in #discord-help.

1 Like

i was gonna say that

That was 4 months ago, how were you gonna say that?

2 Likes

I am reffering to the hosting, my bot works perfectly when hosting from glitch but I need it to be 24/7 due to it being a bot that is quickly growing. I think it has to do with the fact I am using Canvas.JS and the JavaScript module on Termux isn’t able to read it because that is what has been erroring it.

[fixed] I had a problem installing nodejs.

1 Like

[fixed] discord.js module not found.

1 Like

Hi. This won’t work for me. I get “Cannot find module ‘discord.js’” error. What do I do?

1 Like

npm i discord.js

4 Likes

Ok. Thank you!

2 Likes

I can’t able to install quick.db

What errors do you get?

1 Like

@RiversideRocks
No python package installation found like this…

quick.db is only an npm package, you will have to look for an alternative if you want to code in python.

quick.db is only an npm package, Try look at alternative package that similar like quick.db for python

If you get some trouble when installing quick.db on Termux, Make sure you have Python installed, and sql package installed on your termux.

If this still happend, Do npm install quick.db --force and ignore these error.

Ohh thank you :heart:

1 Like

Termux FAQ

1. `Cannot find module <Module>`

Make sure your package is installed correctly. If there’s package.json file and has dependencies property on it, Do npm install or install each package manually.

2. I can't install quick.db

Remember, quick.db is only npm package and not python or pip package. Make sure you’ve been installed sqlite and python package in your Termux. If you don’t know how to do it, Execute pkg install sqlite python -y

3. `E: Unable to locate package <package>`

Try updating your package list by doing pkg update, or change Mirror link.

4. Can i root my device with Termux?

No. You can’t. But you can run fakeroot or running some linux distributions rootfs by using proot.

5. How to hack account with Termux?

403 Forbidden

You aren't allowed to ask this question, anymore.

6. I can't clone any git repository

Did your Termux has git package installed?

1 Like

Oh thanks, I forgot to add that command to the tutorial. I will update it once I will able to.

3 Likes

Hello please help me with these errors i have no clue what to do

Run npm i discord.js
The error is because you never installed the package

Hey guys! If you faced a error like 403 error in bintray error, Please upgrade your Termux to F-droid version at https://termux.com. However, You should uninstall all Termux app with the plugin that installed from Play Store like Termux:API, Termux:Boot, and etc. Fortunately, There’s a step to backup everything in your termux here.

Or, If you don’t waste your time, Do this:

echo "deb https://packages.termux.org/apt/termux-main stable main" > $PREFIX/etc/apt/sources.list
apt update -y && apt upgrade -y

But it’s recommended to upgrade to F-droid version since a lot of updates & bug is patched at F-droid.

Good luck.

3 Likes

How did u fix it? Pls help me


How to fix this?

Does your bot need to recieve members or read member presence data? If it does then you need to go to discord.com/developers and enable the respective intents on your dashboard. While you’re at it make sure to enable message intent if you need it so you won’t be suprised when summer rolls around.

1 Like

Help,I am doing it but it said unable to locate package nodejs!!!
Wwhat should I do???

1 Like

Try apt update to update the local index

1 Like

Possibly pkg update if on Termux.

1 Like

So, I did
cd /storage/emulated/0/MysticBot
(mysticbot is my bot)
and it says, bash: cd: /storage/emulated/0/MysticBot: No such file or directory
Anyone help?

That means that you don’t have the MysticBot folder. You need to make sure the folder MysticBot is available under /storage/emulated/0/MysticBot.
Hope this helps!

I do have the MysticBot folder in “/storage/emulated/0/”
I don’t know why it doesn’t work.

That’s strange! Terminal error says it doesn’t exist.

Try doing this:

ls /storage/emulated/0

Do you get any error?

I hope this helps you guys

I looked it up for like an hour trying to fix it

What you have to do is put in

npm i [email protected]

That’s the version that you need to use, and it finally worked for me after 3 hours

Some tip before using Termux,

Try learn shell scripting basic.

   Argument(s)
       |
       V
~ $ cd myproject
^   ^
|   |
| Command / Program
|
Current Directory / Folder
  • cd to change or go to a directory (or go to folder)
  • ls will list what’s available in current folder
  • mv is used to move a file from old path to new path
  • cp is used to copy a file, But also able to copy folder by adding -r flag
  • rm is used to remove a file, But also able to delete folder by adding -rf flag
  • less to read a file with scrollable Terminal interface

And always remember to read the error and understand it

An error is written in English languange, and you should be able to read the error too.

3 Likes


Can anybody help ?

Edit: ive already fixed this but now i am receiving an “MISSING INTENT” error

this sounds like you installed the wrong version of discord.js, can you find out what version you were using before?

How do i check it ?

check in places where you ran it before. there will be a package-lock.json or similar file that records what versions of dependencies are installed