discord.py如何暂时阻止执行以从用户那里获得新指令

时间:2020-08-21 02:33:49

标签: 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)

如果可能,我可以知道如何使某些邮件不受channel.purge的干扰吗?

2 个答案:

答案 0 :(得分:1)

您正在寻找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))

答案 1 :(得分:1)

在整个执行过程中,您将必须使用client.wait_for()函数从用户处获取输入,只需将此代码添加到要获取用户输入的位置即可:

await ctx.send("Choose an option:\n1. Option 1 details here\n2. Option 2 details here\n3. Option 3 details here")
option = await client.wait_for('message', check=lambda message: message.author == ctx.author and message.content != "")
option = option.content;

if (option == "1"):
    # Do something if user chooses 1
elif (option == "2"):
    # Do something if user chooses 2
elif (option == "3"):
    # Do something if user chooses 3
相关问题