switch inactive_channels.py settings to be days, defaulting to None (no prune)

This commit is contained in:
jesopo 2020-03-08 21:38:13 +00:00
parent f571bebeee
commit fc0b5eb46e

View file

@ -3,19 +3,21 @@ from src import ModuleManager, utils
PRUNE_TIMEDELTA = datetime.timedelta(weeks=4) PRUNE_TIMEDELTA = datetime.timedelta(weeks=4)
SETTING_NAME = "inactive-channels" SETTING_NAME = "inactive-prune"
SETTING = utils.BoolSetting(SETTING_NAME, SETTING = utils.IntRangeSetting(0, None, SETTING_NAME,
"Whether or not to leave inactive channels after 4 weeks") "Amount of days of inactivity before we leave a channel")
MODE_SETTING_NAME = "inactive-channel-modes" MODE_SETTING_NAME = "inactive-prune-modes"
MODE_SETTING = utils.BoolSetting(MODE_SETTING_NAME, MODE_SETTING = utils.BoolSetting(MODE_SETTING_NAME,
"Whether or not we will leave inactive channels that we have a mode in") "Whether or not we will leave inactive channels that we have a mode in")
@utils.export("botset", SETTING) @utils.export("botset", SETTING)
@utils.export("serverset", SETTING) @utils.export("serverset", SETTING)
@utils.export("channelset", SETTING)
@utils.export("serverset", MODE_SETTING) @utils.export("serverset", MODE_SETTING)
@utils.export("channelset", MODE_SETTING) @utils.export("channelset", MODE_SETTING)
@utils.export("channelset", utils.BoolSetting(SETTING_NAME,
"Whether or not to leave this channel when it is inactive"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _get_timestamp(self, channel): def _get_timestamp(self, channel):
return channel.get_setting("last-message", None) return channel.get_setting("last-message", None)
@ -33,31 +35,30 @@ class Module(ModuleManager.BaseModule):
@utils.hook("cron") @utils.hook("cron")
@utils.kwarg("schedule", "0") @utils.kwarg("schedule", "0")
def hourly(self, event): def hourly(self, event):
print("minutely")
parts = [] parts = []
now = utils.datetime.utcnow() now = utils.datetime.utcnow()
botwide_setting = self.bot.get_setting(SETTING_NAME, False) botwide_days = self.bot.get_setting(SETTING_NAME, None)
botwide_mode_setting = self.bot.get_setting(MODE_SETTING_NAME, False) botwide_mode_setting = self.bot.get_setting(MODE_SETTING_NAME, False)
for server in self.bot.servers.values(): for server in self.bot.servers.values():
serverwide_setting = server.get_setting( serverwide_days = server.get_setting(SETTING_NAME, botwide_days)
SETTING_NAME, botwide_setting) if serverwide_days == None:
if not serverwide_setting:
continue continue
mode_setting = server.get_setting( mode_setting = server.get_setting(
MODE_SETTING_NAME, botwide_mode_setting) MODE_SETTING_NAME, botwide_mode_setting)
our_user = server.get_user(server.nickname) our_user = server.get_user(server.nickname)
for channel in server.channels: for channel in server.channels:
if not channel.get_setting(SETTING_NAME, serverwide_setting): if (not channel.get_setting(SETTING_NAME, True) or
continue not mode_setting and channel.get_user_modes(our_user)):
if not mode_setting and channel.get_user_modes(our_user):
continue continue
timestamp = self._get_timestamp(channel) timestamp = self._get_timestamp(channel)
if timestamp: if timestamp:
dt = utils.datetime.parse.iso8601(timestamp) dt = utils.datetime.parse.iso8601(timestamp)
if (now-dt) >= PRUNE_TIMEDELTA: if (now-dt).days >= serverwide_days:
parts.append([server, channel]) parts.append([server, channel])
for server, channel in parts: for server, channel in parts: