I am coding a command (/mc
, /
is the prefix) for my discord bot to get and message a result. But getting the result takes a lot of time. To prevent multiple request at a time, I placed the code of getting the results in a separate function mainmc()
(not async
) and call mainmc
as a thread by creating a new class mainThreadMC(threading.Thread)
to start the thread, and create a new class object and start it, so that when there's multiple request the command will run instantly without having to wait for the previous one. But quickly after I run it, I've found out that it's not possible as sending a message needs await
or else it won't work. But if you have await
in a function the function needs async
. That means I have to edit the module threading
to make it work? And surely this is not the way to do it. But what should I do?
Here's the code (simplified):
class mainThreadMC(threading.Thread):
def __init__(self, threadID, name, ctx,args):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.args = args
self.ctx = ctx
def run(self):
logger.info("Starting " + self.name)
mainmc(self.ctx,self.args)
logger.info(f'Exiting {self.name}')
def mainmc(ctx,args):
# fetching data... creating / editing variable embed
ctx.send(embed=embed) #ERROR!!!!!!!!!!!
# finalize...
return
@bot.command(name='mc',pass_content=True)
async def mc(ctx,*,args=""):
global runno
threadMainMC = mainThreadMC(runno,f"MainMC_{str(runno)}",ctx,args)
threadMainMC.start()
(I just want it to work, so if you have some alternative solutions you can also tell me.)
Thanks for helping.