Ready for release?
This commit is contained in:
parent
e1d314cc83
commit
607b7b6718
54
fifo/fifo.py
54
fifo/fifo.py
@ -93,6 +93,13 @@ class FIFO(commands.Cog):
|
|||||||
|
|
||||||
return new_ctx.valid
|
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):
|
async def _process_task(self, task: Task):
|
||||||
job: Union[Job, None] = await self._get_job(task)
|
job: Union[Job, None] = await self._get_job(task)
|
||||||
if job is not None:
|
if job is not None:
|
||||||
@ -146,9 +153,14 @@ class FIFO(commands.Cog):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@fifo.command(name="set")
|
@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)
|
task = Task(task_name, ctx.guild.id, self.config, bot=self.bot)
|
||||||
await task.load_from_config()
|
await task.load_from_config()
|
||||||
@ -327,9 +339,39 @@ class FIFO(commands.Cog):
|
|||||||
"""
|
"""
|
||||||
Deletes a task from this guild's task list
|
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()
|
||||||
|
|
||||||
@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 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()
|
||||||
|
|
||||||
|
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):
|
async def fifo_trigger(self, ctx: commands.Context):
|
||||||
"""
|
"""
|
||||||
Add a new trigger for a task from the current guild.
|
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
|
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)
|
task = Task(task_name, ctx.guild.id, self.config)
|
||||||
await task.load_from_config()
|
await task.load_from_config()
|
||||||
|
@ -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
|
|
20
fifo/task.py
20
fifo/task.py
@ -117,7 +117,9 @@ class Task:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if t["type"] == "cron": # TODO: Implement this, should be easy
|
if t["type"] == "cron": # TODO: Implement this, should be easy
|
||||||
raise NotImplemented
|
triggers.append(t) # already a string, nothing to do
|
||||||
|
|
||||||
|
continue
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
return triggers
|
return triggers
|
||||||
@ -137,7 +139,7 @@ class Task:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if t["type"] == "cron":
|
if t["type"] == "cron":
|
||||||
raise NotImplemented
|
continue # already a string
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
# async def load_from_data(self, data: Dict):
|
# 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: commands.Context = await self.bot.get_context(message)
|
||||||
new_ctx.assume_yes = True
|
new_ctx.assume_yes = True
|
||||||
if not new_ctx.valid:
|
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
|
return False
|
||||||
|
|
||||||
await self.bot.invoke(new_ctx)
|
await self.bot.invoke(new_ctx)
|
||||||
@ -284,7 +288,7 @@ class Task:
|
|||||||
async def set_channel(self, channel: Union[discord.TextChannel, str]):
|
async def set_channel(self, channel: Union[discord.TextChannel, str]):
|
||||||
self.channel_id = getattr(channel, "id", None) or channel
|
self.channel_id = getattr(channel, "id", None) or channel
|
||||||
await self.config.guild_from_id(self.guild_id).tasks.set_raw(
|
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):
|
def get_command_str(self):
|
||||||
@ -323,3 +327,11 @@ class Task:
|
|||||||
"config": self.config,
|
"config": self.config,
|
||||||
"bot": self.bot,
|
"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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user