In this article, we’ll expand our chatbot by enabling it to respond to user messages.
We’ll also introduce a fun ‘Fortune Telling’ feature to our chatbot.
A Recap of Our ProgressAs of our previous session, our chatbot can now receive user messages in a chat.
Here’s what our chatbot.py script currently looks like:
import discord class MyClient(discord.Client): async def on_ready(self): """Triggered when connecting to Discord.""" print(f'Connected as {self.user}.') async def on_message(self, message: discord.
In this article, we’ll finally dive into creating a chatbot in Python.
Firstly, ensure that your working directory, discord-chatbot, contains at least the following two components from our previous sessions:
.venv directory: This is for a virtual environment.
Related Content Building a Discord Chatbot with Python (2) - Setting Up the Development Environment .discord_token file: This file contains a Discord token.
Related Content Building a Discord Chatbot with Python (3) - Adding Your Bot to Discord 1.
In this article, we’ll walk through the Discord settings for your chatbot.
1. Create a Discord AccountIf you don’t already have a Discord account, start by creating one.
For a guide on account creation, check out this link.
2. Create a Discord ServerNext, set up the Discord server where your chatbot will operate.
Click the ‘+’ icon in the top-left corner.
Choose “Create My Own”.
Select “For me and my friends”.
In this article, we’ll prepare the development environment to build a Discord chatbot in Python.
We’ll be utilizing the discord.py library.
Ensure you have Python version 3.8 or higher to use this library.
1. Create a Working DirectoryLet’s begin by creating a working directory named discord-chatbot.
All our operations will be performed within this directory.
mkdir discord-chatbot cd discord-chatbot 2. Ensure Python 3.8 or Higher is Installed Check your current Python version with the following command:
In this guide, we’ll create a “chatbot”, a program that automatically responds to messages in a chat using Python.
Developing a chatbot is a rewarding next step for beginners who have just completed their basic tutorials or courses.
Over the course of several articles, we’ll add various features to our chatbot, making it more versatile and interactive.
Goals of This SeriesThroughout this series, we aim to add the following functions to our chatbot:
This is a quick note on how to use venv, a Python library for creating virtual environments.
List of Commands Creating a virtual environment:
python -m venv <path-to-virtual-environment> Activating the virtual environment (for bash users. For other shells, refer to the documentation):
source <path-to-virtual-environment>/bin/activate Deactivating the virtual environment:
deactivate Notes Since Python 3.3, a part of virtualenv was added to the standard library as venv. By creating a virtual environment, you can develop programs in an isolated space without affecting your main environment.