|
| 1 | +import datetime |
| 2 | +from src import ModuleManager, utils |
| 3 | + |
| 4 | +PRUNE_TIMEDELTA = datetime.timedelta(weeks=2) |
| 5 | +SETTING = utils.BoolSetting("inactive-channels", |
| 6 | + "Whether or not to leave inactive channels after 2 weeks") |
| 7 | + |
| 8 | +@utils.export("botset", SETTING) |
| 9 | +@utils.export("serverset", SETTING) |
| 10 | +class Module(ModuleManager.BaseModule): |
| 11 | + def _get_timestamp(self, channel): |
| 12 | + return channel.get_setting("last-message", None) |
| 13 | + def _set_timestamp(self, channel): |
| 14 | + channel.set_setting("last-message", |
| 15 | + utils.datetime.format.iso8601(utils.datetime.utcnow())) |
| 16 | + def _del_timestamp(self, channel): |
| 17 | + channel.del_setting("last-message") |
| 18 | + |
| 19 | + @utils.hook("new.channel") |
| 20 | + def new_channel(self, event): |
| 21 | + if self._get_timestamp(event["channel"]) == None: |
| 22 | + self._set_timestamp(event["channel"]) |
| 23 | + |
| 24 | + @utils.hook("cron") |
| 25 | + @utils.kwarg("schedule", "0") |
| 26 | + def hourly(self, event): |
| 27 | + parts = [] |
| 28 | + now = utils.datetime.utcnow() |
| 29 | + botwide_setting = self.bot.get_setting("inactive-channels", False) |
| 30 | + |
| 31 | + for server in self.bot.servers.values(): |
| 32 | + if not server.get_setting("inactive-channels", botwide_setting): |
| 33 | + continue |
| 34 | + |
| 35 | + for channel in server.channels: |
| 36 | + timestamp = self._get_timestamp(channel) |
| 37 | + if timestamp: |
| 38 | + dt = utils.datetime.parse.iso8601(timestamp) |
| 39 | + if (now-dt) >= PRUNE_TIMEDELTA: |
| 40 | + parts.append([server, channel]) |
| 41 | + |
| 42 | + for server, channel in parts: |
| 43 | + self.log.warn("Leaving %s:%s due to channel inactivity", |
| 44 | + [str(server), str(channel)]) |
| 45 | + channel.send_part("Channel inactive") |
| 46 | + self._del_timestamp(channel) |
| 47 | + |
| 48 | + @utils.hook("received.message.channel") |
| 49 | + def channel_message(self, event): |
| 50 | + self._set_timestamp(event["channel"]) |
0 commit comments