首页 > 解决方案 > discord.py 如何暂时阻止执行以从用户那里获取新指令

问题描述

嗨,我正在为我的不和谐服务器的新成员创建一个初始教程,但我坚持一点:我希望在执行教程期间暂时冻结以让用户选择选项,但我不知道如何要做到这一点,我认为它可能来自 asyncio 库,但我不知道。我发送完整的代码:

@client.event
async def on_message(message):
    if message.content == "start":
        #ruoli
        accademico_in_preparazione = get(guild.roles, id=int("746178678274261043"))
        mappercell = get(guild.roles, id=int("746187955315540059"))
        mapperpc = get(guild.roles, id=int("746187952987701250"))
        accademico = get(guild.roles, id=int("690964416644251750"))
        #canali
        regolamento = client.get_channel(727304286215733249)
        annunci = client.get_channel(675101744115744789)
        programmazione = client.get_channel(724936407835213914)
        generaleacc = client.get_channel(697872690505187328)
        corsibase = client.get_channel(675103006953570324)
        guida = client.get_channel(724680601482952714)
        regolamentolmii = client.get_channel(646026232420368395)
        costituzione = client.get_channel(643547822284865556)
        generale = client.get_channel(640563710104043530)
        #rendere utilizzabile il comando su un canale specifico
        channel = client.get_channel(746145103403286618)
        if ctx.channel.id == channel.id:
            await ctx.channel.set_permissions(accademico_in_preparazione, read_messages=True, send_messages=False)
            await message.channel.send("Salve nuovo accademico benvenuto nell'accademia del LMII!")
            await asyncio.sleep(5)
            await message.channel.send("Prima di lasciarti la libertà ti spiego alcune cose importanti")
            await asyncio.sleep(5)
            await message.channel.send("Essendo un accademico sei limitato rispetto ad altri membri, il tuo compito è quello di completare i corsi dedicati per diventare ufficialmente un membro del LMII!")
            await asyncio.sleep(7.5)
            await message.channel.send("Perchè non posso imparare essendo direttamente membro?")
            await asyncio.sleep(5)
            await message.channel.send("Ci teniamo che i nostri membri ufficiali siano già dei bravi mapper che abbiano delle basi solite, ti consiglio vivamente di guardare questi seguenti canali:")
            await asyncio.sleep(7.5)
            await message.channel.send(f"{regolamento.mention} : Il regolamento della nostra accademia!")
            await asyncio.sleep(2)
            await message.channel.send(f"{annunci.menttion} : gli annunci dell'accademia , potresti ricevere (in caso hai i messaggi privati aperti) dei messaggi privati dal bot che sono degli annunci importanti del server")
            await asyncio.sleep(8)
            await message.channel.send(f"{programmazione.mention} : qui c'è il programma dell'accademia , dacci un occchiata!")
            await asyncio.sleep(5)
            await message.channel.send(f"{generaleacc.mention} : questo è il canale dove si può parlare di tutto riguardante l'accademia sfruttalo per ogni evenienza")
            await asyncio.sleep(6)
            await message.channel.send(f"{corsibase.mention} : qui ci sono le informazioni sui corsi che interessano a te! ")
            await asyncio.sleep(5)
            await message.channel.send(f"{guida.mention} : qui per altre informazioni dettagliate sull'accademia!")
            await asyncio.sleep(1)
            await message.channel.send(f"\n")
            await asyncio.sleep(5)
            await message.channel.send(f"Essendo accademico hai accessi limitati ma puoi accedere tranquillamente alla zona mapping nel LMII e ai canali principali della zona pubblica fra cui {generale.mention} dove puoi parlare di tutto e fare buone conoscenze!")
            await asyncio.sleep(10)
            await message.channel.send(f"Attenzione ci sono da rispettare anche il {regolamentolmii.mention} e la {costituzione.mention} che sono molto importanti!")
            await asyncio.sleep(1)
            await message.channel.send(f"\n")
            await asyncio.sleep(6)
            await message.channel.send(f"Ora dovrai rispondere semplicemente ad una domanda per poi poter finalmente accedere a tutti i canali!")
            await asyncio.sleep(10)
            await message.channel.send(f"Sei un mapper da **telefono** o da **PC**?")
            await asyncio.sleep(5)
            await ctx.channel.set_permissions(message.author, send_messages=True)
            ????
            await ctx.channel.set_permissions(message.author, overwrite=None)
            if message.content == "PC":
                await message.channel.send("Quindi sei un mapper da pc!")
                await message.author.add_roles(mapperpc)
                await message.channel.send("Ora tutti sapranno che sei un mapper da PC , in caso cambi idea chiedi a qualche amministratore di cambiare il ruolo in modo manuale!")
                await asyncio.sleep(6)
                await message.channel.send("Ora sei pronto! Se vuoi sapere di più sul bot digita **lmiihelp**\n\nBot programmato da : Peppewarrior Italy")
                await asyncio.sleep(7)
                await message.author.remove_roles(accademico_in_preparazione)
                await message.authro.add_roles(accademico)
                await asyncio.sleep(5)
                await message.channel.purge(limit=100)
                await ctx.channel.set_permissions(accademico_in_preparazione, read_messages=True)
            elif message.content == "telefono":
                await message.channel.send("Quindi sei un mapper da telefono!")
                await message.author.add_roles(mappercell)
                await message.channel.send("Ora tutti sapranno che sei un mapper da telefono , in caso cambi idea chiedi a qualche amministratore di cambiare il ruolo in modo manuale!")
                await asyncio.sleep(6)
                await message.channel.send("Ora sei pronto! Se vuoi sapere di più sul bot digita **lmiihelp**\n\nBot programmato da : Peppewarrior Italy")
                await asyncio.sleep(7)
                await message.author.remove_roles(accademico_in_preparazione)
                await message.authro.add_roles(accademico)
                await asyncio.sleep(5)
                await message.channel.purge(limit=100)
                await ctx.channel.set_permissions(accademico_in_preparazione, read_messages=True)
    await client.process_commands(message)

PS如果可能的话,我可以知道如何使某些消息免受channel.purge

标签: discord.py

解决方案


您正在寻找Client.wait_for,用于等待用户回复。discord.py 文档中有一个非常简单的示例说明如何实现这一点。

@client.event
async def on_message(message):
    if message.content.startswith('$greet'):
        channel = message.channel
        await channel.send('Say hello!')

        def check(m):
            return m.content == 'hello' and m.channel == channel

        msg = await client.wait_for('message', check=check)
        await channel.send('Hello {.author}!'.format(msg))

推荐阅读