Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support media change karma #75

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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