我如何在 discord.py 中接收用户输入

时间:2021-01-06 19:49:53

标签: discord.py

这可能有点愚蠢,但我正在尝试发出清除命令,但它总是有问题。请问有人可以帮忙吗?

@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))

1 个答案:

答案 0 :(得分:2)

我将首先回答您的问题,然后我将阐明创建清除命令的更好方法。要回答您的问题,无需作出 input() 声明。相反,这就是您使用 Discord 机器人进行输入提示的方式:

@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)

这就是您清除频道中消息的方式。但是,您创建此命令的方式可以更简单。您可以使用命令装饰器,这是“标准”的做事方式:

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)