You are on page 1of 18

twitch.

tv/MaraDrinksMilk

discord.py
how to make a discord bot with python and host it 24/7 for free
twitch.tv/MaraDrinksMilk

I'm Mara Hart!


Streamer specializing
in coding content
discord.gg/ebMsPKpkBu
Returning SWE Intern at biweekly challenges + community

Incoming Design Automation


Intern at

Computer Science major from


the linkedin.com/in/maralihart
twitch.tv/MaraDrinksMilk

Why a Discord Bot?


Shifting demographics on Discord
Ease of automation
People pay $$$
Quick and easy set up
twitch.tv/MaraDrinksMilk
Create a Test Server
1 3

SET 2

UP 4
twitch.tv/MaraDrinksMilk
Create a Developer Account
https://discord.com/developers/applications

SET 1. Create a New Application


2. Sidebar > Bot > Add a New Bot
3. Sidebar > OAuth2 > Give Permissions > Bot > Administrator

UP
a. Admin is a strong permission to give a bot, but allows you
to do a lot and bypass a lot of private channels. A lot of
Discord bots will ask for Admin, but if you want, you can
limit permissions in your future bots!
4. Copy the link generated
5. Go to the link and add the bot to your new test server!
twitch.tv/MaraDrinksMilk

LET'S GET replit.com


CODING
create a Python replit
twitch.tv/MaraDrinksMilk

Shhh...
Create a secret variable
for your bot token
twitch.tv/MaraDrinksMilk

main.py
import discord
import os

bot = discord.Client()

@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))

@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')

bot.run(os.environ['TOKEN'])
twitch.tv/MaraDrinksMilk

main.py
...
@bot.event
async def on_message(message):
...
if message.content.startswith('$echo'):
await message.channel.send(message.content[5:])

...
twitch.tv/MaraDrinksMilk

main.py
...
@bot.event
async def on_message(message):
...
if message.content.startswith('$hi'):
if len(message.mentions) != 0:
await message.channel.send("Hey, <@" + message.author.id +
"> !")
else:
for mention in message.mentions:
await message.channel.send("Hey, <@" + mention.id + "> !")

...
twitch.tv/MaraDrinksMilk

main.py
...
@bot.event
async def on_message(message):
...
if message.content.startswith('$channel'):
channel_name = message.content[8:]
await message.channel.category.create_text_channel(channel_name)

...
twitch.tv/MaraDrinksMilk

Hosting 24/7 for free!


twitch.tv/MaraDrinksMilk
twitch.tv/MaraDrinksMilk

stay_awake.py

from flask import Flask


from threading import Thread
# code from FreeCodeCamp
app = Flask("")

@app.route('/')
def home():
return "Hey, I'm awake!"

def run():
app.run(host='0.0.0.0', port=8080)

def stay_awake():
t = Thread(target=run)
t.start()
twitch.tv/MaraDrinksMilk

main.py

import discord
import os
from stay_awake import stay_awake

stay_awake()

bot = discord.Client()

@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))

...
twitch.tv/MaraDrinksMilk
twitch.tv/MaraDrinksMilk

Thanks for
tuning in! discord.gg/ebMsPKpkBu
biweekly challenges + community

linkedin.com/in/maralihart
MaraDrinksMilk

You might also like