I’m working on a Discord bot using Python and I want users to create custom commands right on the server. I’ve tried following an older guide, but it’s not working as expected.
Here’s what I’ve got 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 recognized')
print(f'Context: {ctx}')
print(f'Arguments: {args}')
if len(args) >= 2:
command = args[0]
response = args[1]
custom_commands[command] = 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.reply(custom_commands[command])
else:
await bot.process_commands(message)
bot.run('BOT_TOKEN')
I want to type !add cat meow and have the bot create a custom command where:
Input: !cat
Output: meow
But it’s not saving the commands. The bot sees the arguments but gives a ‘command not found’ error. What am I doing wrong?
I’ve implemented custom commands in my Discord bot and can share some insights from my experience. The issue you’re facing is likely due to the custom commands not persisting between bot restarts. To solve this, you need to implement a storage mechanism. A simple solution is to use a JSON file to store your custom commands. You can create a JSON file to store commands, load commands from the file when the bot starts, and save commands to the file each time new ones are added. Modify your add_command function to save to the file and add a function to load commands on startup. Also, ensure you are not overriding Discord.py’s command processing. Remember to handle errors such as duplicate command names or invalid inputs, and consider providing a way for users to remove custom commands. For a production environment, you might want to explore using a database for more robust storage and better performance.
hey mate, ur code looks alright but the problems lie in the on_message function. it’s blocking ur commands from being processed. try moving the bot.process_commands(message) call to the start of the function, before checking for custom commands. that way, built-in commands like !add will work. also, u might wanna save those custom commands to a file so they don’t disappear when the bot restarts. good luck!
Your approach is on the right track, but there’s a small issue preventing the custom commands from working. The problem lies in your on_message event handler. It’s intercepting all messages before they reach the command processor, so your add_command never gets called.
To fix this, modify your on_message function like this:
async def on_message(message):
if message.author.bot:
return
if message.content.startswith('!'):
command = message.content.split()[0][1:]
if command in custom_commands:
await message.reply(custom_commands[command])
return
await bot.process_commands(message)
This change ensures that custom commands are checked first, but if not found, the message is passed to the regular command processor. Also, consider adding error handling and persistence (like saving to a JSON file) to make your custom command system more robust.