From 3658f690c04b2a9740692f67d3ef549a1b056f9f Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Wed, 30 Aug 2017 13:45:07 +0100 Subject: [PATCH] Plugin to heckle people for starting typing. --- plugin/typing/typing.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 plugin/typing/typing.py diff --git a/plugin/typing/typing.py b/plugin/typing/typing.py new file mode 100644 index 0000000..2a965a3 --- /dev/null +++ b/plugin/typing/typing.py @@ -0,0 +1,66 @@ +import asyncio +import datetime + +from plugins import PluginCommand +import bot + +class Typing(object): + def __init__(self): + # How many times should the user send typing? + self.threshold = 4 + # In how long? + self.ttl = datetime.timedelta(0, 120) + self.probability = 0.1 + self.cache = {} + self.cleaned_up = datetime.datetime.utcnow() + + def description(self): + return 'User is typing...' + + def key(self, user, channel): + return '@'.join([user.id, channel.id]) + + def delete(self, key): + try: + log.debug('Forgetting {}'.format(key)) + del(self.cache[key]) + except KeyError: + pass + + def cleanup(self): + now = datetime.datetime.utcnow() + if now - self.cleaned_up > self.ttl: + for key in self.cache.keys(): + times = self.times(key, now) + if not times: + self.delete(key) + self.cleaned_up = now + + def times(self, key, now = None): + if now is None: + now = datetime.datetime.utcnow() + return [time for time in self.cache.get(key, []) if now - time <= self.ttl] + + async def message_received(self, message): + self.delete(self.key(message.author, message.channel)) + self.cleanup() + + async def non_command_message(self, message): + await self.message_received(message) + return PluginCommand.handled + + async def not_our_message(self, message): + await self.message_received(message) + return PluginCommand.handled + + async def on_typing(self, channel, user, when): + key = self.key(user, channel) + times = self.times(key) + times.append(when) + if len(times) > self.threshold: + await bot.maybe_say(channel, '**{}** is typing ...'.format(user.nick if user.nick is not None else user.name), probability = self.probability) + self.cleanup() + else: + log.debug('Remembering {}'.format(key)) + self.cache[key] = times + return PluginCommand.handled -- 2.7.4