forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
notes.py
116 lines (100 loc) · 4.72 KB
/
notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# 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/>.
import logging
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class NotesMod(loader.Module):
"""Stores global notes (aka snips)"""
strings = {"name": "Notes",
"what_note": "<b>What note should be retrieved?</b>",
"no_note": "<b>Note not found</b>",
"save_what": "<b>You must reply to a message to save it to a note, or type the note.</b>",
"what_name": "<b>You must specify what the note should be called?</b>",
"saved": "<b>Note saved</b>",
"notes_header": "<b>Saved notes:</b>\n\n",
"notes_item": "<b>•</b> <code>{}</code>",
"delnote_args": "<b>What note should be deleted?</b>",
"delnote_done": "<b>Note deleted</b>",
"delnotes_none": "<b>There are no notes to be cleared</b>",
"delnotes_done": "<b>All notes cleared</b>",
"notes_none": "<b>There are no saved notes</b>"}
async def notecmd(self, message):
"""Gets the note specified"""
args = utils.get_args(message)
if not args:
await utils.answer(message, self.strings("what_note", message))
return
asset_id = self._db.get(__name__, "notes", {}).get(args[0], None)
logger.debug(asset_id)
if asset_id is not None:
asset = await self._db.fetch_asset(asset_id)
else:
asset = None
if asset is None:
self.del_note(args[0])
await utils.answer(message, self.strings("no_note", message))
return
await utils.answer(message, await self._db.fetch_asset(asset_id))
async def delallnotescmd(self, message):
"""Deletes all the saved notes"""
if not self._db.get(__name__, "notes", {}):
await utils.answer(message, self.strings("delnotes_none", message))
return
self._db.get(__name__, "notes", {}).clear()
await utils.answer(message, self.strings("delnotes_done", message))
async def savecmd(self, message):
"""Save a new note. Must be used in reply with one parameter (note name)"""
args = utils.get_args(message)
if not args:
await utils.answer(message, self.strings("what_name", message))
return
if not message.is_reply:
if len(args) < 2:
await utils.answer(message, self.strings("save_what", message))
return
else:
message.entities = None
message.message = args[1]
target = message
logger.debug(target.message)
else:
target = await message.get_reply_message()
asset_id = await self._db.store_asset(target)
self._db.set(__name__, "notes", {**self._db.get(__name__, "notes", {}), args[0]: asset_id})
await utils.answer(message, self.strings("saved", message))
async def delnotecmd(self, message):
"""Deletes a note, specified by note name"""
args = utils.get_args(message)
if not args:
await utils.answer(message, self.strings("delnote_args", message))
self.del_note(args[0])
await utils.answer(message, self.strings("delnote_done", message))
def del_note(self, note):
old = self._db.get(__name__, "notes", {})
try:
del old[note]
except KeyError:
pass
else:
self._db.set(__name__, "notes", old)
async def notescmd(self, message):
"""List the saved notes"""
if not self._db.get(__name__, "notes", {}):
await utils.answer(message, self.strings("notes_none", message))
return
await utils.answer(message, self.strings("notes_header", message)
+ "\n".join(self.strings("notes_item", message).format(key)
for key in self._db.get(__name__, "notes", {})))
async def client_ready(self, client, db):
self._db = db