多个用户的多线程Discord bot

发布于 2025-01-30 09:36:40 字数 853 浏览 4 评论 0原文

我有一个需要帮助的问题。我创建了一个机器人,该机器人从Discord用户中获取输入并运行它以分析一些计算。这些计算通常需要40-50秒,并作为不和谐的答复返回结果。到目前为止,一切都很好。

但是,当其他用户或同一用户在进行中的其他单词(计算)时,在不和谐中键入另一个单词时,直到上一个完成后,新的单词才开始计算。我希望在上一个正在进行的过程中开始新的。我该如何编辑以使其正确?照亮我。谢谢。

import discord
from threading import Thread
import asyncio

client = discord.Client()

async def init():
   loop = asyncio.get_event_loop()
   loop.create_task(client.start('TOKEN'))
   await Thread(target=loop.run_forever).start()

@client.event
async def on_ready():
   print("we've logged in as {0.user}".format(client))  

@client.event
async def on_message(message):
   if message.author == client.user:
       return
   if message.content.startswith('!'):
       try:
           #calculation codes
           await message.reply("calculation results")
       except:
           await message.reply("bla bla bla")

I have a issue that I need help. I created a bot that takes input from Discord Users and runs it to analyze for some calculations. These calculations takes 40-50 seconds usually and return the result as a reply in Discord. So far everything is fine.

But when other users or same user types another words in Discord while the previous one is in progress (calculation), the new one doesn't start to get calculated until the previous one gets done. I want the new one starts while the previous one is in progress. How can I edit this to make it right? Illuminate me. Thanks.

import discord
from threading import Thread
import asyncio

client = discord.Client()

async def init():
   loop = asyncio.get_event_loop()
   loop.create_task(client.start('TOKEN'))
   await Thread(target=loop.run_forever).start()

@client.event
async def on_ready():
   print("we've logged in as {0.user}".format(client))  

@client.event
async def on_message(message):
   if message.author == client.user:
       return
   if message.content.startswith('!'):
       try:
           #calculation codes
           await message.reply("calculation results")
       except:
           await message.reply("bla bla bla")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文