2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-31 14:38:04 +00:00

stuff and things and stuff

This commit is contained in:
Nick80835
2020-08-24 17:02:08 -04:00
parent b16bdbb366
commit cad3d8005a
4 changed files with 43 additions and 39 deletions

View File

@@ -10,6 +10,7 @@ howdoi
pillow pillow
psutil psutil
speedtest-cli speedtest-cli
sqlalchemy
telethon telethon
Pafy Pafy
youtube-dl youtube-dl

View File

@@ -1,30 +1,30 @@
# SPDX-License-Identifier: GPL-2.0-or-later # SPDX-License-Identifier: GPL-2.0-or-later
import json import json
import sqlite3
from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine
class Database(): class Database():
db_conn = sqlite3.connect("database.sqlite") engine = create_engine('sqlite:///database.sqlite')
metadata = MetaData()
def __init__(self): chats = Table(
cur = self.db_conn.cursor() "chats",
cur.execute( metadata,
"""CREATE TABLE IF NOT EXISTS chats ( Column("id", Integer, primary_key=True),
id integer PRIMARY KEY, Column("disabled_commands", String, nullable=False)
disabled_commands text NOT NULL )
);"""
)
def ensure_chat_table(self, chat_id: int): metadata.create_all(engine)
cur = self.db_conn.cursor() conn = engine.connect()
cur.execute("INSERT OR IGNORE INTO chats (id, disabled_commands) VALUES (?, ?);", [str(chat_id), "[]"])
self.db_conn.commit() def ensure_disabled_commands_table(self, chat_id: int):
self.conn.execute(f"INSERT OR IGNORE INTO chats (id, disabled_commands) VALUES (?, ?);", (chat_id, "[]"))
def get_disabled_commands(self, chat_id: int) -> list: def get_disabled_commands(self, chat_id: int) -> list:
self.ensure_chat_table(chat_id) self.ensure_disabled_commands_table(chat_id)
cur = self.db_conn.cursor() disabled_raw = self.conn.execute(f"SELECT disabled_commands FROM chats WHERE id = ?;", (chat_id, )).fetchone()
disabled_raw = cur.execute("SELECT disabled_commands FROM chats WHERE id = ?;", [str(chat_id)]).fetchone()
return json.loads(disabled_raw[0] if disabled_raw else "[]") return json.loads(disabled_raw[0] if disabled_raw else "[]")
def disable_command(self, chat_id: int, command: str): def disable_command(self, chat_id: int, command: str):
@@ -33,9 +33,7 @@ class Database():
if command not in disabled_commands: if command not in disabled_commands:
disabled_commands.append(command) disabled_commands.append(command)
new_disabled_commands = json.dumps(disabled_commands) new_disabled_commands = json.dumps(disabled_commands)
cur = self.db_conn.cursor() self.conn.execute("UPDATE chats SET disabled_commands = ? WHERE id = ?;", (new_disabled_commands, chat_id))
cur.execute("UPDATE chats SET disabled_commands = ? WHERE id = ?;", [new_disabled_commands, str(chat_id)])
self.db_conn.commit()
def enable_command(self, chat_id: int, command: str): def enable_command(self, chat_id: int, command: str):
disabled_commands = self.get_disabled_commands(chat_id) disabled_commands = self.get_disabled_commands(chat_id)
@@ -43,6 +41,4 @@ class Database():
if command in disabled_commands: if command in disabled_commands:
disabled_commands.remove(command) disabled_commands.remove(command)
new_disabled_commands = json.dumps(disabled_commands) new_disabled_commands = json.dumps(disabled_commands)
cur = self.db_conn.cursor() self.conn.execute(f"UPDATE chats SET disabled_commands = ? WHERE id = ?;", (new_disabled_commands, chat_id))
cur.execute("UPDATE chats SET disabled_commands = ? WHERE id = ?;", [new_disabled_commands, str(chat_id)])
self.db_conn.commit()

View File

@@ -64,18 +64,19 @@ class Loader():
def add(self, pattern: str = None, **args): def add(self, pattern: str = None, **args):
pattern = args.get("pattern", pattern) pattern = args.get("pattern", pattern)
pattern_extra = args.get("pattern_extra", "") pattern_extra = args.get("pattern_extra", "")
not_disableable = args.get("no_disable", False) or args.get('owner', False) or args.get('sudo', False) or args.get('admin', False)
def decorator(func): def decorator(func):
if args.get("hide_help", False): if args.get("hide_help", False):
if func.__module__.split(".")[-1] in self.help_hidden_dict: if func.__module__.split(".")[-1] in self.help_hidden_dict:
self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
else: else:
if func.__module__.split(".")[-1] in self.help_dict: if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
self.command_handler.incoming_commands.append({ self.command_handler.incoming_commands.append({
"pattern": pattern, "pattern": pattern,
@@ -104,19 +105,20 @@ class Loader():
def add_list(self, pattern: list = None, **args): def add_list(self, pattern: list = None, **args):
pattern_list = args.get("pattern", pattern) pattern_list = args.get("pattern", pattern)
pattern_extra = args.get("pattern_extra", "") pattern_extra = args.get("pattern_extra", "")
not_disableable = args.get("no_disable", False) or args.get('owner', False) or args.get('sudo', False) or args.get('admin', False)
def decorator(func): def decorator(func):
for pattern in pattern_list: for pattern in pattern_list:
if args.get("hide_help", False): if args.get("hide_help", False):
if func.__module__.split(".")[-1] in self.help_hidden_dict: if func.__module__.split(".")[-1] in self.help_hidden_dict:
self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
else: else:
if func.__module__.split(".")[-1] in self.help_dict: if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
self.command_handler.incoming_commands.append({ self.command_handler.incoming_commands.append({
"pattern": pattern, "pattern": pattern,
@@ -145,19 +147,20 @@ class Loader():
def add_dict(self, pattern: dict = None, **args): def add_dict(self, pattern: dict = None, **args):
pattern_dict = args.get("pattern", pattern) pattern_dict = args.get("pattern", pattern)
pattern_extra = args.get("pattern_extra", "") pattern_extra = args.get("pattern_extra", "")
not_disableable = args.get("no_disable", False) or args.get('owner', False) or args.get('sudo', False) or args.get('admin', False)
def decorator(func): def decorator(func):
for pattern, extra in pattern_dict.items(): for pattern, extra in pattern_dict.items():
if args.get("hide_help", False): if args.get("hide_help", False):
if func.__module__.split(".")[-1] in self.help_hidden_dict: if func.__module__.split(".")[-1] in self.help_hidden_dict:
self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_hidden_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
else: else:
if func.__module__.split(".")[-1] in self.help_dict: if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None), not_disableable]]
else: else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]] self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None), not_disableable]]
self.command_handler.incoming_commands.append({ self.command_handler.incoming_commands.append({
"pattern": pattern, "pattern": pattern,

View File

@@ -79,10 +79,14 @@ async def bot_repo(event):
async def disable_command(event): async def disable_command(event):
if event.args: if event.args:
for value in ldr.help_dict.values(): for value in ldr.help_dict.values():
for info in [i[0] for i in value]: for info in value:
if event.args == info: if event.args == info[0]:
await event.reply(f"Disabling **{info}** in chat **{event.chat.id}**!") if info[2]:
ldr.db.disable_command(event.chat.id, info) await event.reply(f"**{info[0]}** cannot be disabled!")
return
await event.reply(f"Disabling **{info[0]}** in chat **{event.chat.id}**!")
ldr.db.disable_command(event.chat.id, info[0])
return return
await event.reply(f"**{event.args}** is not a command!") await event.reply(f"**{event.args}** is not a command!")