0

Say for example I have this:

import discord, asyncio, time

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.lower().startswith("!test"):
        await client.send_message(message.channel,'test')

client.run('clienttokenhere')

I would like to do be able to to do 2 things:

1) Make it so that if and only if the user types in exactly !test and nothing else, it will print out test in the channel

2) Make it so that if the user types in !test first followed by a space and at least one other string character, it wlll print out test -- so for examples: a) !test will not print out anything, b) !test (!test followed by a single space) will not print out anything, c) !test1 will not print out anything, d) !testabc will not print out anything, but e) !test 1 will print out test, f) !test 123abc will print out test, g) !test a will print out test, and h) !test ?!abc123 will print out test, etc.

I only know of startswith and endswith, and far as my research tells, there's no such thing as exact and I'm not sure how to make it so that it requires a minimum number characters after a startswith

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
user152294
  • 159
  • 1
  • 7
  • 3
    Possible duplicate of [Check if string matches pattern](https://stackoverflow.com/questions/12595051/check-if-string-matches-pattern) – Lukas Körfer May 28 '17 at 13:38
  • Please do not vandalise your posts. Once you have submitted a post, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request](https://meta.stackoverflow.com/questions/323395)? – Jed Fox Jun 08 '17 at 15:49

2 Answers2

0

Use the == operator.

1) Print test when there's only !test in the received string

if message.content.lower() == '!test':
    await client.send_message(message.channel,'test')

2) Print test followed by the string when it's followed by a string

# If it starts with !test and a space, and the length of a list
# separated by a space containing only non-empty strings is larger than 1,
# print the string without the first word (which is !test)

if message.content.lower().startswith("!test ") 
   and len([x for x in message.content.lower().split(' ') if x]) > 1:
    await client.send_message(message.channel, 'test ' + ' '.join(message.content.split(' ')[1:]))
0

Looks like you need a regular expression instead of startswith(). And you appear to have two conflicting requirements:

1) Make it so that if and only if the user types in exactly !test and nothing else, it will print out test in the channel

2a) !test will not print out anything

Including 1 and excluding 2a:

import re
test_re = re.compile(r"!test( \S+)?$")
# This says: look for
# !test (optionally followed by one space followed by one or more nonspace chars) followed by EOL
# 

@client.event
async def on_message(message):
    if test_re.match(message.content.lower()):
        await client.send_message(message.channel,'test')

Including 2a and excluding 1, substitute this line (the space and stuff after !test is no longer optional):

test_re = re.compile(r"!test \S+$")
BoarGules
  • 16,440
  • 2
  • 27
  • 44