Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance] Add --end-after-restart option #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions maintenance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
async def setup(bot):
cog = Maintenance(bot)
await bot.add_cog(cog)
await cog.initialize()
4 changes: 3 additions & 1 deletion maintenance/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def __init__(self, start=0, end=0, after=True, whitelist=None):
if not whitelist:
whitelist = []
self.start = start + time.time()
if end:
if end is True:
self.end = True
elif end:
if after:
self.end = end + self.start
else:
Expand Down
3 changes: 3 additions & 0 deletions maintenance/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async def convert(self, ctx, argument):
_end = parser.add_mutually_exclusive_group()
_end.add_argument("--end-after", nargs="*", dest="end", default=[])
_end.add_argument("--end-in", nargs="*", dest="endin", default=[])
_end.add_argument("--end-after-restart", dest="end_after_restart", action="store_true")
try:
vals = vars(parser.parse_args(argument.split(" ")))
except Exception as exc:
Expand All @@ -35,6 +36,8 @@ async def convert(self, ctx, argument):
if end_seconds == None:
end_seconds = convert_time(vals.get("endin", None))
after = False
if vals.get("end_after_restart", False):
end_seconds = True
if start_seconds:
if end_seconds:
scheduled = ScheduledMaintenance(
Expand Down
35 changes: 25 additions & 10 deletions maintenance/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,21 @@ def __init__(self, bot):
"delete": 3,
"scheduledmaintenance": [],
}
# When on maintenance, on will be set to [True, second of when it is off maintenance, list of people who can bypass the maintenance]
# When on maintenance, on will be set to [True, second of when it is off maintenance or True if it's planned to end after bot restart, list of people who can bypass the maintenance]
self.conf.register_global(**default_global)
self.bot.add_check(self.this_check)
self.task = self.bot.loop.create_task(self.bg_loop())
self.task = None

async def cog_load(self):
# disable maintenance scheduled to end after restart,
# if the cog was loaded during bot startup
if not self.bot.is_ready():
on = await self.conf.on()
if on[0] and on[1] is True:
await self.conf.on.set([False, 0, []])

async def initialize(self):
self.task = asyncio.create_task(self.bg_loop())

@listener()
async def on_command_error(self, ctx, error):
Expand All @@ -73,11 +84,9 @@ async def on_command_error(self, ctx, error):
await ctx.send(error)

def cog_unload(self):
self.__unload()

def __unload(self):
self.bot.remove_check(self.this_check)
self.task.cancel()
if self.task is not None:
self.task.cancel()

async def red_delete_data_for_user(
self,
Expand Down Expand Up @@ -121,7 +130,10 @@ async def this_check(self, ctx):
on = await self.conf.on()
if not on[0]:
return True
if on[1] <= time.time() and on[1] != 0:
if on[1] is True:
# maintenance will be disabled after restart
pass
elif on[1] != 0 and on[1] <= time.time():
setting = [False, 0, []]
await self.conf.on.set(setting)
return True
Expand All @@ -145,6 +157,7 @@ async def _on(self, ctx, *, args: Margs = None):
--start-in: Makes the maintenace start in that long.
--end-in: Schedules the maintenance to end in that long from the current second.
--end-after: Schedules the maintenance to end in that long after the maitenance has started.
--end-after-restart: Schedules the maintenance to end after bot restart.
--whitelist: Provide user IDs after this to whitelist people from the maintenance.

Examples:
Expand All @@ -165,8 +178,8 @@ async def _on(self, ctx, *, args: Margs = None):
else:
num = 0
whitelist = []
start = time.time()
if start == time.time():
start = None
if start is None:
setting = [True, num, whitelist]
await self.conf.on.set(setting)
else:
Expand Down Expand Up @@ -195,7 +208,9 @@ async def settings(self, ctx):
sending += " • " + starting
sending += "```"
return await ctx.send(sending)
if on[1] != 0:
if on[1] is True:
done = "after bot restart"
elif on[1] != 0:
done = str(datetime.fromtimestamp(on[1]).strftime("%A, %B %d, %Y %I:%M:%S"))
done = "on " + done
else:
Expand Down