-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move commands to a separate package.
- Loading branch information
1 parent
f9a0ce6
commit 6e76b6e
Showing
12 changed files
with
288 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CommandError(Exception): | ||
pass | ||
|
||
|
||
class BotCommand(object): | ||
def __init__(self, bot, *args, **kwargs): | ||
self.bot = bot | ||
self._db = self.bot.db | ||
self._say = self.bot.say | ||
|
||
def __call__(self, message, *args, **kwargs): | ||
return self.default(message, *args, **kwargs) | ||
|
||
def queue(self, chat_id, msg_id, next_cmd): | ||
self.bot.queue['{}_{}'.format(chat_id, msg_id)] = next_cmd | ||
|
||
@classmethod | ||
def match(cls, message): | ||
return False | ||
|
||
def get_tab(self, tab_id): | ||
return self._db.get_or_create_tab(tab_id)[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import re | ||
from functools import partial | ||
|
||
import telegram | ||
|
||
from . import BotCommand, CommandError | ||
|
||
|
||
class AddCommand(BotCommand): | ||
def get_amount(self, content): | ||
match = re.match('((/add )|(/remove ))?(?P<amount>\d+(\.\d+)?)( (?P<reason>.*))?', content) | ||
if not match: | ||
raise CommandError() | ||
|
||
amount = float(match.groupdict()['amount']) | ||
reason = match.groupdict()['reason'] | ||
return amount, reason | ||
|
||
def add(self, tab_id, user_id, message_id, date, amount, reason=''): | ||
tab = self.get_tab(tab_id) | ||
tab.add(message_id, user_id, date, amount, reason) | ||
|
||
def default(self, message): | ||
content = message.text.split(' ', 1)[1] if len(message.text.split(' ', 1)) == 2 else '' | ||
if content: | ||
self.process_howmuch(message) | ||
else: | ||
msg = self._say(message, 'How much?', reply_markup=telegram.ForceReply(selective=True)) | ||
self.queue(message.chat.id, msg.message_id, partial(self.process_howmuch)) | ||
|
||
def process_howmuch(self, message): | ||
try: | ||
amount, reason = self.get_amount(message.text) | ||
except CommandError: | ||
self._say(message, "Nope, I don't get ya") | ||
return | ||
self.add(message.chat.id, message.from_user.id, message.message_id, | ||
message.date, amount, reason) | ||
self._say(message, telegram.Emoji.THUMBS_UP_SIGN) | ||
|
||
@classmethod | ||
def match(cls, message): | ||
if message.text and message.text.startswith('/add'): | ||
return True |
Oops, something went wrong.