Audience: friends I’m teaching to code.

Warning, these are likely to change as WSL and discord.py do.

If you get stuck anywhere or want to know more, DM me!

Goal

A Discord bot on your local computer that runs when your computer is on.

Prerequisites

  • Know how to read

You will learn

  • How to read long instructions, ignoring irrelevant info so you can find what you need quickly
  • How to use PowerShell, the Windows command line
  • How to use Bash, the Linux command line

Overview

  • (if Windows) install Windows Subsystem for Linux and upgrade it to version 2
  • Install Windows Terminal
  • Install Python and library discord.py
  • Make a new Discord Server and add me/the bot to it
  • Copy some quickstart code into a file and save it

Install WSL v2

Instructions adapted from these, I’ve pulled out the relevant parts as of August 2020.

I recommend WSLv2 because it is easier to work with.

  • Enable WSL
    • Open PowerShell as Administrator (search for it, right click -> run as administrator)
    • Paste the following into it:
    • dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Make sure Windows is a high enough version
    • Press Windows key and r, type winver
    • Verify (OS Build xxxxx) is >= 19041
    • If not, update Windows
  • Enable Virtual Machine Platform
    • Open PowerShell as Administrator and run:
    • dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    • Restart
  • Open Microsoft Store, search for Ubuntu, and install the first result.
  • Open Microsoft Store, search for Windows Terminal, and install the first result.

Install Python

  • Open Windows Terminal to Ubuntu
  • Type the following into the command line
    • sudo apt update && sudo apt install python3 python3-pip -y && pip3 install discord.py

Create a Discord bot and add it to your server

  • Make a new Discord server.
  • Create a bot and add it to this server.
  • Save the bot token!

Create the bot code

  • In Ubuntu:
    • touch main.py
    • explorer .
  • Open main.py with a text editor (right click -> Open with...).
  • Paste the following code in and save (adding your token):
# from https://discordpy.readthedocs.io/en/latest/quickstart.html#a-minimal-bot
# This example requires the 'message_content' intent.

import discord

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run('your token here')

And that’s it!

Start the bot with python3 main.py

Check the discord server and try typing $hello!

Edit the bot by:

  • changing main.py (try changing $hello to $greetings)
  • saving the file
  • hitting Ctrl and c in the terminal window
  • typing python3 main.py again (or pushing up arrow key and then enter).

If you got stuck anywhere, DM me.