Custom Commands Bot for Discord

Start with this template
297
import os
import discord
from discord.ext import commands
from discord.ext.commands import Context, CommandNotFound

# Retrieve the Discord bot token from the environment variables
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')

# Initialize the bot with a command prefix
bot = commands.Bot(command_prefix='!')

# A dictionary to store custom commands and their responses
custom_commands = {}

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')

@bot.command(name='addCommand', help='Adds a custom command with a response')
async def add_command(ctx: Context, command: str, *, response: str):
    # Strip the command prefix if included in the command argument
    command = command.lstrip('!')
    # Add the command and its response to the custom_commands dictionary
    custom_commands[command] = response
Get full code

Custom Commands Bot for Discord

Created: | Last Updated:

Welcome to the guide on using the Custom Commands Bot Discord template on Lazy. This template enables you to establish a Discord bot of learning and responding with customized text commands. It's a way to add a touch to your Discord server without requiring coding expertise.

Getting Started with the Template

To initiate use of this template simply select "Start with this Template" within the Lazy platform. This action will automatically populate the code in the Lazy Builder interface eliminating the need for code copying or pasting.

Initial Setup

Before utilizing the bot you must configure an environment, for the Discord bot token. Follow these steps to obtain and configure your Discord bot token;

1. Visit the Discord Developer Portal. Establish an application.

Under the "Bot" section simply click on "Add Bot" to generate a bot user. Remember to save the token provided for your bot. In the Lazy Builder head over to the Environment Secrets tab. Create a secret labeled as `DISCORD_BOT_TOKEN`. Paste the copied token, as its value. To test it out just hit the Test Button.

2. Once you've set up your environment secret give the "Test" button in the Lazy Builder a push. This action will kickstart the deployment process of your Discord bot. Open up the Lazy CLI. No further input from you will be required at this point.

Using the Application

Following deployment your Discord bot will be up. Running for you to utilize. To introduce a personalized command employ the `!. Feature, in your Discord server using this structure;

!addCommand !. Your unique response goes here

For instance if you wish for the bot to reply with "Hello, world!" upon someone typing `!greet` simply input;

!addCommand !greet Hello, world!

When someone types `!greet`, on your Discord server the bot will greet them with a Hello, world!"

Adding the Bot to Platforms

If you plan to use this bot on different platforms or interfaces you might have to share the server link or API endpoints, for the bot. However this specific setup doesn't require those integrations. Its tailored for Discord. Doesn't offer an API or front end interface.

Just remember all these steps are carried out on the Lazy platform so you won't have to deal with installing libraries or configuring your environment. Lazy takes care of everything for you ensuring a process.

Have fun personalizing your Discord server with your bot!

Technologies

Python Python
Discord Discord