-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEditor.py
More file actions
118 lines (98 loc) · 3.93 KB
/
Copy pathMessageEditor.py
File metadata and controls
118 lines (98 loc) · 3.93 KB
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
117
118
# CopyLeft 2026 github.com/i-execute
# Author: I_execute.t.me
# Licensed under AGPLv3.
__version__ = (1, 0, 1)
# meta developer: Execute_forge.t.me
from telethon.tl.types import Message
from .. import loader, utils
import asyncio
@loader.tds
class MessageEditor(loader.Module):
"""Edit messages with formatting"""
strings = {
"name": "MessageEditor",
"no_reply": "Use this command in reply to a message",
"not_owner": "Are you serious?",
"edit_failed": "Message can no longer be edited",
}
strings_ru = {
"no_reply": "Используй команду в реплай на сообщение",
"not_owner": "Ты серьезно?",
"edit_failed": "Сообщение уже нельзя редактировать",
}
async def _check_and_edit(self, message: Message, entity_type):
if not message.is_reply:
msg = await utils.answer(message, self.strings["no_reply"])
await asyncio.sleep(5)
await msg.delete()
return
reply = await message.get_reply_message()
me = await message.client.get_me()
if reply.sender_id != me.id:
msg = await utils.answer(message, self.strings["not_owner"])
await asyncio.sleep(5)
await msg.delete()
return
try:
from telethon.tl.types import (
MessageEntityBold,
MessageEntityCode,
MessageEntityPre,
MessageEntityBlockquote,
)
text = reply.message or ""
entities = list(reply.entities) if reply.entities else []
if entity_type == "bold":
new_entity = MessageEntityBold(offset=0, length=len(text))
elif entity_type == "code":
new_entity = MessageEntityCode(offset=0, length=len(text))
elif entity_type == "pre":
new_entity = MessageEntityPre(offset=0, length=len(text), language="")
elif entity_type == "quote":
new_entity = MessageEntityBlockquote(offset=0, length=len(text))
elif entity_type == "quote_expandable":
new_entity = MessageEntityBlockquote(offset=0, length=len(text), collapsed=True)
else:
return
entities.insert(0, new_entity)
await reply.edit(text, formatting_entities=entities)
await message.delete()
except Exception as e:
msg = await utils.answer(message, f"{self.strings['edit_failed']}: {str(e)}")
await asyncio.sleep(5)
await msg.delete()
@loader.command(
ru_doc="Сделать текст жирным",
en_doc="Make text bold",
)
async def b(self, message: Message):
"""Make text bold"""
await self._check_and_edit(message, "bold")
@loader.command(
ru_doc="Сделать текст кодом",
en_doc="Make text code",
)
async def c(self, message: Message):
"""Make text code"""
await self._check_and_edit(message, "pre")
@loader.command(
ru_doc="Сделать текст моноширинным",
en_doc="Make text monospace",
)
async def m(self, message: Message):
"""Make text monospace"""
await self._check_and_edit(message, "code")
@loader.command(
ru_doc="Сделать текст цитатой",
en_doc="Make text quote",
)
async def q(self, message: Message):
"""Make text quote"""
await self._check_and_edit(message, "quote")
@loader.command(
ru_doc="Сделать текст сворачиваемой цитатой",
en_doc="Make text expandable quote",
)
async def qe(self, message: Message):
"""Make text expandable quote"""
await self._check_and_edit(message, "quote_expandable")