Как использовать команду wait_for в событии 'on message' перезаписать discord.py - PullRequest
0 голосов
/ 02 апреля 2020

Я пытаюсь сделать так, чтобы диск-бот имел ту же функциональность, что и команда input(), но поскольку в переписывающем файле discord.py такой команды не было, я искал API и обнаружил wait_for. Но, конечно, это принесло с собой массу проблем. Я искал inte rnet для этого, но большинство ответов были в @command.command, а не async def on_message(message), а остальные не были действительно полезны. самое дальнее, что я получил, было:

def check(m):
    if m.author.name == message.author.name and m.channel.name == message.channel.name:
        return True
    else:
        return False
msg = "404 file not found"
try:
msg = await client.wait_for('message', check=check, timeout=60)
await message.channel.send(msg)
except TimeoutError:
    await message.channel.send("timed out. try again.")
    pass
except Exception as e:
    print(e)
    pass

    ```

1 Ответ

1 голос
/ 04 апреля 2020

Прежде всего, вы используете одну и ту же переменную msg для нескольких вещей. Вот рабочий пример, который я могу сделать с предоставленной вами информацией.

msg = "404 file not found"
await message.channel.send(msg)

def check(m):
    return m.author == message.author and m.channel == message.channel

try:
    mesg = await client.wait_for("message", check=check, timeout=60)
except TimeoutError: # The only error this can raise is an asyncio.TimeoutError
    return await message.channel.send("Timed out, try again.")
await message.channel.send(mesg.content) # mesg.content is the response, do whatever you want with this

mesg возвращает сообщение объект.

Надеюсь, это поможет!

...