MissKatyPyro/misskaty/core/message_utils.py
sourcery-ai[bot] 376755e7b3
Sourcery refactored master branch (#18)
* 'Refactored by Sourcery'

* reformating: code

---------

Co-authored-by: Sourcery AI <>
Co-authored-by: yasirarism <yasiramunandar@gmail.com>
2023-02-13 22:44:16 +07:00

52 lines
1.5 KiB
Python

import asyncio
from logging import getLogger
from pyrogram.errors import ChatWriteForbidden, FloodWait, MessageNotModified, ChatAdminRequired, MessageDeleteForbidden, MessageIdInvalid
LOGGER = getLogger(__name__)
# handler for TG function, so need write exception in every code
# Send MSG Pyro
async def kirimPesan(msg, text, **kwargs):
try:
return await msg.reply(text, **kwargs)
except FloodWait as e:
LOGGER.warning(str(e))
await asyncio.sleep(e.value)
return await kirimPesan(msg, text, **kwargs)
except (ChatWriteForbidden, ChatAdminRequired):
LOGGER.info(f"Leaving from {msg.chat.title} [{msg.chat.id}] because doesn't have admin permission.")
return await msg.leave()
except Exception as e:
LOGGER.error(str(e))
return
# Edit MSG Pyro
async def editPesan(msg, text, **kwargs):
try:
return await msg.edit(text, **kwargs)
except FloodWait as e:
LOGGER.warning(str(e))
await asyncio.sleep(e.value)
return await editPesan(msg, text, **kwargs)
except (MessageNotModified, MessageIdInvalid):
return
except Exception as e:
LOGGER.error(str(e))
return
async def hapusPesan(msg):
try:
return await msg.delete()
except (MessageDeleteForbidden, ChatAdminRequired):
return
except FloodWait as e:
LOGGER.warning(str(e))
await asyncio.sleep(e.value)
return await hapusPesan(msg)
except Exception as e:
LOGGER.error(str(e))