mirror of
https://github.com/Nick80835/microbot
synced 2025-08-29 05:29:16 +00:00
remove database stuff and notes
This commit is contained in:
parent
1d0bae96f2
commit
9700c258ef
@ -1,6 +1,5 @@
|
||||
aiohttp
|
||||
cryptg
|
||||
databases[sqlite]
|
||||
hachoir
|
||||
pillow
|
||||
praw
|
||||
|
@ -1,37 +0,0 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from databases import Database as db
|
||||
|
||||
|
||||
class Database():
|
||||
def __init__(self, client):
|
||||
self.db = db("sqlite:///database.db")
|
||||
client.loop.run_until_complete(self.db.connect())
|
||||
|
||||
async def ensure_table(self, table, columns):
|
||||
await self.db.execute(f"create table if not exists {table} ({' TEXT, '.join(columns) + ' TEXT'})")
|
||||
return ", ".join(columns)
|
||||
|
||||
async def single_row_write(self, table, columns, row, value):
|
||||
column_string = await self.ensure_table(table, columns)
|
||||
await self.db.execute(f"delete from {table} where {columns[0]} = '{row}'")
|
||||
await self.db.execute(f"insert into {table}({column_string}) values ('{row}', '{value}')")
|
||||
|
||||
async def single_row_delete(self, table, columns, row):
|
||||
await self.ensure_table(table, columns)
|
||||
await self.db.execute(f"delete from {table} where {columns[0]} = '{row}'")
|
||||
|
||||
async def single_column_readall(self, table, columns, row):
|
||||
await self.ensure_table(table, columns)
|
||||
fetched_tuple = await self.db.fetch_all(f"select {row} from {table}")
|
||||
fetched_list = [item[0] for item in fetched_tuple]
|
||||
return fetched_list
|
||||
|
||||
async def single_row_read(self, table, columns, row):
|
||||
await self.ensure_table(table, columns)
|
||||
content = await self.db.fetch_one(f"select {columns[1]} from {table} where {columns[0]} = '{row}'")
|
||||
|
||||
if content:
|
||||
return content[0]
|
||||
else:
|
||||
return None
|
@ -8,7 +8,6 @@ from telethon.errors.rpcerrorlist import PhoneNumberInvalidError
|
||||
from telethon.network.connection.tcpabridged import \
|
||||
ConnectionTcpAbridged as CTA
|
||||
|
||||
from .database import Database
|
||||
from .loader import Loader
|
||||
from .settings import Settings
|
||||
|
||||
@ -23,19 +22,14 @@ class MicroBot():
|
||||
self.settings = Settings()
|
||||
self.logger = None
|
||||
self.loader = None
|
||||
self.database = None
|
||||
|
||||
def start_microbot(self):
|
||||
self.start_logger()
|
||||
self.start_client()
|
||||
self.start_database()
|
||||
self.start_loader()
|
||||
self.loader.load_all_modules()
|
||||
self.client.run_until_disconnected()
|
||||
|
||||
def start_database(self):
|
||||
self.database = Database(self.client)
|
||||
|
||||
def start_loader(self):
|
||||
self.loader = Loader(self.client, self.logger, self.settings)
|
||||
|
||||
|
@ -1,106 +0,0 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
db = micro_bot.database
|
||||
|
||||
note_columns = ["notename", "notecontent"]
|
||||
|
||||
|
||||
@ldr.add(pattern="save")
|
||||
async def savenote(event):
|
||||
notename, notecontent = await get_text_arg(event)
|
||||
|
||||
if not notename or not notecontent:
|
||||
await event.edit("`Provide both a note name and content to save!`")
|
||||
return
|
||||
|
||||
await event.edit(f"`Saving to note `**{notename}**`…`")
|
||||
await db.single_row_write("Notes", note_columns, notename, notecontent)
|
||||
await event.edit(f"`Successfully saved to note `**{notename}**`!`")
|
||||
|
||||
|
||||
@ldr.add(pattern="get")
|
||||
async def getnote(event):
|
||||
notename = event.pattern_match.group(1).replace(" ", "_")
|
||||
|
||||
if not notename:
|
||||
notelist = '\n'.join(await db.single_column_readall("Notes", note_columns, "notename"))
|
||||
|
||||
if notelist:
|
||||
await event.edit(f"`Provide a note name to get its content, saved notes:`\n**{notelist}**")
|
||||
return
|
||||
else:
|
||||
await event.edit(f"`You haven't saved any notes!\nUse `**{micro_bot.settings.get_config('cmd_prefix') or '.'}save**` to save notes.`")
|
||||
return
|
||||
|
||||
notecontent = await db.single_row_read("Notes", note_columns, notename)
|
||||
|
||||
if notecontent:
|
||||
await event.edit(f"{notecontent}")
|
||||
else:
|
||||
notelist = '\n'.join(await db.single_column_readall("Notes", note_columns, "notename"))
|
||||
|
||||
if notelist:
|
||||
await event.edit(f"`Note `**{notename}**` not found, saved notes:`\n**{notelist}**")
|
||||
else:
|
||||
await event.edit(f"`You haven't saved any notes!\nUse `**{micro_bot.settings.get_config('cmd_prefix') or '.'}save**` to save notes.`")
|
||||
|
||||
|
||||
@ldr.add(incoming=True, noprefix=True, pattern="#(.*)")
|
||||
async def getnoteincoming(event):
|
||||
if not micro_bot.settings.get_bool("incoming_allowed"):
|
||||
return
|
||||
|
||||
notename = event.pattern_match.group(0).replace(" ", "_").lstrip("#")
|
||||
|
||||
if not notename:
|
||||
notelist = '\n'.join(await db.single_column_readall("Notes", note_columns, "notename"))
|
||||
|
||||
if notelist:
|
||||
await event.reply(f"`Provide a note name to get its content, saved notes:`\n**{notelist}**")
|
||||
return
|
||||
else:
|
||||
await event.reply(f"`There aren't any saved notes!`")
|
||||
return
|
||||
|
||||
notecontent = await db.single_row_read("Notes", note_columns, notename)
|
||||
|
||||
if notecontent:
|
||||
await event.reply(f"{notecontent}")
|
||||
else:
|
||||
notelist = '\n'.join(await db.single_column_readall("Notes", note_columns, "notename"))
|
||||
|
||||
if notelist:
|
||||
await event.reply(f"`Note `**{notename}**` not found, saved notes:`\n**{notelist}**")
|
||||
else:
|
||||
await event.reply(f"`There aren't any saved notes!`")
|
||||
|
||||
|
||||
@ldr.add(pattern="del")
|
||||
async def delnote(event):
|
||||
notename = event.pattern_match.group(1).replace(" ", "_")
|
||||
|
||||
if not notename:
|
||||
await event.edit("`I need a notes name to delete it!`")
|
||||
return
|
||||
|
||||
await db.single_row_delete("Notes", note_columns, notename)
|
||||
await event.edit(f"`Note `**{notename}**` successfully deleted!`")
|
||||
|
||||
|
||||
async def get_text_arg(event):
|
||||
text_arg = event.pattern_match.group(1)
|
||||
notename = None
|
||||
notecontent = None
|
||||
|
||||
if event.is_reply and text_arg:
|
||||
reply = await event.get_reply_message()
|
||||
notename = text_arg.replace(" ", "_")
|
||||
notecontent = reply.text
|
||||
elif text_arg:
|
||||
notename = text_arg.split(" ")[0]
|
||||
notecontent = " ".join(text_arg.split(" ")[1:])
|
||||
|
||||
return notename, notecontent
|
Loading…
x
Reference in New Issue
Block a user