Python Discord.py:如何从线程函数发送消息?
我有以下代码:Python Discord.py:如何从线程函数发送消息?,python,discord.py,Python,Discord.py,我有以下代码: 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 = b
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上下文管理器应该在任务中使用。
我试着直接将ctx传递给类,并通过保存id来获取通道,但都不起作用,有什么提示吗