discord.py를 사용하여 discord 봇이 자체 메시지에 대한 명령을 트리거하도록하려면 어떻게해야합니까?

동기화 271

사용자가 잘못된 명령을 입력했다고 가정 해 봅시다. 봇이 (y / n)? y이면 봇이 제안 된 명령을 트리거해야합니다. 나는 이것이 두 가지 방법으로 달성 될 수 있다고 생각합니다.

  1. 봇이 자체 메시지에서 명령을 트리거 할 수있는 경우
  2. 다른 톱니 바퀴에서 명령을 부를 수 있다면

둘 중 하나가 작동하지 않는 것 같습니다.

다음은 저를 더 잘 도울 수있는 예입니다.

아래 코드가 Joke.py라는 톱니 바퀴에서 가져온 것이라고 가정 해 보겠습니다.

@commands.command()
async def joke(self,ctx):
await ctx.send("A Joke")

그리고 data.json 파일에 저장된 사용자가 사용하는 잘못된 명령을 수정하는 또 다른 장부 "CommandsCorrection.py"가 있습니다.

@commands.Cog.listener()
async def on_message(self, message):
    channel = message.channel
    prefix = get_prefix(self,message)
    if message.author.id == bot.id:
        return
    elif message.content.startswith(prefix):
            withoutprefix = message.content.replace(prefix,"")
            if withoutprefix in data:
                return
            else:
                try:
                    rightCommand= get_close_matches(withoutprefix, data.keys())[0]
                    await message.channel.send(f"Did you mean {prefix}%s instead? Enter Y if yes, or N if no:" %rightCommand)
                    def check(m):
                        return m.content == "Y" or "N" and m.channel == channel
                    msg = await self.client.wait_for('message', check=check, timeout = 10.0)
                    msg.content = msg.content.lower()
                    if msg.content == "y":
                        await channel.send(f'{prefix}{rightCommand}')
                    elif msg.content == "n":
                        await channel.send('You said no.')
                except asyncio.TimeoutError:
                    await channel.send('Timed Out')
                except IndexError as error:
                    await channel.send("Command Not Found. Try !help for the list of commands and use '!' as prefix.")

위의 코드 await message.channel.send(f"Did you mean {prefix}%s instead? Enter Y if yes, or N if no:" %rightCommand)에서 올바른 명령을 제안하고 올바른 명령을 await channel.send(f'{prefix}{rightCommand}')보냅니다.

예를 들면 다음과 같습니다.

user : !jok
bot  : Did you mean !joke instead? Enter Y if yes, or N if no:
user : y
bot  : !joke **I want to trigger the command when it sends this message by reading its own message or my just calling that command/function

어떻게해야합니까?

패트릭 호

한 가지 해결책은 명령의 논리를 명령 콜백에서 분리하여 자체 코 루틴에 넣는 것입니다. 그런 다음 명령 콜백에서 이러한 코 루틴을 자유롭게 호출 할 수 있습니다.

따라서 다음과 같이 코드를 설정합니다.

@bot.command()
async def my_command(ctx):
    await ctx.send("Running command")

@bot.command()
async def other_command(ctx, arg):
    if arg == "command":
        await ctx.send("Running command")

다음과 같이 :

async def command_logic(ctx):
    await ctx.send("Running command")

@bot.command()
async def my_command(ctx):
    await command_logic(ctx)

@bot.command()
async def other_command(ctx, arg):
    if arg == "command":
        await command_logic(ctx)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관