Browse Source

Add breadcount

pull/2/head
lucidiot 11 months ago
parent
commit
789deaeffa
  1. 2
      .gitignore
  2. 31
      breadbot/__main__.py
  3. 8
      breadbot/data/bread_messages.txt
  4. 70
      breadbot/plugins/bread.py

2
.gitignore

@ -61,3 +61,5 @@ tags
[._]*.un~
.vscode/*
breadbot/data/breadcount.txt

31
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()

8
breadbot/data/bread_messages.txt

@ -0,0 +1,8 @@
bread
mmhhhh, bread
bread!!!
bread
🅱️ r e a d
baguette
bagel
waffle

70
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'
)
Loading…
Cancel
Save