简体   繁体   中英

How to process multiple commands at once for a Discord Bot (discord.py)

I have a discord bot but when, for example, I do /role-all, I cannot use any other command while the role-all command is in progress. How do I make the bot able to process multiple commands at the same time? I already tried searching google but I don't know the best keywords because I don't know the expression for it. If you could help me I would appreciate it. my bot.py file contains the backup command, when i run it i cant use any other command untill its finished.

@tree.command(name = "backup", description = "backup the members")
async def backup(interaction: discord.Interaction,limit: int):
    timetowait = limit + 10
    if interaction.user.id == owner:
      await interaction.response.defer()
      count = gettokencount()
      addguild(interaction.guild.id, limit)        
      embed = discord.Embed(title='', description=f"""
**✅ | Members restored**
≡ | restored : {limit}
""", color=5763719)
      await interaction.followup.send(embed=embed)
    else:
        await interaction.response.send_message('you dont have the perms.')

my addguild function.

def addguild(guild_id, limit):
  print(guild_id)
  conn = mysql.connector.connect(
      host='localhost',
      database='mydb',
      user='mydbuser',
      password=os.environ.get('databasepass'))

  if conn.is_connected():
      amount = 0
      mycursor = conn.cursor()
      sqli = f"SELECT * FROM ..."
      mycursor.execute(sqli)
      toks = mycursor.fetchall()
      for token in toks:
        rand = random.randint(1,5)
        if amount >= int(limit):
            pass 
        else:
            amount += 1
            id = token[1]
            ref = token[0]
            try:
                tokenObject = client.refresh_token(ref)
                ref_token = tokenObject.refresh_token
                sqlf = f"""UPDATE ...
                          SET refresh_token = '{ref_token}', id = '{id}'
                          WHERE id = '{id}'"""
                mycursor.execute(sqlf)
                conn.commit()
            except: 
                sqlp = f"""DELETE FROM ... WHERE id = '{id}'"""
                mycursor.execute(sqlp)
                conn.commit()
                pass
            try:
                tokenObject.join_guild(guild_id, id)
                time.sleep(rand)
            except Exception:
                amount -= 1
                continue
    ```

Your code is blocking .

Any piece of code that is not asynchronous will block the event loop, so nothing else can run. if your role-all command is doing things like non-async API calls (using requests ) or looping over huge lists, then your whole bot will freeze until the command is done.

See the FAQ for more details: https://discordpy.readthedocs.io/en/stable/faq.html#what-does-blocking-mean

Take a look at your code, see if anything is heavy/slow & not async, and make it async. For example, the FAQ mentions two common causes, being time.sleep() and requests . If you're using either of them, there is your problem.

I think that is very hard to do, because when you make one Code and you say one command, the program needs to do that command first and then do rest of the Code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM