zeroxspider_

make a discord bot

Kodari AI
KodariNov 11 at 6:43 pm 1
DiscordBotCore
Creates a modular Discord bot framework using discord.py, featuring command handling, event listeners, and configurable settings. Includes a command prefix system, error handling, and extensible cog structure for adding custom features like moderation, music, or utility commands. Provides a clean entry point with async setup and token management.

Created a Discord bot with /hello and /help commands. Add your bot token to .env to run it.

What else would you like the bot to do?

Kodari can make mistakes. Review important code before using.
Files
bot.py
requirements.txt
Code
2 65
bot.py
import logging
import os

import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

bot = commands.Bot(command_prefix=commands.when_mentioned, intents=discord.Intents.default())


@bot.event
async def on_ready():
    print('KODARI_READY', flush=True)
    logger.info('Logged in as %s', bot.user)


@bot.tree.command(name='hello', description='Say hello to the bot')
@app_commands.checks.cooldown(1, 5.0, key=lambda interaction: interaction.user.id)
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f'Hello, {interaction.user.mention}!')


@bot.tree.command(name='help', description='Show the available bot commands')
@app_commands.checks.cooldown(1, 5.0, key=lambda interaction: interaction.user.id)
async def help_command(interaction: discord.Interaction):
    embed = discord.Embed(title='Bot commands', color=discord.Color.blurple())
    embed.add_field(name='/hello', value='Say hello to the bot.', inline=False)
    embed.add_field(name='/help', value='Show this message.', inline=False)
    await interaction.response.send_message(embed=embed, ephemeral=True)


@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
    if isinstance(error, app_commands.CommandOnCooldown):
        message = f'Try again in {error.retry_after:.1f} seconds.'
    else:
        logger.error('Slash command failed', exc_info=(type(error), error, error.__traceback__))
        message = 'Something went wrong while running that command.'
    if interaction.response.is_done():
        await interaction.followup.send(message, ephemeral=True)
    else:
        await interaction.response.send_message(message, ephemeral=True)


async def setup_hook():
    try:
        await bot.tree.sync()
        logger.info('Slash commands synced')
    except discord.DiscordException:
        logger.exception('Failed to sync slash commands')


bot.setup_hook = setup_hook

token = os.getenv('DISCORD_TOKEN')
if not token or token == 'your_discord_bot_token_here':
    raise RuntimeError('Set DISCORD_TOKEN in .env before starting the bot.')
bot.run(token)