Commit 04368ad7 authored by zeroone2numeral2's avatar zeroone2numeral2
Browse files

Lints

parent fc3536fa
import logging
import importlib
from telegram.ext.dispatcher import run_async
from bot import updater
from bot import dispatcher
logging.basicConfig(format='[%(asctime)s][%(name)s] %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
@run_async
def error_callback(bot, update, error):
pass # p-t-b's logger already logs this to the console
def main():
for modname in ["sed", "help", "yesno"]:
......@@ -18,10 +15,9 @@ def main():
for handler in module.handlers:
dispatcher.add_handler(handler)
dispatcher.add_error_handler(error_callback)
updater.start_polling(clean=True)
updater.idle()
if __name__ == '__main__':
main()
\ No newline at end of file
main()
from telegram.ext import Filters
from telegram.ext import BaseFilter
class _Reply_text(BaseFilter):
name = 'Filters.reply_text'
......@@ -9,4 +10,5 @@ class _Reply_text(BaseFilter):
return bool(message.reply_to_message.text or message.reply_to_message.caption)
return False
Filters.reply_text = _Reply_text()
import re
from telegram import Update
from telegram.ext import RegexHandler
class FilteredRegexHandler(RegexHandler):
def __init__(self, *args, **kwargs):
self.filters = kwargs.pop("filters")
RegexHandler.__init__(self, *args, **kwargs)
def check_update(self, update):
......@@ -25,4 +28,5 @@ class FilteredRegexHandler(RegexHandler):
update.effective_message.text:
match = re.match(self.pattern, update.effective_message.text)
return bool(match)
return False
\ No newline at end of file
import logging
from telegram.ext import CommandHandler
from telegram.ext import CallbackQueryHandler
from telegram.ext.dispatcher import run_async
from telegram import InlineKeyboardMarkup
from telegram import InlineKeyboardButton
from telegram import ParseMode
from bot.customfilters import Filters
from bot import strings as s
......@@ -18,23 +19,24 @@ short_help_markup = InlineKeyboardMarkup([
[InlineKeyboardButton("reduce", callback_data="reduce")]
])
@run_async
def help_message(bot, update):
def help_message(_, update):
logger.info("/help or /start command")
update.message.reply_markdown(s.help_short,
reply_markup=extended_help_markup, disable_web_page_preview=True)
update.message.reply_markdown(s.help_short, reply_markup=extended_help_markup,
disable_web_page_preview=True)
@run_async
def on_extended_help_button(bot, update):
def on_extended_help_button(_, update):
logger.info("extend help")
update.callback_query.message.edit_text(s.help_extended,
reply_markup=short_help_markup, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
update.callback_query.message.edit_text(s.help_extended, reply_markup=short_help_markup,
parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
@run_async
def on_short_help_button(bot, update):
def on_short_help_button(_, update):
logger.info("reduce help")
update.callback_query.message.edit_text(s.help_short,
reply_markup=extended_help_markup, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
update.callback_query.message.edit_text(s.help_short, reply_markup=extended_help_markup,
parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
class module:
name = "help"
......
import logging
from html import escape as html_escape
from telegram.ext.dispatcher import run_async
from telegram.error import BadRequest
from telegram.constants import MAX_MESSAGE_LENGTH
from bot.customfilters import Filters
from bot.regexer import Regex
from bot.filteredregexhandler import FilteredRegexHandler
from bot import strings as s
logger = logging.getLogger(__name__)
......@@ -17,13 +17,13 @@ MODES = {
"*": "*{}"
}
def get_response(mode, string):
mode = mode.replace("/", "")
return MODES[mode].format(html_escape(string))
@run_async
def on_sed(bot, update, groups):
def on_sed(_, update, groups):
text = update.message.reply_to_message.text or update.message.reply_to_message.caption
mode = groups[0]
pattern = groups[1]
......@@ -45,19 +45,20 @@ def on_sed(bot, update, groups):
except Exception as e:
logger.info("re.subn exception: %s", str(e), exc_info=True)
#update.message.reply_text(s.oopsie_woopsie) # might be the user who fucked up the regex
return # don't proceed further
return # don't proceed further
if n_subs > 0:
if len(new_string) > MAX_MESSAGE_LENGTH:
logger.info("result too long: substringing...")
new_string = new_string[:MAX_MESSAGE_LENGTH-16] # -16: "*Did you mean:*\n"
new_string = new_string[:MAX_MESSAGE_LENGTH-16] # -16: "*Did you mean:*\n"
update.message.reply_to_message.reply_html(get_response(mode, new_string), disable_web_page_preview=True)
if mode.endswith("/"): # try to delete the command
if mode.endswith("/"): # try to delete the command
try:
update.message.delete()
except BadRequest as e: # the bot doesn't have the permission to delete the message
except BadRequest as e: # the bot doesn't have the permission to delete the message
logger.info("exception while trying to delete a message: %s", e)
class module:
name = "sed"
handlers = (
......
import re
import logging
from random import choice
from telegram.ext.dispatcher import run_async
from telegram.ext import RegexHandler
from bot.customfilters import Filters
from bot import strings as s
logger = logging.getLogger(__name__)
YESNO_REGEX = re.compile(r".*(?:y(?:es)?\/no?|no?\/y(?:es)?)$", re.I)
@run_async
def on_yesno(bot, update):
def on_yesno(_, update):
logger.info("yes/no")
reply = choice(s.yesno_list)
update.message.reply_text(reply)
class module:
name = "yesno"
handlers = (
RegexHandler(YESNO_REGEX, on_yesno),
)
\ No newline at end of file
)
......@@ -3,7 +3,9 @@ import logging
logger = logging.getLogger(__name__)
REGEX_FLAGS = "ilmsax"
FLAGS_DICT = {
"i": re.I,
"l": re.L,
......@@ -13,22 +15,24 @@ FLAGS_DICT = {
"x": re.X
}
class Regex():
class Regex:
def __init__(self, string, pattern, repl, flags=None):
self.string = string
self.pattern = pattern
self.repl = repl
self.count = 1 # by default, make just one replacement, unless the "g" flag is passed
self.flags = 0 # default value for the "flags" argument of re.sub/re.subn
self.count = 1 # by default, make just one replacement, unless the "g" flag is passed
self.flags = 0 # default value for the "flags" argument of re.sub/re.subn
if flags:
for flag in flags:
logger.info("elaborating flag: %s", flag)
flag_lower = flag.lower()
if flag_lower == "g": # re.G: don't return after the first match
if flag_lower == "g": # re.G: don't return after the first match
logger.info("<g> flag found")
self.count = 0 # passing count=0 to resub/re.subn will make it not return after the first match
self.count = 0 # passing count=0 to resub/re.subn will make it not return after the first match
if flag_lower in REGEX_FLAGS:
self.flags |= FLAGS_DICT[flag_lower] # biwise-concatenete the re.FLAG object
self.flags |= FLAGS_DICT[flag_lower] # bitwise-concatenete the re.FLAG object
def subn(self, escape_html=False):
return re.subn(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment