Discord bot using slash commands in python

 import discord
from discord import app_commands
import os

intents = discord.Intents.default()
intents.messages = True
intents.message_content = True

bot = discord.Client(intents=intents, activity = discord.CustomActivity("Example Activity"))
bot_token = os.environ.get('DISCORD_BOT_TOKEN')

tree = app_commands.CommandTree(bot)

async def on_ready():
    print(f'Logged in as {bot.user.name}')
    await tree.sync()

async def sing_command(interaction, text: str = None):
    """The ping command


About this template

Just a cute /ping bot

Introduction to the Discord Bot Template

Welcome to the step-by-step guide on how to create your own Discord bot using the Lazy template for slash commands in Python. This template provides a basic structure for a Discord bot that responds to the /ping command. It's designed to be a starting point for your bot development, allowing you to add more features and commands as needed.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy or paste any code manually.

Initial Setup

Before you can test and use your Discord bot, you'll need to set up a Discord bot token as an environment secret. Follow these steps to obtain your token:

  • Go to the Discord Developer Portal and create a new application.
  • Navigate to the 'Bot' section within your application settings.
  • Under the 'TOKEN' section, click 'Reset Token' to generate your Discord bot token.
  • Copy the token and go to the Environment Secrets tab within the Lazy Builder.
  • Set the 'DISCORD_BOT_TOKEN' environment secret with the token you copied.

Ensure that the 'Message Content Intent' is enabled in your bot settings if your bot will interact with messages.

Test: Pressing the Test Button

Once you have set up your environment secret, you can press the "Test" button on the Lazy platform. This will begin the deployment of your Discord bot and launch the Lazy CLI. You will not be prompted for any additional user input at this stage.

Using the App

After deployment, your Discord bot will be live and ready to interact with users on your server. To use the bot, simply type the /ping command in any text channel where the bot has permissions to read and send messages. You can also pass additional text to the command, and the bot will include it in its response.

Integrating the App

If you wish to integrate this bot into a larger system or use it with other tools, you may need to provide the bot's server link or API endpoints to those external tools. For example, if you're creating a web dashboard that interacts with your bot, you'll need to use the appropriate Discord API endpoints and ensure your bot has the necessary permissions to perform its functions.

Remember, this template is just the beginning. You can expand your bot's capabilities by adding new commands and features, tailoring it to fit the needs of your Discord server or application.

Happy building, and enjoy your new Discord bot!

Last published
May 18, 2024

More templates like this

To-Do App

A great starting point for a feature-rich To-do List app. This skeleton supports adding, editing, deleting, and prioritizing tasks with due dates and reminders. It's built with Flask and SQLAlchemy, ensuring easy database management. The responsive UI is powered by Tailwind CSS, making it user-friendly and visually appealing. Perfect for creating a robust task management system with potential for further enhancements like project management and collaboration tools.


FALLBACK | Flask, HTML, JS and Tailwind Based Website

This is a good starting point for styled website. It has a header, footer. Has Tailwind and Flowbite loaded so you can build nice looking pages from here.


CSV Deduper

A webpage that dedupes a CSV based on the values in the first column and allows for downloading.

Discord bot using slash commands in python