<commit_before>#!/usr/bin/env python3

###############################################################################
# Module Imports
###############################################################################

import arrow
import peewee
import sopel
import re

from pyscp_bot import snark

###############################################################################

db = peewee.SqliteDatabase('notes.db')


class BaseModel(peewee.Model):

    class Meta:
        database = db


class Tell(BaseModel):
    sender = peewee.CharField()
    recipient = peewee.CharField(index=True)
    text = peewee.TextField()
    time = peewee.DateTimeField()


###############################################################################


def setup(bot):
    db.connect()
    Tell.create_table(True)
    bot._tells_recipients = {
        i.recipient for i in
        Tell.select(Tell.recipient).distinct(Tell.recipient)}


@sopel.module.commands('tell')
def tell(bot, trigger):
    name, text = trigger.group(2).split(maxsplit=1)
    now = arrow.now().format('YYYY-MM-DD hh:mm:ss')
    Tell.create(sender=trigger.nick, recipient=name, text=text, time=now)
    bot._tells_recipients.add(name)
    bot.say(snark.tell_stored(trigger.nick))


@sopel.module.rule('.*')
def user_active(bot, trigger):
    if re.match(r'[!\.](st|showt|showtells)', trigger.group(0)):
        return
    if trigger.nick in bot._tells_recipients:
        deliver_tells(bot, trigger.nick)


@sopel.module.commands('st', 'showt', 'showtells')
def showtells(bot, trigger):
    if trigger.nick in bot._tells_recipients:
        deliver_tells(bot, trigger.nick)
    else:
        bot.notice(snark.no_tells(trigger.nick), trigger.nick)


def deliver_tells(bot, name):
    tells = Tell.select().where(Tell.recipient == name)
    for tell in tells:
        time_passed = arrow.get(tell.time).humanize()
        msg = '{} said {}: {}'.format(tell.sender, time_passed, tell.text)
        bot.notice(msg, name)
    Tell.delete().where(Tell.recipient == name).execute()
    bot._tells_recipients -= {name}<commit_msg>fix tells<commit_after>#!/usr/bin/env python3

###############################################################################
# Module Imports
###############################################################################

import arrow
import peewee
import sopel
import re
import collections

import pyscp_bot.jarvis as vocab

###############################################################################

db = peewee.SqliteDatabase('notes.db')


class BaseModel(peewee.Model):

    class Meta:
        database = db


class Tell(BaseModel):
    sender = peewee.CharField()
    recipient = peewee.CharField(index=True)
    message = peewee.TextField()
    time = peewee.DateTimeField()


###############################################################################

MemTell = collections.namedtuple('MemTell', 'sender recipient message time')


def setup(bot):
    db.connect()
    Tell.create_table(True)
    bot.memory['tells'] = collections.defaultdict(list)
    for tell in Tell.select():
        memtell = MemTell(tell.sender, tell.recipient, tell.message, tell.time)
        bot.memory['tells'][tell.recipient].append(memtell)


@sopel.module.commands('tell')
def tell(bot, trigger):
    name, text = trigger.group(2).split(maxsplit=1)
    name = name.strip().lower()
    now = arrow.utcnow().format('YYYY-MM-DD hh:mm:ss')
    tell = MemTell(str(trigger.nick), name, text, now)
    Tell.create(**tell._asdict())
    bot.memory['tells'][name].append(tell)
    bot.say(vocab.tell_stored(trigger.nick))


@sopel.module.rule('.*')
def user_active(bot, trigger):
    if re.match(r'[!\.](st|showt|showtells)', trigger.group(0)):
        return  # avoid double dipping
    if trigger.nick.strip().lower() in bot.memory['tells']:
        deliver_tells(bot, trigger.nick)


@sopel.module.commands('st', 'showt', 'showtells')
def showtells(bot, trigger):
    print(bot.memory['tells'])
    print(trigger.nick.lower())
    if trigger.nick.lower() in bot.memory['tells']:
        deliver_tells(bot, trigger.nick)
    else:
        bot.notice(vocab.no_tells(trigger.nick), trigger.nick)


def deliver_tells(bot, name):
    for tell in bot.memory['tells'][name.lower()]:
        time_passed = arrow.get(tell.time).humanize()
        msg = '{} said {}: {}'.format(tell.sender, time_passed, tell.message)
        bot.notice(msg, name)
    del bot.memory['tells'][name.lower()]
    Tell.delete().where(Tell.recipient == name.lower()).execute()
{'fraction_non_alphanumeric': 0.27158034528552455, 'fraction_numerical': 0.0017706949977866313, 'mean_word_length': 4.431490384615385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2330243', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1402, 'n_words': 244}