Make breadcount file path configurable

This commit is contained in:
lucidiot 2023-02-23 21:09:34 +01:00
parent 190b2ee7ec
commit cd8b767ca8
3 changed files with 22 additions and 11 deletions

View File

@ -1,6 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import re import re
from pathlib import Path
from typing import Any
import yaml import yaml
from irc.client import Event, NickMask, ServerConnection # type: ignore from irc.client import Event, NickMask, ServerConnection # type: ignore
@ -11,6 +13,15 @@ RELAY_REGEX = re.compile(r'^<([^@]+@(?:tilde|bread))>\s?(.*)$')
class BreadBot(bot.Bot): class BreadBot(bot.Bot):
def __init__(
self,
*args: Any,
data_path: str = '/bread/breadbot',
**kwargs: Any,
):
super().__init__(*args, **kwargs)
self.data_path = Path(data_path)
def on_welcome(self, c: ServerConnection, e: Event) -> None: def on_welcome(self, c: ServerConnection, e: Event) -> None:
c.mode(self.bot_nick, '+B') c.mode(self.bot_nick, '+B')
super().on_welcome(c, e) super().on_welcome(c, e)

View File

@ -1,7 +1,6 @@
import logging import logging
import random import random
import re import re
from pathlib import Path
from typing import Optional from typing import Optional
from pinhook import plugin # type: ignore from pinhook import plugin # type: ignore
@ -9,14 +8,11 @@ from pinhook.bot import Bot # type: ignore
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
data_path = Path('/bread/breadbot')
count_file = data_path / 'breadcount.txt'
messages_file = data_path / 'bread_messages.txt'
word_regex = re.compile(r'\W') word_regex = re.compile(r'\W')
def get_that_bread() -> str: def get_that_bread(ctx: Bot.Message) -> str:
messages_file = ctx.bot.data_path / 'bread_messages.txt'
try: try:
bread_messages = messages_file.read_text().strip().splitlines() bread_messages = messages_file.read_text().strip().splitlines()
assert bread_messages assert bread_messages
@ -30,7 +26,8 @@ def get_that_bread() -> str:
return random.choice(bread_messages) return random.choice(bread_messages)
def increment_bread() -> None: def increment_bread(ctx: Bot.Message) -> None:
count_file = ctx.bot.data_path / 'breadcount.txt'
try: try:
count = int(count_file.read_text().strip()) count = int(count_file.read_text().strip())
except (IOError, TypeError, ValueError): except (IOError, TypeError, ValueError):
@ -45,9 +42,9 @@ def increment_bread() -> None:
def bread(ctx: Bot.Message) -> Optional[plugin.Output]: def bread(ctx: Bot.Message) -> Optional[plugin.Output]:
words = set(word_regex.split(ctx.text.lower())) words = set(word_regex.split(ctx.text.lower()))
if 'bread' in words: if 'bread' in words:
increment_bread() increment_bread(ctx)
if words == {'bread'}: if words == {'bread'}:
return plugin.message(get_that_bread()) return plugin.message(get_that_bread(ctx))
return None return None
@ -58,7 +55,7 @@ def bread(ctx: Bot.Message) -> Optional[plugin.Output]:
ops_msg='This command is restricted to breadpunk admins.' ops_msg='This command is restricted to breadpunk admins.'
) )
def nukethebread(ctx: Bot.Message) -> plugin.Output: def nukethebread(ctx: Bot.Message) -> plugin.Output:
count_file.write_text('0') (ctx.bot.data_path / 'breadcount.txt').write_text('0')
return plugin.message('Bread count has been reset.') return plugin.message('Bread count has been reset.')
@ -67,13 +64,15 @@ def nukethebread(ctx: Bot.Message) -> plugin.Output:
help_text='How many times did bakers say bread here?', help_text='How many times did bakers say bread here?',
) )
def breadcount(ctx: Bot.Message) -> plugin.Output: def breadcount(ctx: Bot.Message) -> plugin.Output:
count_file = ctx.bot.data_path / 'breadcount.txt'
try: try:
count = int(count_file.read_text().strip()) count = int(count_file.read_text().strip())
except (IOError, TypeError, ValueError): except (IOError, TypeError, ValueError) as e:
return plugin.message( return plugin.message(
'something is wrong with the breadcount! ' 'something is wrong with the breadcount! '
'quick, scream at lucitoast' 'quick, scream at lucitoast'
) )
logger.warning('breadcount is broken!', exc_info=e)
else: else:
return plugin.message( return plugin.message(
f'{count} bread{"s" if count != 1 else ""} so far' f'{count} bread{"s" if count != 1 else ""} so far'

View File

@ -8,3 +8,4 @@ channels:
- '#bread' - '#bread'
plugin_dir: breadbot/plugins plugin_dir: breadbot/plugins
data_path: /bread/breadbot