DEV Community

Cover image for Hosting a Discord bot on Repl.it

Hosting a Discord bot on Repl.it

Fizal Sarif on April 02, 2020

As Blog posts age the content becomes a bit stale. The information here might not be accurate anymore. If you're an avid discord user you've de...
Collapse
 
botmaker349 profile image
botmaker349 • Edited

/home/runner/trying-trying-tryimg/index.js:8
const express = require('express');
^

SyntaxError: Identifier 'express' has already been declared
Hint: hit control+c anytime to enter REPL.

help pls

Collapse
 
thediamondcg profile image
Diamond

You have already set up express as a variable, and now can't change it because it's ALREADY a constant.

P.S: This is a sign you cant' understand the code you're writing. If you don't understand the code you are writing, then you NEED to go to the basics and learn JavaScript itself before delving into complicated topics like Node.js and APIs.

Collapse
 
mczgodpiggy profile image
MCZgodpiggy363 • Edited

i'm good with that tho
dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
aryamaan08 profile image
Aryamaan08

Hey,
I made mine using Python and unfortunately it didn't work. I'm guessing it's because I can't see the token because it is in .env and Uptime Monitor is not the owner. I even tried it without the .env, it still didn't work can you help me?

Collapse
 
thediamondcg profile image
Diamond

Python isn't necessarily the language for back-end web apps. Either use Go or Node.js. Go is phenomenal for the job because it's incredibly fast, yet its size is bloated so not good for storage-sensitive applications. Luckily, servers aren't storage-sensitive which is where it's supposed to run :) On the other hand, Node.js is delightfully tiny, only as big as the text file you made to run it. The down side is that it's much slower than languages like Go.

Collapse
 
mataskrivickas profile image
Force Studios - Comms Open

Unfortunately, your using Python, that is the problem. This is meant for discord.js. I would recommend going to here: dev.to/harshitojha/host-a-discord-...

It is a python version of this

Collapse
 
xghp3x profile image
Explox

I used Node.js for this and it works. The guy in the tutorial did the same thing. So I recommend you try to use Node.js and see how it goes

Collapse
 
aplha101 profile image
Aplha101

I used this method mine is working but where can I add more features to it?

Thread Thread
 
anmols149 profile image
AnmolS149

Well, there are two things you can do:

  1. Learn a coding language like JavaScript or Python... or
  2. use Scratch for Discord to make a bot and host it on RepL.it. You don't need any coding knowledge. You will need some basic knowledge of file and package management... Here's scratch for discord: scratch-for-discord.netlify.app

there can be some bugs with it, also you need to create a .env file to protect your bot token from hacking....

Collapse
 
centanomics profile image
Cent

Thanks for this! I've used Repl.it before but never for hosting purposes

Collapse
 
hassan_bh profile image
Hassan-BH

Yes of course.

Collapse
 
mataskrivickas profile image
Force Studios - Comms Open

It's good, i'm pretty sure you're using "Atom" as a coding platform. Visual Code & Atom is same. Almost every coding software is the same and can be used on as long as you have discord.js downloaded by downloading node.js

Collapse
 
seenloveswumpus profile image
Seenloveswumpus

Hi, I have a repl team and it not working when I put the URL in to uptime robot. (not keeping online)

Collapse
 
creationsstudios profile image
Creations Studios

Hi it won't work. The bot isn't online and in repl it said the token is invalid.

Collapse
 
creationsstudios profile image
Creations Studios

Server {
insecureHTTPParser: undefined,
_events: [Object: null prototype] {
request: [Function: app] EventEmitter {
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
setMaxListeners: [Function: setMaxListeners],
getMaxListeners: [Function: getMaxListeners],
emit: [Function],
addListener: [Function: addListener],
on: [Function: addListener],
prependListener: [Function: prependListener],
once: [Function: once],
prependOnceListener: [Function: prependOnceListener],
removeListener: [Function: removeListener],
off: [Function: removeListener],
removeAllListeners: [Function: removeAllListeners],
listeners: [Function: listeners],
rawListeners: [Function: rawListeners],
listenerCount: [Function: listenerCount],
eventNames: [Function: eventNames],
init: [Function: init],
defaultConfiguration: [Function: defaultConfiguration],
lazyrouter: [Function: lazyrouter],
handle: [Function: handle],
use: [Function: use],
route: [Function: route],
engine: [Function: engine],
param: [Function: param],
set: [Function: set],
path: [Function: path],
enabled: [Function: enabled],
disabled: [Function: disabled],
enable: [Function: enable],
disable: [Function: disable],
acl: [Function],
bind: [Function],
checkout: [Function],
connect: [Function],
copy: [Function],
delete: [Function],
get: [Function],
head: [Function],
link: [Function],
lock: [Function],
'm-search': [Function],
merge: [Function],
mkactivity: [Function],
mkcalendar: [Function],
mkcol: [Function],
move: [Function],
notify: [Function],
options: [Function],
patch: [Function],
post: [Function],
pri: [Function],
propfind: [Function],
proppatch: [Function],
purge: [Function],
put: [Function],
rebind: [Function],
report: [Function],
search: [Function],
source: [Function],
subscribe: [Function],
trace: [Function],
unbind: [Function],
unlink: [Function],
unlock: [Function],
unsubscribe: [Function],
all: [Function: all],
del: [Function],
render: [Function: render],
listen: [Function: listen],
request: [IncomingMessage],
response: [ServerResponse],
cache: {},
engines: {},
settings: [Object],
locals: [Object: null prototype],
mountpath: '/',
_router: [Function]
},
connection: [Function: connectionListener],
listening: [Function: bound onceWrapper] { listener: [Function] }
},
_eventsCount: 3,
_maxListeners: undefined,
_connections: 0,
_handle: TCP {
reading: false,
onconnection: [Function: onconnection],
[Symbol(owner_symbol)]: [Circular]
},
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
maxHeadersCount: null,
Promise {
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
}
Hint: hit control+c anytime to enter REPL.
Example app listening at localhost:3000
(node:316) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47
(node:316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
Promise {
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
}
Hint: hit control+c anytime to enter REPL.
Example app listening at localhost:3000
(node:359) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47
(node:359) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
TypeError: Cannot read property 'env' of undefined
at /home/runner/ReminderBot/index.js:24:28
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
Promise {
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
}
Hint: hit control+c anytime to enter REPL.
Example app listening at localhost:3000
(node:456) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47
(node:456) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
Promise {
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
}
Hint: hit control+c anytime to enter REPL.
Example app listening at localhost:3000
(node:496) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/ReminderBot/node_modules/discord.js/src/client/Client.js:206:52)
at /home/runner/ReminderBot/index.js:24:8
at Script.runInContext (vm.js:130:18)
at Object. (/run_dir/interp.js:209:20)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47
(node:496) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see nodejs.org/api/cli.html#cli_unhand...). (rejection id: 1)
(node:496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here is the console

Collapse
 
opielix profile image
Opilite Elix

same

Collapse
 
opielix profile image
Opilite Elix

and the repl brakes and can't wake up

Collapse
 
aryasarukkai profile image
Arya Sarukkai

With this, people can access the code and token of this bot though, right?

Collapse
 
bestplayerxu profile image
bestPlayerXu

no. the .env file is the only private-hidden file.

Collapse
 
anmols149 profile image
AnmolS149

no the .env file is private and can be only viewed by you...plus what are the odds of someone searching your rep and then scrolling through it to find your token...also you can always regenerate it...

Collapse
 
paaaap profile image
pAAAAp

Would this work if you close the Uptime Robots tab?

Collapse
 
gio2chi profile image
Gio2chi

It still works?

Collapse
 
hassan_bh profile image
Hassan-BH

I don't know how to thank you dude.
You rock!

Collapse
 
duckyisgood profile image
duckyisgood

can you do it on python?

Collapse
 
agiltriono profile image
Agil Triono

i have been created one discord bot it is been in development but i have the fork version so to try this , it work !!
Used uptimerobot for every 30 minutes with delay 1 minutes .

Collapse
 
bradtft profile image
BradTFT

so when i paste my webserver url into uptime robot is just says "Enter a valid IP or FQDN" any fixes for this?

Collapse
 
wylimee profile image
emily :) |open commisions|

It doesn't work, what to do
also with the new secrets thing idk what
for the "Next we need to instantiate our bot." do we like put it in the same file?

Collapse
 
warengonzaga profile image
Waren Gonzaga

Is there any limits on users? What if you host a music discord bot in your repl it will buffer?

Collapse
 
anmols149 profile image
AnmolS149

I host 3 separate scripts (reps) for the same bot on RepL..they are a chat bot, a rank bot and a music bot...none of them lag...

Collapse
 
partybot profile image
PartyBoy Gaming

I can’t find my web server link what do I do?
(Ps I dont have the web tab in the bottom)

Collapse
 
cyrah profile image
Cyrah Aerin Cirineo

so how do i add a prefix to the bot ?!

ive been trying to do it for awhile but it hasnt been working

Collapse
 
avuleee profile image
avuleee

You have to go to the Secret Enviroment and type in "DISCORD_TOKEN" as the key and your bot's token in value for .env.

Collapse
 
venomoussteam81 profile image
VenomousSteam81

Hey, they made it so people cant make .env files anymore. You have to use Secrets (Environment Variables) tab on the side. Can you make an updated version?

Collapse
 
venomoussteam81 profile image
VenomousSteam81

Nevermind. I figured out a solution!

Collapse
 
codertrek profile image
CoderStarTrek

Does this depend on your computer to be online at all times?

Collapse
 
fizal619 profile image
Fizal Sarif

It does not! Your bot is hosted on replit's servers so you can close the chrome tab even you're done setting it up

Collapse
 
1nterception profile image
Harrison • Edited

Guys this only works on Node.js(as far as i know) and if uptimerobot.com is not working then check the link or pause and start again.

Collapse
 
mlgmemezboi profile image
Existable

How do I make it so then I can add more commands instead of just the ping command?

Collapse
 
mataskrivickas profile image
Force Studios - Comms Open
  1. Get discord.js by downloading node.js
  2. I would recommend this code if you want a 2nd answer from the person and bot:

client.on('message', msg => {
if (msg.content === 'PERSONS MESSAGE') {
msg.reply('BOTS REPLY');
} else if (msg.content === 'PERSONS MESSAGE') {
msg.reply('BOTS REPLY')
}
});

// This is the full message command

Collapse
 
jakyt124 profile image
JAKT

Thanks! Now I can keep my bot on 24/7.

Collapse
 
themegacoders profile image
THEMEGACODERS

To do it with python I just made it http then added the end f it and it worked

Collapse
 
misterjack49 profile image
Joris Mansart

Just followed you tutorial, it works nicely !

Collapse
 
devyxx profile image
devyxx

My bot didnt come online i did everything

Collapse
 
xghouftw profile image
XGHouFTW

What will the repl instantiating code be for a java project?

Collapse
 
fizal619 profile image
Fizal Sarif

I have a repo with a working java version. github.com/fizal619/MerlinBot
Feel free to fork it!