【发布时间】:2021-05-12 17:34:16
【问题描述】:
我正在开发一个 Discord 机器人来抓取 reddit 上的帖子。现在我创建了一个命令,以便我可以使用@tasks.loop(time=y) 每隔 y 时间获取 x 数量的帖子。在 cog 中构建命令,将其加载到机器人中,然后按预期工作。
现在我的问题是,据我了解,该命令只能同时运行一个实例。即,如果我在服务器中运行该命令,它将正常工作,但在我 .cancel() 正在运行的实例之前,我将无法将该命令运行到另一台服务器中。
我想知道是否有某种方法可以运行多个实例。就像一种命名循环或其他东西的方法,或者我是否应该使用 Discord.py 的其他功能来实现我想要做的事情。
编辑: 为了简单起见,我在代码中省略了一些参数,但这里是有问题的代码
def launch_client():
bot = Bot()
reddit = client.Client()
bot.add_cog(ScheduleCog(bot, reddit))
class ScheduleCog(commands.Cog):
time_unit = {'seconds': 1, 'minutes': 60, 'hours': 3600, 'days': 86400, 'weeks': 604800}
def __init__(self, bot, reddit):
self.bot = bot
self.reddit = reddit
def cog_unload(self):
self.schedule_loop.cancel()
@tasks.loop(seconds=1)
async def schedule_loop(self, ctx, sub, sort, limit, time):
posts = await self.reddit.get_posts(sub, sort, limit, time)
if posts is not None:
for post in posts:
await ctx.channel.send(post.url)
else:
await ctx.channel.send('Error in your request. Please validate fields.')
@schedule_loop.before_loop
async def before_schedule_loop(self):
await self.bot.wait_until_ready()
@commands.command()
async def subschedule(self, ctx, interval: int, interval_unit: str, sub: str, sort: str, limit: int,
time: str = 'hot'):
unit: int = self.time_unit[interval_unit]
self.schedule_loop.change_interval(seconds=interval * unit)
self.schedule_loop.start(ctx, sub, sort, limit, time)
@commands.command()
async def subschedulestop(self, ctx):
self.schedule_loop.cancel()
所以我已经按照官方 discord.ext.tasks documentation 写了这个协议。
现在一切都按照循环的预期工作。我启动 subschedule() 命令,它每隔 'y' 单位时间的 'x' 间隔发送一次帖子。
现在正如原始问题中提到的那样,它在单个实例中工作,我不知道如何拥有多个实例。我希望能够在不同的公会中运行多个 subschedule() 实例,甚至在同一个公会中运行多个实例,但是当我再次调用该命令时它现在可以正常工作,我收到运行时错误“任务已启动且未完成” .
我只是想知道能够并行运行多个任务的正确方法。
【问题讨论】:
标签: python command discord.py