x secons 이후에 discord.py에서 사용자가 작성한 명령을 제거하는 방법

Jan 19 2021

그래서 나는 명령을 내렸다 $mute @name which basically mutes a player when typed. But what i want is when i type the command , after 5 or 10 seconds the command input line that is $@name 음소거는 채팅에서 제거됩니다.

답변

IPSDSILVA Jan 19 2021 at 19:08

이는 명령 후 5-10 초 동안 기다렸다가 삭제해야 함을 의미합니다. 봇에 메시지 삭제 권한이 있는지 확인하세요.

@client.command()
async def mute(ctx, member: discord.Member):
    muted_role = ctx.guild.get_role(YOUR_ROLE_ID)  # Make sure not to put it in a string
    await member.add_roles(muted_role)
    await asyncio.sleep(5)  # Make sure you import asyncio, also change the 5 to whatever seconds you would like
    await ctx.message.delete()

위 코드는 사용자에게 Muted역할 (해당 역할의 ID를 통해 지정한 역할)을 부여하고 해당 역할을 구성원에게 부여합니다. 그런 다음 5 초 동안 기다린 다음 (원하는대로 변경할 수 있음) 명령 메시지를 삭제합니다.

Bagle Jan 19 2021 at 19:09

사용할 수있는 두 가지 방법이 있습니다.


첫 번째 방법은 import 를 사용 await asyncio.sleep하는 것 입니다. 우리가 사용하지 않는 이유는 이것이 차단기이기 때문입니다. 차단기는 한 곳에서이 명령을 사용하면 전체 봇을 중지하고 완료 될 때까지 다른 사람이 명령을 사용할 수 없음을 의미합니다. 예는 다음과 같습니다.asynciotime.sleep

import asyncio

@client.command()
async def test(ctx):
    await ctx.message.delete() # deletes message sent by user
    # do some things here
    msg = await ctx.send("done")
    await asyncio.sleep(5) # waits for 5 seconds
    await msg.delete() # deletes message sent by bot, aka 'done'

두 번째 방법은 delete_after봇의 메시지 만 삭제한다고 가정 하여 사용하는 것 입니다. 문서에 대한 직접 링크를 얻을 수 없었지만 다음과 같이 말합니다.

delete_after (float) – 제공된 경우 방금 보낸 메시지를 삭제하기 전에 백그라운드에서 대기하는 시간 (초)입니다. 삭제에 실패하면 자동으로 무시됩니다.

예는 다음과 같습니다.

@client.command()
async def test2(ctx):
    await ctx.send("done", delete_after=5)

다른 추천 링크 :

  • SO : 5 초 후에 봇이 자신의 메시지를 삭제하도록하는 방법
  • SO : Discord.py 특정 시간이 지난 후 봇이 메시지를 삭제하도록하려면 어떻게해야합니까?
  • SO : discord.py에서 봇의 메시지 삭제

참고 :이 두 명령은 모두 테스트되었으며 두 명령 모두 예상대로 작동합니다.