Skip to content

Commit

Permalink
Add support media change karma (#75)
Browse files Browse the repository at this point in the history
* add all content-types with caption

* fix caption_entities

* fix emoji and non-caption media message
  • Loading branch information
bomzheg authored Nov 30, 2020
1 parent 05175b3 commit 5beca8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
22 changes: 13 additions & 9 deletions app/filters/karma_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class KarmaFilter(BoundFilter):
karma_change: bool

async def check(self, message: types.Message) -> typing.Dict[str, typing.Dict[str, float]]:
karma_change, comment = get_karma_trigger(message.text or message.sticker.emoji or "")
possible_trigger_text = message.text or message.caption
if possible_trigger_text is None and message.sticker:
possible_trigger_text = message.sticker.emoji or None
karma_change, comment = get_karma_trigger(possible_trigger_text)
if karma_change is None:
return {}
rez = {'karma': {'karma_change': karma_change, 'comment': comment}}
Expand All @@ -35,14 +38,15 @@ def get_karma_trigger(text: str) -> typing.Tuple[typing.Optional[float], str]:
comment: all text after trigger
:param text:
"""
possible_trigger, comment = get_first_word(text)
changer = has_plus_karma(possible_trigger)
if changer:
return changer, comment
possible_trigger, comment = get_first_line(text)
changer = has_minus_karma(possible_trigger)
if changer:
return changer, comment
if text is not None:
possible_trigger, comment = get_first_word(text)
changer = has_plus_karma(possible_trigger)
if changer:
return changer, comment
possible_trigger, comment = get_first_line(text)
changer = has_minus_karma(possible_trigger)
if changer:
return changer, comment
return None, ""


Expand Down
13 changes: 12 additions & 1 deletion app/handlers/change_karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ async def too_fast_change_karma(message: types.Message, *_, **__):
return await message.reply("Вы слишком часто меняете карму")


@dp.message_handler(karma_change=True, has_target=True, content_types=[ContentType.STICKER, ContentType.TEXT])
@dp.message_handler(karma_change=True, has_target=True, content_types=[
ContentType.TEXT,
ContentType.STICKER,
ContentType.ANIMATION,
ContentType.AUDIO,
ContentType.DOCUMENT,
ContentType.PHOTO,
ContentType.VIDEO,
ContentType.VOICE,
])
@a_throttle.throttled(rate=30, on_throttled=too_fast_change_karma)
@dp.throttled(rate=1)
async def karma_change(message: types.Message, karma: dict, user: User, chat: Chat, target: User):
Expand Down
10 changes: 6 additions & 4 deletions app/services/find_target_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ def is_one_user(user_1: types.User, user_2: types.User):


def get_mentioned_user(message: types.Message) -> typing.Optional[types.User]:
if not message.text:
possible_mentioned_text = message.text or message.caption
if not possible_mentioned_text:
return None
if not message.entities:
entities = message.entities or message.caption_entities
if not entities:
return None
for ent in message.entities:
for ent in entities:
if ent.type == "text_mention":
return ent.user
elif ent.type == "mention":
username = ent.get_text(message.text).lstrip("@")
username = ent.get_text(possible_mentioned_text).lstrip("@")
return types.User(username=username)
return None

Expand Down

0 comments on commit 5beca8e

Please sign in to comment.