Discord bot using slash commands in python

meow_18838
 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)

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

@tree.command(name='ping')
async def sing_command(interaction, text: str = None):
    """The ping command

    Parameters

About this template

Update: /ping can be used in user apps too now.

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. This template provides you with a basic Discord bot that responds to a /ping command with a "Pong!" message. It's a great starting point for building more complex bots with additional functionality.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, ready for customization and deployment.

Initial Setup

Before you can test and deploy your bot, you need to set up an environment secret for your Discord bot token. Follow these steps to obtain and set up your Discord bot token:

  • Go to the Discord Developer Portal and create a new application.
  • Navigate to the 'Bot' section and click 'Reset Token' to get your Discord bot token.
  • Enable the 'Message Content Intent' slider if your bot will read or reply to messages.
  • In the Lazy Builder, go to the Environment Secrets tab.
  • Set the 'DISCORD_BOT_TOKEN' with the token you obtained from the Discord Developer Portal.

Test

With the environment secret set, you can now press the "Test" button in the Lazy Builder. This will deploy your application and launch the Lazy CLI. If the code requires any user input, you will be prompted to provide it through the Lazy CLI.

Entering Input

For this particular template, there is no additional user input required through the CLI after pressing the test button, as the bot token is already set as an environment secret.

Using the Bot

Once your bot is deployed, you can invite it to your Discord server using the OAuth2 URL provided by Discord. After inviting the bot, you can use the /ping command in any text channel where the bot has permission to read and send messages. The bot will respond with "Pong!" or "Pong: [your text]" if you provide additional text with the command.

Integrating the Bot

If you wish to integrate this bot into other services or frontends, you may need to provide the bot's server link or API endpoints to the external tool. For this basic template, such integration steps are not required. However, as you expand the bot's functionality, you may need to consider what permissions the bot needs and where to add the code in the external tool to achieve your goals.

Remember, this is just the beginning! With the Lazy platform, you can easily add more commands and features to your Discord bot to make it even more powerful and tailored to your needs.

Category
Technology
Last published
July 26, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

Simple Multiplayer Telegram game

This app is a simple frontend for a game where users can upvote and downvote the most popular word in their country, learn about the flags of other countries, and view what other people voted for on a leaderboard.

Telegram
Python
Javascript

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
Discord bot using slash commands in python