kioku-space

As a personal memory space.

Building a Discord Chatbot with Python (8) - Adding Options to Bot Commands

Continuing from our last session, we’ll be using discord.ext.commands to add options to our chatbot commands.
(Reference: Documentation)

We’ll add a new ‘category’ option to the ‘quiz’ feature, allowing for quizzes in various categories.
Additionally, we’ll introduce a ’timeout’ option to adjust the time limit for answering questions.

In our previous article, we refactored the code using discord.py’s extension, discord.ext.commands.

The code for the ‘quiz’ feature, found in commands/quiz.py, is as follows:

Building a Discord Chatbot with Python (7) - Refactoring Code Using Discord.py's Extension

In this article, we’ll leverage the discord.ext.commands extension in discord.py
to refactor our somewhat complex on_message function.

The code up to our last session is as follows:
We added !omikuji and !quiz commands, which made the on_message function somewhat bloated.

Building a Discord Chatbot with Python (6) - Adding a 'Quiz' Feature

In this post, we’ll introduce a method to wait for user replies and leverage it to add a new ‘Quiz’ feature to our chatbot.

Previously, we delved into how to reply to user messages.
We then employed this mechanism to incorporate a ‘fortune-telling’ feature.

Building a Discord Chatbot with Python (5) - Adding a 'Fortune Telling' Feature

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.

As of our previous session, our chatbot can now receive user messages in a chat.
Here’s what our chatbot.py script currently looks like:

Building a Discord Chatbot with Python (4) - Receiving Messages with Your Chatbot

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:

  1. Move to the discord-chatbot directory and activate the virtual environment:

Building a Discord Chatbot with Python (3) - Adding Your Bot to Discord

In this article, we’ll walk through the Discord settings for your chatbot.

If you don’t already have a Discord account, start by creating one.
For a guide on account creation, check out this link.

Next, set up the Discord server where your chatbot will operate.

  1. Click the ‘+’ icon in the top-left corner.

  2. Choose “Create My Own”.

  3. Select “For me and my friends”.