Custom Commands Bot for Discord

By
 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

About this template

This template provides a Discord bot created in Python that enables users to generate and utilize text commands. Users can interact with this bot by employing the !addCommand feature, in the format; "!addCommand ! ". At present only custom text commands with tailored text responses are compatible with this bot.

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!

Category
Technology
Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.

Streamlit
Python

A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .

Python
Flask

WhatsApp ChatBot (Twilio, InstantChat)

Build a WhatApp chatbot using Twilio, Discord and LazyAI.

WhatsApp
Twilio
Discord
Home
/
Custom Commands Bot for Discord