Search code examples
pythonargumentsdiscord.pyargs

Discord.py multi word arguments, another option?


I know I can use *args, I know I can use *, arg, and I know I can use quotes around the command to make them read as one argument. The last option seems appealing to be but is there a way to make the bot automatically assume there are quotes around a certain argument to allow multiple words? Maybe allowing as many words as I want until a specific character is used, then it breaks into the next argument? I can't use *args for this because the final argument needs to allow multiple words too. Without reconstructing the command, how can I allow an argument to accept multiple words?

I've seen a bot do this before, but it was in js, so I'm actually thinking it might not be possible but idk.


Solution

  • This function will let you do that. It will loop through the string, adding each character to a separate string, and if it comes across the specified delimiter, it appends that string to a list. It keeps doing this until it reaches the end of the given string.

    def seperArgs(arg,delimeter):
        finalArgs = []
        toAppend = ''
        index = 0
        for i in arg:
            if(i == delimeter):
                finalArgs.append(toAppend.strip())
                toAppend = ''
            else:
                toAppend += i
            if(index == len(arg) - 1):
                finalArgs.append(toAppend.strip())
                toAppend = ''
            index += 1
        return finalArgs
    
    print(seperArgs('hello my name is -world -land','-'))
    
    #should print: ['hello my name is', 'world', 'land']