mirror of
https://github.com/Nick80835/microbot
synced 2025-08-23 02:28:31 +00:00
120 lines
4.1 KiB
Python
120 lines
4.1 KiB
Python
|
# Copyright (C) 2019 Nick Filmer (nick80835@gmail.com)
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU Affero General Public License as
|
||
|
# published by the Free Software Foundation, either version 3 of the
|
||
|
# License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU Affero General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Affero General Public License
|
||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
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
|