-
Login to Discord Developer Portal or register if you somehow don't have an account.
-
Click
New Application
, go toBot
-> add bot field (keep default settings) -> clickCopy
to copy your token. This token is basically a password, so don't share it with anyone. If you leak it then generate another one. -
Go to
OAuth2
-> addbot
in scopes -> add bot permissions (of course they depend on you bot's purposes, butsend messages
,read message history
,mention everyone
,add reactions
, andview channels
are basically a must have). -
Copy the url from the
scopes
field. This is the invitation that allows you to join the bot to your servers. After using the link (paste it into a web browser) your bot should join a selected server, but it will be offline.
-
Go to replit.com and create new python repl. Name of the repl is not important. If you want to use your own server then simply create
main.py
file that you will have to run. -
Paste the following code into the
main.py
:
import discord
import os
client = discord.Client()
@client.event
async def on_ready():
print('The bot is ready')
@client.event
async def on_message(message):
if message.author == client.user:
return
if messsage.content.startswith('hello'):
await message.channel.send('sup')
# When using replit (which is public) one must hide the token by creating so called secret
# repl (left panel) -> tools -> secrets -> paste the token and name the variable e.g. BOT_TOKEN
client.run(os.getenv('BOT_TOKEN'))
- Run the script. At this point your bot should be online and should reply to the hello message.
We need to make our bot run a web server in order to keep the bot alive (in case of no requests that will put the bot to sleep). You can read more about this issue here.
- Create file, e.g.
online.py
in your repl and paste this code:
from flask import Flask
from threading import Thread
app = Flask('')
@app.route('/')
def home():
return "Online"
def run():
app.run(host = '0.0.0.0', port = 8080)
def keep_alive():
t = Thread(target = run)
t.start()
- Update
main.py
by adding
from online import keep_alive
...
keep_alive()
client.run(os.getenv('BOT_TOKEN'))
-
Run the code. A website with white background and text Online should appear. Copy the url of the webpage.
-
Login to uptimerobot.com (register if needed) and click
Add New Monitor
. Set monitor type to https. Name is not important. Paste your url into the URL field. Set monitoring interval to 5 minutes. ClickCreate monitor
.
Your bot is ready and should continue to run, even when there is no activity on your server for a longer peroid of time.