2020-02-19 15:31:39 +00:00
|
|
|
""" PagerMaid event listener. """
|
|
|
|
|
2021-04-03 15:24:07 +00:00
|
|
|
import sys, sentry_sdk, re
|
2020-08-09 15:17:55 +00:00
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
from telethon import events
|
|
|
|
from telethon.errors import MessageTooLongError
|
|
|
|
from distutils2.util import strtobool
|
|
|
|
from traceback import format_exc
|
|
|
|
from time import gmtime, strftime, time
|
|
|
|
from telethon.events import StopPropagation
|
2021-07-17 17:25:54 +00:00
|
|
|
from pagermaid import bot, config, help_messages, logs, mp, user_id
|
2021-07-18 05:52:17 +00:00
|
|
|
from pagermaid.utils import attach_report, lang, alias_command
|
2020-10-01 04:16:54 +00:00
|
|
|
|
2021-07-17 17:25:54 +00:00
|
|
|
try:
|
|
|
|
allow_analytics = strtobool(config['allow_analytics'])
|
|
|
|
except KeyError:
|
|
|
|
allow_analytics = True
|
|
|
|
except ValueError:
|
|
|
|
allow_analytics = True
|
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
|
2020-08-09 15:46:25 +00:00
|
|
|
def noop(*args, **kw):
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
|
2021-04-12 16:25:32 +00:00
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
def listener(**args):
|
|
|
|
""" Register an event listener. """
|
|
|
|
command = args.get('command', None)
|
|
|
|
description = args.get('description', None)
|
|
|
|
parameters = args.get('parameters', None)
|
|
|
|
pattern = args.get('pattern', None)
|
|
|
|
diagnostics = args.get('diagnostics', True)
|
|
|
|
ignore_edited = args.get('ignore_edited', False)
|
2020-08-09 15:17:55 +00:00
|
|
|
is_plugin = args.get('is_plugin', True)
|
2020-02-19 15:31:39 +00:00
|
|
|
if command is not None:
|
|
|
|
if command in help_messages:
|
2021-04-12 16:25:32 +00:00
|
|
|
raise ValueError(f"{lang('error_prefix')} {lang('command')} \"{command}\" {lang('has_reg')}")
|
2020-02-19 15:31:39 +00:00
|
|
|
pattern = fr"^-{command}(?: |$)([\s\S]*)"
|
|
|
|
if pattern is not None and not pattern.startswith('(?i)'):
|
|
|
|
args['pattern'] = f"(?i){pattern}"
|
|
|
|
else:
|
|
|
|
args['pattern'] = pattern
|
|
|
|
if 'ignore_edited' in args:
|
|
|
|
del args['ignore_edited']
|
|
|
|
if 'command' in args:
|
|
|
|
del args['command']
|
|
|
|
if 'diagnostics' in args:
|
|
|
|
del args['diagnostics']
|
|
|
|
if 'description' in args:
|
|
|
|
del args['description']
|
|
|
|
if 'parameters' in args:
|
|
|
|
del args['parameters']
|
2020-08-09 15:17:55 +00:00
|
|
|
if 'is_plugin' in args:
|
|
|
|
del args['is_plugin']
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
def decorator(function):
|
|
|
|
|
|
|
|
async def handler(context):
|
|
|
|
try:
|
2021-07-17 17:25:54 +00:00
|
|
|
analytics = True
|
2020-02-19 15:31:39 +00:00
|
|
|
try:
|
|
|
|
parameter = context.pattern_match.group(1).split(' ')
|
|
|
|
if parameter == ['']:
|
|
|
|
parameter = []
|
|
|
|
context.parameter = parameter
|
|
|
|
context.arguments = context.pattern_match.group(1)
|
|
|
|
except BaseException:
|
2021-07-17 17:25:54 +00:00
|
|
|
analytics = False
|
2020-02-19 15:31:39 +00:00
|
|
|
context.parameter = None
|
|
|
|
context.arguments = None
|
|
|
|
await function(context)
|
2021-07-18 05:52:17 +00:00
|
|
|
if analytics and allow_analytics:
|
2021-07-17 17:25:54 +00:00
|
|
|
try:
|
|
|
|
upload_command = context.text.split()[0].replace('-', '')
|
2021-07-18 05:52:17 +00:00
|
|
|
upload_command = alias_command(upload_command)
|
2021-07-17 17:25:54 +00:00
|
|
|
if context.sender_id:
|
|
|
|
if context.sender_id > 0:
|
|
|
|
mp.track(str(context.sender_id), f'Function {upload_command}',
|
|
|
|
{'command': upload_command})
|
|
|
|
else:
|
|
|
|
mp.track(str(user_id), f'Function {upload_command}',
|
|
|
|
{'command': upload_command})
|
|
|
|
else:
|
|
|
|
mp.track(str(user_id), f'Function {upload_command}',
|
|
|
|
{'command': upload_command})
|
|
|
|
except Exception as e:
|
|
|
|
logs.info(f"Analytics Error ~ {e}")
|
2020-02-19 15:31:39 +00:00
|
|
|
except StopPropagation:
|
|
|
|
raise StopPropagation
|
|
|
|
except MessageTooLongError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('too_long'))
|
2021-04-03 15:24:07 +00:00
|
|
|
except BaseException as e:
|
2020-08-11 15:49:16 +00:00
|
|
|
exc_info = sys.exc_info()[1]
|
|
|
|
exc_format = format_exc()
|
2020-02-19 15:31:39 +00:00
|
|
|
try:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('run_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
except BaseException:
|
|
|
|
pass
|
|
|
|
if not diagnostics:
|
|
|
|
return
|
2020-04-06 06:16:23 +00:00
|
|
|
if strtobool(config['error_report']):
|
|
|
|
report = f"# Generated: {strftime('%H:%M %d/%m/%Y', gmtime())}. \n" \
|
|
|
|
f"# ChatID: {str(context.chat_id)}. \n" \
|
|
|
|
f"# UserID: {str(context.sender_id)}. \n" \
|
|
|
|
f"# Message: \n-----BEGIN TARGET MESSAGE-----\n" \
|
|
|
|
f"{context.text}\n-----END TARGET MESSAGE-----\n" \
|
|
|
|
f"# Traceback: \n-----BEGIN TRACEBACK-----\n" \
|
2020-08-11 15:49:16 +00:00
|
|
|
f"{str(exc_format)}\n-----END TRACEBACK-----\n" \
|
|
|
|
f"# Error: \"{str(exc_info)}\". \n"
|
2020-10-01 04:16:54 +00:00
|
|
|
await attach_report(report, f"exception.{time()}.pagermaid", None,
|
2021-04-12 16:25:32 +00:00
|
|
|
"Error report generated.")
|
2020-10-01 04:16:54 +00:00
|
|
|
try:
|
2021-04-22 16:47:50 +00:00
|
|
|
sentry_sdk.set_context("Target", {"ChatID": str(context.chat_id), "UserID": str(context.sender_id), "Msg": context.text})
|
|
|
|
sentry_sdk.set_tag('com', re.findall("\w+",str.lower(context.text.split()[0]))[0])
|
2021-04-03 15:24:07 +00:00
|
|
|
sentry_sdk.capture_exception(e)
|
2020-10-01 04:16:54 +00:00
|
|
|
except:
|
|
|
|
logs.info(
|
2021-04-12 16:25:32 +00:00
|
|
|
lang('report_error')
|
2020-10-01 04:16:54 +00:00
|
|
|
)
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
if not ignore_edited:
|
|
|
|
bot.add_event_handler(handler, events.MessageEdited(**args))
|
|
|
|
bot.add_event_handler(handler, events.NewMessage(**args))
|
|
|
|
|
|
|
|
return handler
|
|
|
|
|
2020-08-09 15:46:25 +00:00
|
|
|
if not is_plugin and 'disabled_cmd' in config:
|
|
|
|
if config['disabled_cmd'].count(command) != 0:
|
|
|
|
return noop
|
|
|
|
|
2020-02-19 15:31:39 +00:00
|
|
|
if description is not None and command is not None:
|
|
|
|
if parameters is None:
|
|
|
|
parameters = ""
|
|
|
|
help_messages.update({
|
2021-04-12 16:25:32 +00:00
|
|
|
f"{command}": f"**{lang('use_method')}:** `-{command} {parameters}`\
|
2020-02-19 15:31:39 +00:00
|
|
|
\n{description}"
|
|
|
|
})
|
|
|
|
|
|
|
|
return decorator
|