aboutsummaryrefslogtreecommitdiff
path: root/modules/inactive_channels.py
diff options
context:
space:
mode:
authorGravatar jesopo2020-02-07 17:57:36 +0000
committerGravatar jesopo2020-02-07 17:57:36 +0000
commit1d51b43c22efdbf877497c8ec5382408b200dd73 (patch)
treeed58b40e0aecb17bea30e2d68d8e421b46d8adb5 /modules/inactive_channels.py
parentshow how many seconds by which you missed !bef/!bang (diff)
signature
add opt-in inactive channel pruning
Diffstat (limited to 'modules/inactive_channels.py')
-rw-r--r--modules/inactive_channels.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/inactive_channels.py b/modules/inactive_channels.py
new file mode 100644
index 00000000..fb28bda2
--- /dev/null
+++ b/modules/inactive_channels.py
@@ -0,0 +1,50 @@
+import datetime
+from src import ModuleManager, utils
+
+PRUNE_TIMEDELTA = datetime.timedelta(weeks=2)
+SETTING = utils.BoolSetting("inactive-channels",
+ "Whether or not to leave inactive channels after 2 weeks")
+
+@utils.export("botset", SETTING)
+@utils.export("serverset", SETTING)
+class Module(ModuleManager.BaseModule):
+ def _get_timestamp(self, channel):
+ return channel.get_setting("last-message", None)
+ def _set_timestamp(self, channel):
+ channel.set_setting("last-message",
+ utils.datetime.format.iso8601(utils.datetime.utcnow()))
+ def _del_timestamp(self, channel):
+ channel.del_setting("last-message")
+
+ @utils.hook("new.channel")
+ def new_channel(self, event):
+ if self._get_timestamp(event["channel"]) == None:
+ self._set_timestamp(event["channel"])
+
+ @utils.hook("cron")
+ @utils.kwarg("schedule", "0")
+ def hourly(self, event):
+ parts = []
+ now = utils.datetime.utcnow()
+ botwide_setting = self.bot.get_setting("inactive-channels", False)
+
+ for server in self.bot.servers.values():
+ if not server.get_setting("inactive-channels", botwide_setting):
+ continue
+
+ for channel in server.channels:
+ timestamp = self._get_timestamp(channel)
+ if timestamp:
+ dt = utils.datetime.parse.iso8601(timestamp)
+ if (now-dt) >= PRUNE_TIMEDELTA:
+ parts.append([server, channel])
+
+ for server, channel in parts:
+ self.log.warn("Leaving %s:%s due to channel inactivity",
+ [str(server), str(channel)])
+ channel.send_part("Channel inactive")
+ self._del_timestamp(channel)
+
+ @utils.hook("received.message.channel")
+ def channel_message(self, event):
+ self._set_timestamp(event["channel"])