discord.py 실행을 일시적으로 차단하여 사용자로부터 새 명령을받는 방법
Aug 21 2020
안녕하세요 저는 제 discord 서버의 새 멤버를위한 초기 튜토리얼을 만들고 있는데 한 가지 문제가 있습니다. 튜토리얼을 실행하는 동안 잠시 정지하여 사용자가 옵션을 선택할 수 있도록하고 싶지만 방법을 모르겠습니다. 그것을하기 위해, 나는 그것이 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
있습니까?
답변
1 NuKeFluffy Aug 21 2020 at 21:43
이 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
1 Jawad Aug 21 2020 at 11:05
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))