Creating dynamic command system for Python chat bot

I’m building a chat bot in Python and need help with creating a feature where mods can add new commands on the fly. I want to make something like !createcmd where a moderator types !createcmd !hello Welcome to the stream and then regular users can type !hello to get the response Welcome to the stream.

I’m having trouble figuring out how to parse the chat message and store it properly. Here’s what I tried so far:

def handle_create_command():
    data_file = open("commands.txt", "w")
    user_input = input('')
    data_file.write(user_input)
    data_file.close()
    send_chat_message(CHANNEL, 'New command created successfully')

This doesn’t work because it waits for console input instead of reading from the actual chat. How can I grab the message content from chat and save it so other users can trigger these custom commands later?

I ran into similar issues when building my discord bot last year. The key thing you’re missing is that your chat message handler needs to extract the actual message text and pass it to your command creation function. Most chat APIs give you access to the message content through an event parameter or callback.

For the storage part, I’d recommend using a simple dictionary that gets saved to JSON after each update. Something like loading all commands into memory when the bot starts, then whenever someone creates a new command you update both the in-memory dict and write it back to the file. This way you don’t have file I/O overhead every time someone uses a command.

Also make sure you handle edge cases like duplicate command names and empty responses. Users will definitely try to break it by creating commands with weird characters or super long responses.

The main issue with your current approach is that you’re using input() which blocks and waits for console input rather than processing the actual chat message. You need to pass the chat message content to your function as a parameter instead. Here’s a better approach - modify your function to accept the message content and parse it properly:

def handle_create_command(message_content):
    # Split the message to extract command and response
    parts = message_content.split(' ', 2)  # Split into max 3 parts
    if len(parts) < 3:
        send_chat_message(CHANNEL, 'Usage: !createcmd !commandname response text')
        return
    
    new_command = parts[1]  # The new command like !hello
    response_text = parts[2]  # The response text
    
    # Store in your commands dictionary or file
    with open("commands.txt", "a") as f:
        f.write(f"{new_command}:{response_text}\n")
    
    send_chat_message(CHANNEL, f'Command {new_command} created successfully')

You’ll also need a way to load and check these custom commands when processing regular chat messages. Consider using a dictionary to store commands in memory for faster lookup rather than reading from file every time.

Been working on similar functionality for my twitch bot and found that regex parsing works really well for this use case. Instead of just splitting on spaces, you can use something like re.match(r'!createcmd\s+(\S+)\s+(.+)', message) to properly capture the command name and response text even if there are multiple spaces between parts. This handles cases where users might type extra spaces or the response contains multiple sentences.

One thing I learned the hard way is to sanitize the command names before storing them. You definitely want to strip any special characters and maybe convert to lowercase to avoid conflicts. Also consider setting a character limit on responses otherwise someone will inevitably try to create a command with a massive wall of text that breaks your chat formatting.

you could also use json instead of txt file for storing commands - makes parsing way easier. something like json.dump({"!hello": "welcome msg"}, file) then load it back when bot starts. also dont forget to check if user has mod permissions before letting them create commands lol

honestly sqlite might be overkill but way more reliable than text files if your bot gets popular. also remmeber to add cooldowns on command creation or mods will spam accidentally