Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

python - Client Event in command discord.py

I'm currently making a discord bot with tutorials and I want that if the member types "next", the next step sends.

Code:

async def pytutorial(ctx):
    embed=discord.Embed(title="Help for Wolf-Hosting Bot", description="panel.wolf-hosting.nl", color=0x0400ff)
    embed.set_thumbnail(url="https://cdn.discordapp.com/icons/807978904727191572/0704feeefd7a91c1abb2c37f9c9cc4f4.webp?size=256")
    embed.add_field(name="Welcome to the Wolf Hosting python tutorial!", value="We are first going to start with chosing an editor. I prefer using VSCode.", inline=True)
    embed.add_field(name='Type "next" for the next step.', value="Don't include the """, inline=True)
    embed.add_field(name="Download VSCode:", value="https://code.visualstudio.com/download", inline=False)
    await ctx.send(embed=embed)
    @client.event
    async def on_message(message):
        if "next" in message.content:
            embed=discord.Embed(title="Help for Wolf-Hosting Bot", description="panel.wolf-hosting.nl", color=0x0400ff)
            embed.set_thumbnail(url="https://cdn.discordapp.com/icons/807978904727191572/0704feeefd7a91c1abb2c37f9c9cc4f4.webp?size=256")
            embed.add_field(name="Installing discord.py", value='First click on Terminal in VSCode. Type "py -3 -m pip install -U discord.py".', inline=True)
            embed.add_field(name='Type "next" for the next step.', value="Don't include the """, inline=True)
            embed.add_field(name="More advanced explaining:", value="https://discordpy.readthedocs.io/en/latest/intro.html#installing", inline=False)
            await ctx.send(embed=embed)

Problem: The on_message event keeps running and running and people can't run other commands anymore. Question: How to make an event inside a command (or a better way to code this xd)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Waiting for messages or any discord.py event can be done with bot.wait_for

@bot.command() #assuming bot since you haven't specified
async def command(ctx):
   #show help or some embed here
   try: 
      msg = await bot.wait_for('message', check = lambda x: 'next' in x.content and x.author == ctx.author, timeout = 30) #use self.bot.wait_for in cogs
   except asyncio.TimeoutError:
      await ctx.send('You took too long to respond')
   finally:
      #do other stuff here
      await msg.channel.send('hi')

Using bot.wait_for:

  1. The first parameter is the event you are waiting for, all such events are passed
  2. The check for the event, the check function takes in the same parameters as the on_event function. Ex on_message takes message as a parameter. The check function should return a bool. Our check function returns true if the message contains next and the author is the same as the command author.
  3. The timeout parameter tells the bot how long to wait, if the timeout is exceeded, asyncio.TimeoutError is raised, we catch that with a try and except.

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...