How do I take in user input in discord.py

Nicholas Chen

This may be kinda dumb but I'm trying to make a purge command but theres always something wrong with it. Can anyone help please?

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('p!purge'):
        numberoftimes = input(int('How many times: '))
        await message.channel.purge(limit=str(numberoftimes))
IPSDSILVA

I'll start with answering your question, then I'll clarify on a better way to create a purge command. To answer your question, there is no need to make an input() statement. Rather, this is how you make an input prompt with a Discord bot:

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('p!purge'):
        await message.channel.send('How many messages do you want to purge?')

        while True:
            numberoftimes = client.wait_for('message')  # This means, the client is waiting for a message
            if numberoftimes.author == message.author:  # Make sure that the person responding is the person who did the command
                limit = int(numberoftimes.content)  # If its a string, it will be treated as a word. You want to purge a "number" of messages
        await message.channel.purge(limit=limit+1)

That's how you would purge messages in a channel. But, the way you are creating this command can be made simpler. You can use the command decorator, which is the "standard" way of doing things:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix='p!')

@client.command()
@commands.has_permissions(manage_messages=True)
async def purge(ctx, limit):
    limit = int(limit)
    await ctx.channel.purge(limit=limit + 1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I reliably get user input as a variable from a list of certain options using discord.py?

How to take user input after command and save it as variable discord.py

How do I get the ID of an user in Discord.py

Discord.py: How do I get a user from a UserID?

How do I check if a user has a role in discord.py?

how do I take user input data as a dictionary?

How do i take input from user and search api?

How to DM a discord user with user input from discord.py

How do I get user input from discord chat in python?

How do I make a command in discord.py that sends a dm to a user that i put thei user id?

How do I move a user to a specific channel on discord using the discord.py API

How do I get the user IDs of all the members in my discord server using python and discord.py?

how do i mention a user using user's id in discord.py

Discord.py: How would I get the input from the user after they use a command?

Discord.py: How can I make a command that i can only do with my user ID?

How do I take a user input that ends when the user presses Tab in Python?

Discord.py: How do I get the name of the user who triggers on_member_update?

How do I get someone's user through a mention with discord.py

How do I check if the same user replies to my discord.py bot

discord.py :: How do I make on_message_delete ignore a certain user / role via an embed?

How do I add user's avatar in embed title? discord.py

Discord.py: How do I get the User_ID of a "Bot" object?

How to get user input in discord.py after command

How to get input from mentioned user with discord.py rewrite?

How to accept user input in an embedded message Discord.py

How do i take formatted input in python?

How do i take the input quote from a user and shuffle it then put every letter separate in a table in JavaScript

How do I take user input and store it into an array that is in another class? || Java

How do I create a script that will take user input and use it in a terminal command using python 3.6?