From 607b7b67183bc12667d6f4db9b4e2b9ad298f49d Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 3 Sep 2020 10:04:10 -0400 Subject: [PATCH] Ready for release? --- fifo/fifo.py | 54 +++++++++++++++++++++++++++++++++++++++++++++----- fifo/redjob.py | 44 ---------------------------------------- fifo/task.py | 20 +++++++++++++++---- 3 files changed, 65 insertions(+), 53 deletions(-) delete mode 100644 fifo/redjob.py diff --git a/fifo/fifo.py b/fifo/fifo.py index 10b72af..d4156ea 100644 --- a/fifo/fifo.py +++ b/fifo/fifo.py @@ -93,6 +93,13 @@ class FIFO(commands.Cog): return new_ctx.valid + async def _delete_task(self, task: Task): + job: Union[Job, None] = await self._get_job(task) + if job is not None: + job.remove() + + await task.delete_self() + async def _process_task(self, task: Task): job: Union[Job, None] = await self._get_job(task) if job is not None: @@ -146,9 +153,14 @@ class FIFO(commands.Cog): pass @fifo.command(name="set") - async def fifo_setauthor(self, ctx: commands.Context, task_name: str, author_or_channel: Union[discord.Member, discord.TextChannel]): + async def fifo_set( + self, + ctx: commands.Context, + task_name: str, + author_or_channel: Union[discord.Member, discord.TextChannel], + ): """ - Sets the task to be executed as a different author or in a different channel. + Sets a different author or in a different channel for execution of a task. """ task = Task(task_name, ctx.guild.id, self.config, bot=self.bot) await task.load_from_config() @@ -327,9 +339,39 @@ class FIFO(commands.Cog): """ Deletes a task from this guild's task list """ - pass + task = Task(task_name, ctx.guild.id, self.config, bot=self.bot) + await task.load_from_config() + + if task.data is None: + await ctx.maybe_send_embed( + f"Task by the name of {task_name} is not found in this guild" + ) + return + + await self._delete_task(task) + await ctx.maybe_send_embed(f"Task[{task_name}] has been deleted from this guild") + + @fifo.command(name="cleartriggers", aliases=["cleartrigger"]) + async def fifo_cleartriggers(self, ctx: commands.Context, task_name: str): + """ + Removes all triggers from specified task + + Useful to start over with new trigger + """ + + task = Task(task_name, ctx.guild.id, self.config, bot=self.bot) + await task.load_from_config() - @fifo.group(name="trigger") + if task.data is None: + await ctx.maybe_send_embed( + f"Task by the name of {task_name} is not found in this guild" + ) + return + + await task.clear_triggers() + await ctx.tick() + + @fifo.group(name="addtrigger", aliases=["trigger"]) async def fifo_trigger(self, ctx: commands.Context): """ Add a new trigger for a task from the current guild. @@ -405,7 +447,9 @@ class FIFO(commands.Cog): self, ctx: commands.Context, task_name: str, *, cron_str: CronConverter ): """ - Add a "time of day" trigger to the specified task + Add a cron "time of day" trigger to the specified task + + See https://crontab.guru/ for help generating the cron_str """ task = Task(task_name, ctx.guild.id, self.config) await task.load_from_config() diff --git a/fifo/redjob.py b/fifo/redjob.py deleted file mode 100644 index c276aa4..0000000 --- a/fifo/redjob.py +++ /dev/null @@ -1,44 +0,0 @@ -import six -from apscheduler.job import Job -from apscheduler.schedulers.asyncio import AsyncIOScheduler -from apscheduler.schedulers.base import STATE_STOPPED -from apscheduler.util import undefined - - -class RedJob(Job): - pass - - -class RedAsyncIOScheduler(AsyncIOScheduler): - - def add_job(self, func, trigger=None, args=None, kwargs=None, id=None, name=None, - misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, - next_run_time=undefined, jobstore='default', executor='default', - replace_existing=False, **trigger_args): - job_kwargs = { - 'trigger': self._create_trigger(trigger, trigger_args), - 'executor': executor, - 'func': func, - 'args': tuple(args) if args is not None else (), - 'kwargs': dict(kwargs) if kwargs is not None else {}, - 'id': id, - 'name': name, - 'misfire_grace_time': misfire_grace_time, - 'coalesce': coalesce, - 'max_instances': max_instances, - 'next_run_time': next_run_time - } - job_kwargs = dict((key, value) for key, value in six.iteritems(job_kwargs) if - value is not undefined) - job = RedJob(self, **job_kwargs) - - # Don't really add jobs to job stores before the scheduler is up and running - with self._jobstores_lock: - if self.state == STATE_STOPPED: - self._pending_jobs.append((job, jobstore, replace_existing)) - self._logger.info('Adding job tentatively -- it will be properly scheduled when ' - 'the scheduler starts') - else: - self._real_add_job(job, jobstore, replace_existing) - - return job diff --git a/fifo/task.py b/fifo/task.py index 62d50ca..9ed4e12 100644 --- a/fifo/task.py +++ b/fifo/task.py @@ -117,7 +117,9 @@ class Task: continue if t["type"] == "cron": # TODO: Implement this, should be easy - raise NotImplemented + triggers.append(t) # already a string, nothing to do + + continue raise NotImplemented return triggers @@ -137,7 +139,7 @@ class Task: continue if t["type"] == "cron": - raise NotImplemented + continue # already a string raise NotImplemented # async def load_from_data(self, data: Dict): @@ -266,7 +268,9 @@ class Task: new_ctx: commands.Context = await self.bot.get_context(message) new_ctx.assume_yes = True if not new_ctx.valid: - log.warning(f"Could not execute task due invalid context: {new_ctx}") + log.warning( + f"Could not execute Task[{self.name}] due invalid context: {new_ctx.invoked_with}" + ) return False await self.bot.invoke(new_ctx) @@ -284,7 +288,7 @@ class Task: async def set_channel(self, channel: Union[discord.TextChannel, str]): self.channel_id = getattr(channel, "id", None) or channel await self.config.guild_from_id(self.guild_id).tasks.set_raw( - self.name, "channel_id", value=self.author_id + self.name, "channel_id", value=self.channel_id ) def get_command_str(self): @@ -323,3 +327,11 @@ class Task: "config": self.config, "bot": self.bot, } + + async def clear_triggers(self): + self.data["triggers"] = [] + await self.save_data() + + async def delete_self(self): + """Hopefully nothing uses the object after running this...""" + await self.config.guild_from_id(self.guild_id).tasks.clear_raw(self.name)