Nicholas Chen
Nicholas Chen

Reputation: 144

How to get user input in discord.py after command

I am wondering how to get user input after the command string. I have this command called p!spam and I want it to do p!spam (message) (amount of times) but I have no clue how to do this. Can someone help me?

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('p!spam'):

Upvotes: 0

Views: 520

Answers (2)

ArJakusz
ArJakusz

Reputation: 11

I would recommend putting the number of times the message is supposed to be sent before the message for simplicity.
The command syntax for the following code is:

p!spam [# of times to repeat] [message]
@client.event
async def on_message(message):
    if message.author.id == client.user.id:
        return

    if message.content.startswith('p!spam'):
        parts = message.content.split(' ')
        del parts[0]
        number = int(parts.pop(0))
        msg = ' '.join(parts)
        for k in range(number):
            await message.channel.send(msg)

Upvotes: 1

Sujit
Sujit

Reputation: 1782

The on_message method isn't a command, it's an event.

Using the on_message() event,

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('p!spam'):
        try:
            spam = split(' ')
            length = len(spam)
            amount = int(spam[length - 1])
            del spam[0]
            del spam[length - 1]
            for i in range (amount):
                await message.channel.send(' '.join(spam))
        except:
            await message.channel.send('Sorry, something went wrong.')

    await client.process_commands(ctx)     # Important for commands to work

The message to call this command would look like this (Which is what you were looking for I believe):

p!spam This is my spam message. 10

Another method, where the amount is at the beginning:

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('p!spam'):
        try:
            spam = split(' ')
            amount = int(spam[1])
            del spam[0]
            del spam[1]
            for i in range (amount):
                await message.channel.send(' '.join(spam))
        except:
            await message.channel.send('Sorry, something went wrong.')

    await client.process_commands(ctx)     # Important for commands to work

The message to call this command would look like this:

p!spam 10 This is my spam message.


Doing this with a command will be a lot easier, you can simply do this:

@client.command
async def spam(ctx, amount, *, spam):
    for i in range(int(amount)):
        await ctx.send(spam)

The message to call this command would look like this:

p!spam 10 This is my spam message.

Upvotes: 0

Related Questions