2.3.8. Demo: Try Running the Discord Bot!#

Choose Social Media Platform: Reddit | Discord | Bluesky

Running this Jupyter Notebook#

This page is called a “Jupyter Notebook” which it is a text page that has runnable Python code in it.

In order to run the code, you can look for the rocket button at the top which will give you an option to “launch binder.”

screenshot of this page in the online textbook, with the launch binder link highlighted under the rocket button at the top.

If you launch binder, it will take a while to load, but eventually show you a version of this Jupyter Notebook page in a code editor called Jupyter Lab.

a screenshot of this page viewed in jupyter_lab, with menus and options above the editable page

In Jupyter Lab you can double click any section to edit it, and you can press the triangle “run” button to run the code (or display the text).

a screenshot of this page viewed in jupyter_lab,with the triangle "run" button circled. Next to it are a square "interrupt the kernal" button and other options

When the code runs, the little number to the left of the code block should change. There might also be some output from your action displayed below the code block.

So now you can go through the rest of this page and select and run each section of code.

Here is the bot code you can run!#

Our demo Discord bot code is below, broken up into different sections.

You can select each section of the code below and run it to see what it does.

By default this code uses a fake version of our Discord connection so it doesn’t connect to a real Discord account.

If you want to actually connect to your Discord account, you can put your discord token in the right code section below, and then when you run the code make sure to skip the code section that makes a fake discord connection with “fake_discord”.

Step 1: Load discord code#

# Load some code called "discord" that will help us work with Discord
import discord

# Load another library that helps the bot work in Jupyter Noteboook
import nest_asyncio
nest_asyncio.apply()

(Optional) Step 1b: Make a fake discord connection with the fake_discord library#

For testing purposes, we’ve added this line of code, which loads a fake version of discord, so it wont actually connect to Discord. If you want to try to actually connect to Discord, don’t run this line of code.

%run ../../fake_apis/fake_discord.ipynb
Fake discord is replacing the discord.py library. Fake discord doesn't need real passwords, and prevents you from accessing real discord

Step 2: Set up your Discord connection#

To use this on your real Discord account, copy your discord token into the code below, replacing our fake passwords.

# Set up your Discord connection
# TODO: put the discord token for your bot below
discord_token = "m#5@_fake_discord_token_$%Ds"
client = discord.Client(intents=discord.Intents.default())
Fake discord is pretending to set up a client connection

Step 3: Define what your bot will do#

These are the steps your bot will take when it loads (get the channel, post a message, and shut down). If you want to post on an actual Discord channel you will have to replace the fake channel_id below with your real channel id (see the making bot instructions)

# Provide instructions for what your discord bot should do once it has logged in
@client.event
async def on_ready():
    # Load the discord channel you want to post to
    # TODO: put the discord channel id number below
    channel_id = 123456789
    channel = client.get_channel(channel_id)

    # Post a message to your discord channel
    # TODO: put the message you want to post to discord below
    await channel.send("This post was made by a computer program!")

    # Tell your bot to stop running
    await client.close()

Step 4: Run your bot#

This final step will run your bot, which you defined in the steps above.

# Now that we've defined how the bot should work, start running your bot
client.run(discord_token)
Fake discord bot is fake logging in and starting to run
Fake Discord is pretending to post:
This post was made by a computer program!
Fake discord bot is shutting down

Step 5: Modify the bot code above to create a different discord post, and run it#

Note: In order to run the bot again, you have to go back and run the code again starting at Step 2, otherwise you will get an error message (RuntimeError: Session is closed) about how your bot is no longer logged into Discord.