From 789deaeffabc587b73627213c58ddd433367a177 Mon Sep 17 00:00:00 2001 From: lucidiot Date: Sat, 5 Dec 2020 02:21:56 +0100 Subject: [PATCH] Add breadcount --- .gitignore | 2 + breadbot/__main__.py | 31 ++++++++++++++ breadbot/data/bread_messages.txt | 8 ++++ breadbot/plugins/bread.py | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 breadbot/__main__.py create mode 100644 breadbot/data/bread_messages.txt create mode 100644 breadbot/plugins/bread.py diff --git a/.gitignore b/.gitignore index 4eac4aa..ca0f953 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,5 @@ tags [._]*.un~ .vscode/* + +breadbot/data/breadcount.txt diff --git a/breadbot/__main__.py b/breadbot/__main__.py new file mode 100644 index 0000000..b6cd754 --- /dev/null +++ b/breadbot/__main__.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +import argparse +import yaml +from pinhook import plugin, bot + + +class BreadBot(bot.Bot): + + def on_welcome(self, c, e): + super().on_welcome(c, e) + for channel in self.chanlist: + self.process_output(c, channel, plugin.message('bread')) + + +def main(): + parser = argparse.ArgumentParser( + description='Breadpunk Bread Bot', + ) + parser.add_argument( + 'config', + type=argparse.FileType('r'), + help='Configuration file', + ) + args = parser.parse_args() + config = yaml.safe_load(args.config) + bot = BreadBot(**config) + bot.start() + + +if __name__ == '__main__': + main() diff --git a/breadbot/data/bread_messages.txt b/breadbot/data/bread_messages.txt new file mode 100644 index 0000000..d170414 --- /dev/null +++ b/breadbot/data/bread_messages.txt @@ -0,0 +1,8 @@ +bread +mmhhhh, bread +bread!!! +bread +🅱️ r e a d +baguette +bagel +waffle diff --git a/breadbot/plugins/bread.py b/breadbot/plugins/bread.py new file mode 100644 index 0000000..773fc20 --- /dev/null +++ b/breadbot/plugins/bread.py @@ -0,0 +1,70 @@ +from pinhook import plugin +from pathlib import Path +import logging +import re +import random + +logger = logging.getLogger(__name__) +data_path = Path(__file__).absolute().parent.parent / 'data' +count_file = data_path / 'breadcount.txt' +messages_file = data_path / 'bread_messages.txt' +word_regex = re.compile(r'\W') + +try: + bread_messages = messages_file.read_text().strip().splitlines() + assert bread_messages +except (IOError, TypeError, ValueError, AssertionError): + if not messages_file.exists(): + messages_file.write_text('bread\n') + logger.warning(f'Bread messages file at {messages_file} not found ' + 'or corrupted, using ["bread"]') + bread_messages = ['bread'] + + +def increment_bread(): + try: + count = int(count_file.read_text().strip()) + except (IOError, TypeError, ValueError): + logger.warning(f'Breadcount file at {count_file} not found ' + 'or corrupted, restarting from zero') + count = 0 + count += 1 + count_file.write_text(str(count)) + + +@plugin.listener('bread') +def bread(ctx): + words = set(word_regex.split(ctx.text.lower())) + if 'bread' in words: + increment_bread() + if words == {'bread'}: + return plugin.message(random.choice(bread_messages)) + + +@plugin.command( + '!nukethebread', + help_text='Reset the breadcount.', + ops=True, + ops_msg='This command is restricted to breadpunk admins.' +) +def nukethebread(ctx): + count_file.write_text('0') + return plugin.message('Bread count has been reset.') + + +@plugin.command( + '!breadcount', + help_text='How many times did bakers say bread here?', +) +def breadcount(ctx): + try: + count = int(count_file.read_text().strip()) + except (IOError, TypeError, ValueError): + return plugin.message( + 'something is wrong with the breadcount! ' + 'quick, scream at lucitoast' + ) + else: + return plugin.message( + f'{count} bread{"s" if count != 1 else ""} so far' + )