0

How do I make this only convert commands and not arguments or urls?

because if i do >play UrL it converts the url to lower case >play url. I want it only to convert the command and not the url/arguments e.g. >say Hi: I don't want that to convert to >say hi, I want to only convert the command: >SaY Hi to >say Hi.

code:

message.content = message.content.lower().replace(' ', '')
await client.process_commands(message)

the whole code

@client.event
async def on_message(message):
    author = message.author
    if message.author.nick is None:
        author = message.author.display_name
        content = message.content
        channel = message.channel
        print('{}: {}'.format(author, content))
        print('Channel: {}'.format(channel))
        print(' ')
        message.content = message.content.lower().replace(' ', ' ')
        await client.process_commands(message)
    else:
        author = message.author.nick
        content = message.content
        channel = message.channel
        print('{}: {}'.format(author, content))
        print('Channel: {}'.format(channel))
        print(' ')
        message.content = message.content.lower().replace(' ', ' ')
        await client.process_commands(message)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Gaming4me
  • 15
  • 7
  • Not actually a dupe because you're not using the `commands` extension. If you do choose to use that extension (I recommend it) consult [this question](https://stackoverflow.com/questions/48120312/how-to-make-a-command-case-insensitive-in-discord-py) and the linked github issue. [If you're on the `rewrite` branch, you can pass `bot = commands.Bot(command_prefix='>', case_insensitive=True)` to get case-insensitive commands](https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.Bot.case_insensitive). – Patrick Haugh Aug 02 '18 at 20:32
  • How do i use rewrite – Gaming4me Aug 03 '18 at 16:54
  • [How to install](https://stackoverflow.com/questions/50686388/how-to-install-discord-py-rewrite) and [the documentation](https://discordpy.readthedocs.io/en/rewrite/index.html) – Patrick Haugh Aug 03 '18 at 16:56
  • https://gyazo.com/83fd732231227c9cb9796fe76c5fbfe1i can't install – Gaming4me Aug 03 '18 at 17:30
  • Ask a new question and post all of the relevant error details in that question as text. (That link is returning 404) – Patrick Haugh Aug 03 '18 at 18:34
  • https://gyazo.com/2d413799ad9c328e93a9d12c13ca8bef can you just help me here – Gaming4me Aug 03 '18 at 19:44
  • Try running `pip install "yarl<1.2"` first. – Patrick Haugh Aug 03 '18 at 19:47
  • same error should i uninstall the old discord.py – Gaming4me Aug 03 '18 at 21:33

2 Answers2

0

Often times you will see programs have a specific character which indicates a command, like and exclamation point or something. I would recommend this. So you make the command for "say" like this.

!say Hi

Then, when looking at the command, do something like this:

input = message.content
if input[0] == '!':
    input = input.split(' ')
    cmd = input[0].lower()[1:]
    params = input[1:]

else:
    cmd = None
    params = input

if cmd == 'say':
    content = ' '.join(params)

Something like that, if you have any other questions feel free to ask.

Edit:

So you want to take some input such as ">Say Hello" And turn it into ">say Hello"

Lets approach it like this:

content = message.content

if (content[0] == '>'):
    vals = content.split(' ')
    vals[0] = vals[0].lower()
    content = ' '.join(vals)

The value you want is stored in the variable "content"

Matthew H.
  • 113
  • 1
  • 9
  • Huh? Thats odd, you are only making the first word lowercase. Are you sure you are splitting it before you do the .replace(' ', '')? – Matthew H. Aug 02 '18 at 20:26
  • i think i know what im doing wrong one second let me test – Gaming4me Aug 02 '18 at 20:33
  • the problem is with if cmd == 'say': content = params.join(' ') the bot doesn't know what cmd is – Gaming4me Aug 02 '18 at 20:36
  • Ah, I see, I forgot the other case for if there us no !, let me make an edit – Matthew H. Aug 02 '18 at 20:37
  • new error `content = params.join(' ') AttributeError: 'list' object has no attribute 'join'` also sorry if this is annoying you – Gaming4me Aug 02 '18 at 20:53
  • My bad, it is ' '.join(params) – Matthew H. Aug 02 '18 at 20:57
  • `if input[0] == '>': IndexError: string index out of range` e.e – Gaming4me Aug 02 '18 at 21:23
  • Did you input an empty string? – Matthew H. Aug 02 '18 at 21:33
  • no i didn't i said >say Hi also this doesn't convert the command because when i did >Say hi it said `discord.ext.commands.errors.CommandNotFound: Command "Say" is not found` so it doesn't convert the command still – Gaming4me Aug 02 '18 at 22:03
  • Im not sure what to say without looking at it closer, but it looks like Patrick, who commented on the question, knows a method using already available code. Id look into that – Matthew H. Aug 02 '18 at 22:32
  • Also, as a note, I just ran that code with a few test cases and it worked fine. At this point the issue might be different from the original question, in which case you need to open a new question. – Matthew H. Aug 02 '18 at 22:40
0

I would set up a white list of commands you don't want to convert to lowercase. Split the message along spaces. Then you can use a nested for loop to check every word against the whitelist. If there is a match, convert the word to lowercase, otherwise keep it the same. Finally merge the list of words back into a sentence.

whitelist = 'say', 'play'
text = 'SaY hello'
words = text.split(' ')
for i in range(len(words)):
    for cmd in whitelist:
        if words[i].lower() == cmd:
            words[i] = words[i].lower()
finaltext = ' '.join(words)

print(text)
print(finaltext)

The output will be:

SaY hello
say hello
Aeolus
  • 996
  • 1
  • 8
  • 22