Mutes ready

This commit is contained in:
SantaSpeen 2022-03-22 00:12:27 +03:00
parent 6335170a8f
commit c451190632
2 changed files with 46 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import asyncio
import logging
import time
@ -101,10 +102,27 @@ async def mute(msg: types.Message):
if reply_message:
warn_user = reply_message.from_user
user_id = warn_user.id
await tools.set_user_permissions(user_id, msg.chat.id, mute_perm)
splt = msg.text.split(" ")[1:]
if len(splt) > 0:
c = time.time()
try:
for word in splt:
word: str
if word.endswith("d"):
c += int(word[:len(word)-1]) * 24 * 60 * 60
elif word.endswith("h"):
c += int(word[:len(word)-1]) * 60 * 60
elif word.endswith("m"):
c += int(word[:len(word)-1]) * 60
elif word.endswith("s"):
c += int(word[:len(word)-1])
tools.set_mute(user_id, c)
await tools.set_user_permissions(user_id, msg.chat.id, mute_perm)
except Exception as e:
await msg.reply(f"Exception: <code>{e}</code>", parse_mode=ParseMode.HTML)
else:
await msg.reply("Укажи время: <code>/mute 1d 5h 10m 30s</code>", parse_mode=ParseMode.HTML)
else:
await msg.reply("Сначала надо выбрать пользователя.")
@ -164,13 +182,13 @@ async def new_chat_member(msg: types.Message):
@dp.message_handler(content_types=['text', 'photo', 'document', 'audio', 'sticker', 'animation', 'voice', 'video_note'])
async def all_messages(msg: types.Message):
global mute_all
text = msg.text.lower()
text = msg.text
user_id = msg.from_user.id
log.info(f"New message from {user_id}(@{msg.from_user.username}) in {msg.chat.id}: '{text}'; "
f"Type: {msg.content_type}")
if msg.chat.type in [ChatType.SUPERGROUP, ChatType.GROUP]: # Если сообщение пришло из группы
asyncio.create_task(tools.fix_muted(unmute_perm))
admins = await tools.admins
if user_id in admins['ids']:
if text == "суд идёт":

View File

@ -3,7 +3,7 @@ import time
import aiogram
from aiogram.types import ParseMode
from peewee import DoesNotExist
from peewee import DoesNotExist, ModelObjectCursorWrapper
from SqlModels import Users, Mailing
from config import Config
@ -44,6 +44,16 @@ class Tools:
user = Users(user_id=user_id)
return user
async def fix_muted(self, unmute_perm):
users = Users().select().where(Users.muted_until <= time.time())
for user in users:
user.muted_until = None
user.save()
await self.set_user_permissions(user.user_id,
self.config.remote_chat,
unmute_perm)
# ModelObjectCursasdaorWrapper
async def set_user_permissions(self, user_id, chat_id, permissions):
try:
await self.dispatcher.bot.restrict_chat_member(chat_id, user_id, permissions=permissions)
@ -55,6 +65,12 @@ class Tools:
parse_mode=ParseMode.MARKDOWN)
return False
@classmethod
def set_mute(cls, user_id, until):
user = cls.get_user(user_id)
user.muted_until = until
user.save()
async def kick_chat_member(self, chat_id, user_id):
try:
await self.dispatcher.bot.kick_chat_member(chat_id, user_id)
@ -101,6 +117,11 @@ class Tools:
user = cls.get_user(user_id)
return user.banned, user.ban_msg, user.ban_by
@classmethod
def is_muted(cls, user_id):
user = cls.get_user(user_id)
return False if user.muted_until == 0 else time.time() >= user.muted_until
async def ban_user(self, msg: aiogram.types.Message):
user = self.get_user(msg.reply_to_message.from_user.id)
@ -122,3 +143,4 @@ class Tools:
Mailing(user_id=user_id).save()
return registered