This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Добавил бота функцию "карма". #26
Open
MikhailWar
wants to merge
5
commits into
BotfatherDev:work_in_groups
Choose a base branch
from
MikhailWar:work_in_groups
base: work_in_groups
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -102,7 +102,7 @@ celerybeat.pid | |
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.env.dist | ||
.venv | ||
env/ | ||
venv/ | ||
|
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,10 @@ | ||
|
||
|
||
# тут мы будем хранить данные для того, чтобы не наргуржать базу данных не нужной информацией. | ||
""" | ||
format | ||
{message_id: user_id} | ||
|
||
""" | ||
reply_message_id_user = {} | ||
|
Binary file not shown.
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,42 @@ | ||
from aiogram import types | ||
from aiogram.dispatcher.filters import BoundFilter | ||
from loguru import logger | ||
|
||
from data.cache_karma import reply_message_id_user | ||
from loader import db, bot | ||
|
||
|
||
class Restriction_Karma(BoundFilter): | ||
|
||
async def check(self, message: types.Message) -> bool: | ||
bot_id = (await bot.get_me()).id | ||
my_user_id = message.from_user.id | ||
user_id_not_karma = [bot_id, my_user_id] | ||
|
||
# чтобы нельзя было давать карму боту и самому себе | ||
if message.reply_to_message.from_user.id in user_id_not_karma: | ||
await message.delete() | ||
return False | ||
|
||
if await self._get_cached_message_value(message) is None: | ||
await self._set_cached_message_value(message) | ||
db.add_user(user_id=message.reply_to_message.from_user.id, | ||
full_name=message.reply_to_message.from_user.full_name) | ||
await message.delete() | ||
return True | ||
|
||
async def _set_cached_message_value(self, message: types.Message): | ||
if not await self._get_cached_message_value(message) is None: | ||
return | ||
reply_message_id_user[message.reply_to_message.message_id] = message.from_user.id | ||
|
||
async def _get_cached_message_value(self, message: types.Message): | ||
try: | ||
return reply_message_id_user[message.reply_to_message.message_id] | ||
except Exception as error: | ||
logger.info(error) | ||
|
||
|
||
|
||
|
||
|
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,11 @@ | ||
from aiogram import types | ||
from aiogram.dispatcher.filters import BoundFilter | ||
|
||
|
||
class IsPrivateAdmin(BoundFilter): | ||
def __init__(self, users_id: list): | ||
self.users_id = list(map(int, users_id)) | ||
|
||
async def check(self, message:types.Message): | ||
|
||
return message.from_user.id in self.users_id |
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .errors import dp | ||
from .other import dp | ||
from .metabolism import dp | ||
from .karma import dp | ||
|
||
__all__ = ["dp"] |
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,72 @@ | ||
import re | ||
|
||
from aiogram import types | ||
from aiogram.dispatcher.filters import Command, AdminFilter | ||
from aiogram.utils.markdown import hlink, hbold | ||
|
||
from data.cache_karma import reply_message_id_user | ||
from data.config import ADMINS_ID | ||
from filters import IsGroup, IsReplyFilter, IsPrivate, IsPrivateAdmin | ||
from filters.access_karma import Restriction_Karma | ||
from loader import dp, bot, db | ||
from utils.misc import rate_limit | ||
from utils.misc.karma import karma, url | ||
|
||
|
||
@dp.message_handler(IsPrivate(), IsPrivateAdmin(ADMINS_ID), Command("clear")) | ||
async def clear_cache_message_id_karma(message: types.Message): | ||
size = None | ||
|
||
if type(reply_message_id_user) is dict: | ||
size = 144 | ||
if len(reply_message_id_user) > 8: | ||
size += 12 * (len(reply_message_id_user) - 8) | ||
await message.answer(f"Памяти в кэше {size}\n" | ||
"Было очищено") | ||
reply_message_id_user.clear() | ||
|
||
@rate_limit(limit=0) | ||
@dp.message_handler(IsGroup(), Command("top_karma", prefixes="!/")) | ||
async def result_top_users(message: types.Message): | ||
name_bot = await bot.get_me() | ||
command_parse = re.compile(r"(!top_karma|/top_karma) ?(\d+)?") | ||
a = command_parse.match(message.text) | ||
limit = a.group(2) | ||
|
||
if limit is None: | ||
limit = 10 | ||
|
||
if int(limit) > 20: | ||
limit = 20 | ||
|
||
list_top_users = db.select_karma_top(limit=limit) | ||
text_messages = [f"Топ участников чата по карме: {hbold(name_bot.full_name)}"] | ||
|
||
for count, user_object in enumerate(list_top_users, 1): | ||
user_id, karma_user, full_name = user_object | ||
text = f"{count}. {hlink(full_name, url(user_id))} - {karma_user} 💋" | ||
text_messages.append(text) | ||
|
||
await message.answer("\n".join(text_messages)) | ||
|
||
@rate_limit(limit=60) | ||
@dp.message_handler(IsGroup(), IsReplyFilter(is_reply=True), Restriction_Karma(), text=["+", "-"]) | ||
async def give_karma_user(message: types.Message): | ||
|
||
answer = message.text | ||
text_with_karma = {"+": (1, "пополнил"), | ||
"-": (-1, "понизил")} | ||
karma(message, karma=text_with_karma[f"{answer}"][0]) | ||
|
||
user = hlink(message.from_user.first_name, message.from_user.url) | ||
text = text_with_karma.get(answer)[1] | ||
to_user = hlink(message.reply_to_message.from_user.first_name, message.reply_to_message.from_user.url) | ||
get_karma = str(text_with_karma.get(answer)[0]) | ||
total_karma = db.select_karma_user(user_id=message.reply_to_message.from_user.id)[0][1] # получаем общую карму пользователя | ||
|
||
await message.answer(R"""{user} {text} {to_user} карму на {get_karma} | ||
Текущая карма: {total_karma}""".format(user=user, | ||
text=text, | ||
to_user=to_user, | ||
get_karma=get_karma, | ||
total_karma=total_karma)) |
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
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from aiogram import Dispatcher | ||
from loguru import logger | ||
|
||
from .throttling import ThrottlingMiddleware | ||
|
||
from .throttling import ThrottlingMiddleware | ||
|
||
def setup(dp: Dispatcher): | ||
logger.info("Подключение middlewares...") | ||
dp.middleware.setup(ThrottlingMiddleware()) | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нет, нахрена это? dist - файл-пример