我有以下代码:
import asyncio
import threading
import time
import discord
from discord.ext import commands
from .utils import error
class threaded:
def __init__(self, channel, bot):
self.channel = bot.get_channel(channel)
self.bot = bot
def doSomething(self):
print("abc")
async def IAmThreaded(self):
time.sleep(30)
embed = discord.Embed(title=f"Finished doing something",
description=f"abc",
color=0x000000)
# this is where I get the error
await self.channel.send(embed=embed)
time.sleep(30)
Busy.busy = False
def threadMe(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.IAmThreaded())
loop.close()
class Busy:
busy = False
@commands.command()
async def something(ctx):
if not Busy.busy:
threadMeClass = threaded(ctx.channel.id, ctx.bot)
embed = discord.Embed(title=f"Starting doing something",
description=f"abc", color=0x000000)
await ctx.channel.send(embed=embed)
threadMeClass.doSomething()
Busy.bosy= True
threading.Thread(target=threadMeClass.threadMe).start()
else:
await error(ctx, "There is already something running, please wait for it to finish")
当它运行时,线程会一直工作,直到它完成并尝试发送消息,当它发送时,我得到: RuntimeError: Timeout context manager should be used inside a task. 我尝试将 ctx 直接传递给类并通过保存 id 来获取频道,但它们都不起作用,有什么提示吗?