Setting up custom server commands for a Discord bot

I’m working on a Discord bot using Python. I want users to make their own commands right on the server. From what I know, I need to use a dictionary to store these custom commands.

I found an old guide and tried to follow it. Here’s my code so far:

import discord
from discord.ext import commands

custom_commands = {}

bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@bot.command('add')
async def add_command(ctx, *args):
    print("Command received")
    print(f'Context: {ctx}')
    print(f'Arguments: {args}')
    
    if len(args) >= 2:
        command_name = args[0]
        response = ' '.join(args[1:])
        custom_commands[command_name] = response

@bot.event
async def on_message(message):
    if message.author.bot:
        return

    command = message.content.split()[0]
    
    if command in custom_commands:
        await message.channel.send(custom_commands[command])
    else:
        await bot.process_commands(message)

bot.run('BOT_TOKEN_HERE')

What I want to do:
When I type !add animal woof, it should create a new command. Then, when someone types !animal, the bot should reply with “woof”.

But it’s not working right. The bot sees the !add command and prints the info, but it doesn’t save the new commands. When I try to use a custom command, I get a “command not found” error.

Any ideas on what I’m doing wrong? How can I make this work?

I’ve been down this road before, and I think I see where the issue lies. Your custom commands aren’t persisting because they’re stored in memory, which resets every time the bot restarts. To fix this, you’ll want to implement some form of data persistence.

One straightforward approach is to use a JSON file to store your custom commands. Here’s a quick outline of how you could modify your code:

  1. Import the json module at the top of your script.
  2. Load existing commands from a JSON file when the bot starts up.
  3. Save new commands to the JSON file in your add_command function.
  4. Update your on_message event to check the JSON file for custom commands.

This way, your custom commands will stick around even if the bot restarts. It’s a simple solution that should get you moving in the right direction. Let me know if you need more specifics on implementation!

I’ve encountered a similar challenge while developing my Discord bot. The issue seems to be that your custom commands aren’t being saved persistently. To resolve this, consider using a database like SQLite or MongoDB to store your commands. This approach offers better scalability and data management compared to in-memory storage or JSON files.

Implement a database connection in your script, then modify your add_command function to insert new commands into the database. Update your on_message event to query the database for custom commands. This method ensures your commands persist across bot restarts and allows for more advanced features like command editing or deletion in the future.

Remember to handle potential database errors and implement proper connection management to avoid issues during runtime. If you need help with the database implementation, feel free to ask for more specific guidance.

hey there! i’ve had similar issues before. looks like ur bot isn’t processing the commands properly. try adding await bot.process_commands(message) at the end of ur on_message event. this should let the bot recognize both custom and built-in commands. hope that helps!