Minor bug fixes. Base for okteto pinger support

pull/1/head
hikari.ftg 2022-03-27 12:40:52 +00:00
parent 114fc76b6d
commit bf80acb0d1
10 changed files with 94 additions and 26 deletions

View File

@ -56,7 +56,7 @@ from .translations.core import Translator
from math import ceil from math import ceil
__version__ = (1, 0, 2) __version__ = (1, 0, 3)
try: try:
from .web import core from .web import core
except ImportError: except ImportError:

View File

@ -85,8 +85,7 @@ class CoreMod(loader.Module):
await utils.answer(message, self.strings("hikka").format(*ver)) await utils.answer(message, self.strings("hikka").format(*ver))
async def blacklistcmd(self, message: Message) -> None: async def blacklistcmd(self, message: Message) -> None:
""".blacklist [id] """Blacklist the bot from operating somewhere"""
Blacklist the bot from operating somewhere"""
chatid = await self.blacklistcommon(message) chatid = await self.blacklistcommon(message)
self._db.set( self._db.set(
@ -98,8 +97,7 @@ class CoreMod(loader.Module):
await utils.answer(message, self.strings("blacklisted", message).format(chatid)) await utils.answer(message, self.strings("blacklisted", message).format(chatid))
async def unblacklistcmd(self, message: Message) -> None: async def unblacklistcmd(self, message: Message) -> None:
""".unblacklist [id] """Unblacklist the bot from operating somewhere"""
Unblacklist the bot from operating somewhere"""
chatid = await self.blacklistcommon(message) chatid = await self.blacklistcommon(message)
self._db.set( self._db.set(
@ -130,8 +128,7 @@ class CoreMod(loader.Module):
return return
async def blacklistusercmd(self, message: Message) -> None: async def blacklistusercmd(self, message: Message) -> None:
""".blacklistuser [id] """Prevent this user from running any commands"""
Prevent this user from running any commands"""
user = await self.getuser(message) user = await self.getuser(message)
self._db.set( self._db.set(
@ -145,8 +142,7 @@ class CoreMod(loader.Module):
) )
async def unblacklistusercmd(self, message: Message) -> None: async def unblacklistusercmd(self, message: Message) -> None:
""".unblacklistuser [id] """Allow this user to run permitted commands"""
Allow this user to run permitted commands"""
user = await self.getuser(message) user = await self.getuser(message)
self._db.set( self._db.set(
@ -280,8 +276,6 @@ class CoreMod(loader.Module):
async def setlangcmd(self, message: Message) -> None: async def setlangcmd(self, message: Message) -> None:
"""Change the preferred language used for translations """Change the preferred language used for translations
Specify the language as space separated list of ISO 639-1 language codes in order of preference (e.g. fr en)
With no parameters, all translations are disabled
Restart required after use""" Restart required after use"""
langs = utils.get_args(message) langs = utils.get_args(message)
self._db.set(main.__name__, "language", langs) self._db.set(main.__name__, "language", langs)

View File

@ -64,7 +64,7 @@ class HelpMod(loader.Module):
*Split modules by spaces""" *Split modules by spaces"""
modules = utils.get_args(message) modules = utils.get_args(message)
if not modules: if not modules:
await utils.answer(message, self.strings("no_mod")) await utils.answer(message, self.strings("no_mod", message))
return return
mods = [ mods = [
@ -88,7 +88,7 @@ class HelpMod(loader.Module):
await utils.answer( await utils.answer(
message, message,
self.strings("hidden_shown").format( self.strings("hidden_shown", message).format(
len(hidden), len(hidden),
len(shown), len(shown),
"\n".join([f"👁‍🗨 <i>{m}</i>" for m in hidden]), "\n".join([f"👁‍🗨 <i>{m}</i>" for m in hidden]),
@ -121,7 +121,7 @@ class HelpMod(loader.Module):
if args in self.allmodules.commands: if args in self.allmodules.commands:
module = self.allmodules.commands[args].__self__ module = self.allmodules.commands[args].__self__
else: else:
await utils.answer(message, self.strings("bad_module").format(args)) await utils.answer(message, self.strings("bad_module", message).format(args))
return return
try: try:
@ -129,7 +129,7 @@ class HelpMod(loader.Module):
except KeyError: except KeyError:
name = getattr(module, "name", "ERROR") name = getattr(module, "name", "ERROR")
reply = self.strings("single_mod_header").format(utils.escape_html(name)) reply = self.strings("single_mod_header", message).format(utils.escape_html(name))
if module.__doc__: if module.__doc__:
reply += ( reply += (
"<i>\n " + utils.escape_html(inspect.getdoc(module)) + "\n</i>" "<i>\n " + utils.escape_html(inspect.getdoc(module)) + "\n</i>"
@ -161,11 +161,11 @@ class HelpMod(loader.Module):
reply += self.strings("undoc_ihandler", message) reply += self.strings("undoc_ihandler", message)
for name, fun in commands.items(): for name, fun in commands.items():
reply += self.strings("single_cmd").format(prefix, name) reply += self.strings("single_cmd", message).format(prefix, name)
if fun.__doc__: if fun.__doc__:
reply += utils.escape_html(inspect.getdoc(fun)) reply += utils.escape_html(inspect.getdoc(fun))
else: else:
reply += self.strings("undoc_cmd") reply += self.strings("undoc_cmd", message)
await utils.answer(message, reply) await utils.answer(message, reply)
return return
@ -187,7 +187,7 @@ class HelpMod(loader.Module):
hidden = list(filter(lambda module: module in mods, self.get("hide", []))) hidden = list(filter(lambda module: module in mods, self.get("hide", [])))
self.set("hide", hidden) self.set("hide", hidden)
reply = self.strings("all_header").format(count, len(hidden) if not force else 0) reply = self.strings("all_header", message).format(count, len(hidden) if not force else 0)
shown_warn = False shown_warn = False
plain_ = [] plain_ = []
@ -231,7 +231,7 @@ class HelpMod(loader.Module):
else: else:
emoji = self.config['plain_emoji'] emoji = self.config['plain_emoji']
tmp += self.strings("mod_tmpl").format(emoji, name) tmp += self.strings("mod_tmpl", message).format(emoji, name)
first = True first = True
@ -243,10 +243,10 @@ class HelpMod(loader.Module):
for cmd in commands: for cmd in commands:
if first: if first:
tmp += self.strings("first_cmd_tmpl").format(cmd) tmp += self.strings("first_cmd_tmpl", message).format(cmd)
first = False first = False
else: else:
tmp += self.strings("cmd_tmpl").format(cmd) tmp += self.strings("cmd_tmpl", message).format(cmd)
icommands = [ icommands = [
name name
@ -256,10 +256,10 @@ class HelpMod(loader.Module):
for cmd in icommands: for cmd in icommands:
if first: if first:
tmp += self.strings("first_cmd_tmpl").format(f"🎹 {cmd}") tmp += self.strings("first_cmd_tmpl", message).format(f"🎹 {cmd}")
first = False first = False
else: else:
tmp += self.strings("cmd_tmpl").format(f"🎹 {cmd}") tmp += self.strings("cmd_tmpl", message).format(f"🎹 {cmd}")
if commands or icommands: if commands or icommands:
tmp += " )" tmp += " )"

View File

@ -192,6 +192,7 @@ class HikkaSettingsMod(loader.Module):
self._db.set(main.__name__, "nonickusers", nn) self._db.set(main.__name__, "nonickusers", nn)
async def nonickcmdcmd(self, message: Message) -> None: async def nonickcmdcmd(self, message: Message) -> None:
"""Allow certain command to be executed without nickname"""
args = utils.get_args_raw(message) args = utils.get_args_raw(message)
if not args: if not args:
return await utils.answer(message, self.strings("no_cmd")) return await utils.answer(message, self.strings("no_cmd"))

View File

@ -207,8 +207,7 @@ class TestMod(loader.Module):
@loader.owner @loader.owner
async def suspendcmd(self, message: Message) -> None: async def suspendcmd(self, message: Message) -> None:
""".suspend <time> """<time> - Suspends the bot for N seconds"""
Suspends the bot for N seconds"""
try: try:
time_sleep = float(utils.get_args_raw(message)) time_sleep = float(utils.get_args_raw(message))
await utils.answer( await utils.answer(

View File

@ -0,0 +1,49 @@
import os
import re
result = "{\n"
for mod in os.scandir("."):
if not mod.path.endswith(".py") or mod.path.endswith("checker.py"):
continue
with open(mod.path, "r") as f:
code = f.read()
print(mod.path)
try:
strings = re.search("strings = {(.*?) }", code, flags=re.S).group(1)
except AttributeError:
continue
strs = [
(
re.search('"(.*?)": "(.*?)"', line).group(1),
re.search('"(.*?)": "(.*?)"', line).group(2),
)
for line in strings.splitlines()
if re.search('"(.*?)": "(.*?)"', line) is not None
]
for st, tr in strs:
if st == "name":
continue
result += f" \"hikka.modules.{mod.path.split('/')[-1].split('.')[0]}.{st}\": \"{tr}\",\n"
cmds = re.findall(r" +async def ([a-zA-Zа-яА-Я]*?)cmd\(.*?\).*?:.*?\"\"\"(.*?)\"\"\"", code, flags=re.S)
for cmd, doc in cmds:
doc = doc.replace('\n', '\\n')
result += f" \"hikka.modules.{mod.path.split('/')[-1].split('.')[0]}_cmd_doc_{cmd}\": \"{doc}\",\n"
cls_ = re.search(r"@loader\.tds.*?class .*?Mod\(loader\.Module\):.*?\"\"\"(.*?)\"\"\"", code, flags=re.S)
if cls_ is None:
print("BRUH")
continue
result += f" \"hikka.modules.{mod.path.split('/')[-1].split('.')[0]}_cls_doc\": \"{cls_.group(1)}\",\n"
result += "}"
print(result)

View File

@ -54,6 +54,7 @@ class Web:
self.app.router.add_post("/sendTgCode", self.send_tg_code) self.app.router.add_post("/sendTgCode", self.send_tg_code)
self.app.router.add_post("/check_session", self.check_session) self.app.router.add_post("/check_session", self.check_session)
self.app.router.add_post("/web_auth", self.web_auth) self.app.router.add_post("/web_auth", self.web_auth)
self.app.router.add_post("/okteto", self.okteto)
self.app.router.add_post("/tgCode", self.tg_code) self.app.router.add_post("/tgCode", self.tg_code)
self.app.router.add_post("/finishLogin", self.finish_login) self.app.router.add_post("/finishLogin", self.finish_login)
self.api_set = asyncio.Event() self.api_set = asyncio.Event()
@ -132,6 +133,14 @@ class Web:
self.sign_in_clients[phone] = client self.sign_in_clients[phone] = client
return web.Response() return web.Response()
async def okteto(self, request):
if not self._check_session(request):
return web.Response(body="WAIT")
text = await request.text()
main.hikka.okteto_uri = text
return web.Response(body="URI_SAVED")
async def tg_code(self, request): async def tg_code(self, request):
if not self._check_session(request): if not self._check_session(request):
return web.Response(status=401) return web.Response(status=401)

View File

@ -170,4 +170,7 @@
</script> </script>
<script src="{{ static("root.js") }}"></script> <script src="{{ static("root.js") }}"></script>
<script src="{{ static("blackhole.js") }}"></script> <script src="{{ static("blackhole.js") }}"></script>
{% if okteto %}
<script src="{{ static("okteto.js") }}"></script>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -0,0 +1,13 @@
function okteto() {
fetch("/okteto", {
method: "POST",
credentials: "include",
body: window.location.href
})
.then(response => response.text())
.then((response) => {
if(response == "WAIT") {
setTimeout(() => {okteto();}, 5000);
}
})
}

View File

@ -14,7 +14,7 @@ function auth(callback) {
fetch("/web_auth", { fetch("/web_auth", {
method: "POST", method: "POST",
credentials: "include", credentials: "include",
timeout: 5000 timeout: 300000
}) })
.then(response => response.text()) .then(response => response.text())
.then((response) => { .then((response) => {