I built a Discord Bot
Last week, I found myself discussing potential bots for the MonolithTracker Discord Server, when a user mentioned the name "MonolithBot". It seemed like a joke at the time, but as I thought about the idea, it became more and more of a reasonable project to undertake.
I wanted to use Python for this project; So, I did a bit of research.
It turns out, there's a few ways for you to send Discord API requests. One is using a Websocket REST api (Representional-State-Transfer, the most common type of api) and another more alike to a SDK called Discord.py
Discord.py is a Python library for easy interfacing with the Discord API, without worrying about JSON formatting and URL Schemas.
Firstly, I created an application and a bot using the Discord Developer Console. This gave me some API keys for interfacing with my application and controlling my bot.
Then I found a few tutorials to get a base code, I liked the one here at RealPython.com, here it is:
# bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
client.run(TOKEN)import discord
TOKEN = "INSERT TOKEN HERE"
GUILD = "INSERT NAME OF DISCORD SERVER(GUILD) HERE"
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
client.run(TOKEN)What's going on here? Well...
import discord
TOKEN = "INSERT TOKEN HERE"
GUILD = "INSERT NAME OF DISCORD SERVER(GUILD) HERE"client = discord.Client()@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)client.run(TOKEN)Let's run it!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'discord'
Oh dear, we forgot to install Discord.py. No worries though, we just use pip to install it.
pip install discordor maybe use:
pip3 install discordGreat, we're connected!

Well, let's make it useful and add this into our code, before "client.run(TOKEN)":
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!hello':
output = "hello"
await message.channel.send(output)This can be adapted to output anything; as long as it's a string
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!hello':
output = "hello"
await message.channel.send(output)My code
I edited my code to message the MonolithMapper API and collect data, here is my code:
import requests
import time
r = requests.get("https://monolithtracker.com/json-export")
numberofmonoliths = (len(r.json()))
import os
import discord
import random
TOKEN = ""
GUILD ="Monolithtracker"
client = discord.Client()
@client.event
async def on_ready():
guild = discord.utils.get(client.guilds, name=GUILD)
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
commands = {"!help" : "Show help menu",
"!howmany" : "Get amount of Monoliths",
"!random" : "Select a random monolith"
}
commandstring = ""
for i in commands:
commandstring = commandstring+ i+" : "+commands[i]+"\n"
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!help':
output = """__**Commands**__\n"""+"```"+commandstring+"```"
if message.content == '!howmany':
output = "*There are "+str(len(requests.get("https://monolithtracker.com/json-export").json()))+" total monoliths.*"
if message.content == '!random':
response = requests.get("https://monolithtracker.com/json-export").json()
index = random.randint(0,len(response))
output = "*Here's a random monolith:\n"+response[index]["title"][0]["value"]+" ["+"https://monolithtracker.com/node/"+str(response[index]["nid"][0]["value"]) + "]*"
await message.channel.send(output)
client.run(TOKEN)
But when I close the Python, it turns off!
You can use
nohup python3 [file.py] &pskill -9 [process id]
