Make breadcount file path configurable
This commit is contained in:
parent
190b2ee7ec
commit
cd8b767ca8
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
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):
|
||||
|
||||
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:
|
||||
c.mode(self.bot_nick, '+B')
|
||||
super().on_welcome(c, e)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import logging
|
||||
import random
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from pinhook import plugin # type: ignore
|
||||
|
@ -9,14 +8,11 @@ from pinhook.bot import Bot # type: ignore
|
|||
|
||||
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')
|
||||
|
||||
|
||||
def get_that_bread() -> str:
|
||||
def get_that_bread(ctx: Bot.Message) -> str:
|
||||
messages_file = ctx.bot.data_path / 'bread_messages.txt'
|
||||
try:
|
||||
bread_messages = messages_file.read_text().strip().splitlines()
|
||||
assert bread_messages
|
||||
|
@ -30,7 +26,8 @@ def get_that_bread() -> str:
|
|||
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:
|
||||
count = int(count_file.read_text().strip())
|
||||
except (IOError, TypeError, ValueError):
|
||||
|
@ -45,9 +42,9 @@ def increment_bread() -> None:
|
|||
def bread(ctx: Bot.Message) -> Optional[plugin.Output]:
|
||||
words = set(word_regex.split(ctx.text.lower()))
|
||||
if 'bread' in words:
|
||||
increment_bread()
|
||||
increment_bread(ctx)
|
||||
if words == {'bread'}:
|
||||
return plugin.message(get_that_bread())
|
||||
return plugin.message(get_that_bread(ctx))
|
||||
return None
|
||||
|
||||
|
||||
|
@ -58,7 +55,7 @@ def bread(ctx: Bot.Message) -> Optional[plugin.Output]:
|
|||
ops_msg='This command is restricted to breadpunk admins.'
|
||||
)
|
||||
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.')
|
||||
|
||||
|
||||
|
@ -67,13 +64,15 @@ def nukethebread(ctx: Bot.Message) -> plugin.Output:
|
|||
help_text='How many times did bakers say bread here?',
|
||||
)
|
||||
def breadcount(ctx: Bot.Message) -> plugin.Output:
|
||||
count_file = ctx.bot.data_path / 'breadcount.txt'
|
||||
try:
|
||||
count = int(count_file.read_text().strip())
|
||||
except (IOError, TypeError, ValueError):
|
||||
except (IOError, TypeError, ValueError) as e:
|
||||
return plugin.message(
|
||||
'something is wrong with the breadcount! '
|
||||
'quick, scream at lucitoast'
|
||||
)
|
||||
logger.warning('breadcount is broken!', exc_info=e)
|
||||
else:
|
||||
return plugin.message(
|
||||
f'{count} bread{"s" if count != 1 else ""} so far'
|
||||
|
|
|
@ -8,3 +8,4 @@ channels:
|
|||
- '#bread'
|
||||
|
||||
plugin_dir: breadbot/plugins
|
||||
data_path: /bread/breadbot
|
||||
|
|
Loading…
Reference in New Issue