-
Notifications
You must be signed in to change notification settings - Fork 1
/
snips.py
130 lines (120 loc) · 4.59 KB
/
snips.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from telethon import events
from d3vilbot.sql import snip_sql as sq
from . import *
lg_id = Config .LOGGER_ID
@bot.on(events.NewMessage(pattern=r'\#(\S+)', outgoing=True))
async def incom_note(event):
if not lg_id:
return
try:
if not (await event.get_sender()).bot:
notename = event.text[1:]
notename = notename.lower()
note = sq.get_note(notename)
message_id_to_reply = await reply_id(event)
if note:
if note.f_mesg_id:
msg_o = await event.client.get_messages(
entity=lg_id, ids=int(note.f_mesg_id)
)
await event.delete()
await event.client.send_message(
event.chat_id,
msg_o,
reply_to=message_id_to_reply,
link_preview=False,
)
elif note.reply:
await event.delete()
await event.client.send_message(
event.chat_id,
note.reply,
reply_to=message_id_to_reply,
link_preview=False,
)
except AttributeError:
pass
@bot.on(d3vil_cmd(pattern=r"snip ?(.*)"))
@bot.on(sudo_cmd(pattern=r"snip ?(.*)", allow_sudo=True))
async def add_snip(event):
if not lg_id:
return await eod(
event, "You need to setup `LOGGER_ID` to save snips..."
)
trigger = event.pattern_match.group(1)
stri = event.text.partition(trigger)[2]
cht = await event.get_reply_message()
cht_id = None
trigger = trigger.lower()
if cht and not stri:
await event.client.send_message(
lg_id,
f"#NOTE \n\nAdded Note with `#{trigger}`. Below message is the output. \n**DO NOT DELETE IT**",
)
cht_o = await event.client.forward_messages(
entity=lg_id, messages=cht, from_peer=event.chat_id, silent=True
)
cht_id = cht_o.id
elif cht:
return await eod(event, f"**ERROR** \nReply to a message with `{hl}snip <trigger>` to add snips...")
if not cht:
if stri:
await event.client.send_message(
lg_id,
f"#NOTE \n\nAdded Note with `#{trigger}`. Below message is the output. \n**DO NOT DELETE IT**",
)
cht_o = await event.client.send_message(lg_id, stri)
cht_id = cht_o.id
stri = None
else:
return await eod(event, f"Invalid Syntax. Check `{hl}plinfo snips` to get proper Syntax.")
success = "**Successfully {} snip with trigger** `#{}` "
if sq.add_note(trigger, stri, cht_id) is False:
sq.rm_note(trigger)
if sq.add_note(trigger, stri, cht_id) is False:
return await edit_or_reply(
event, f"Error Adding Snip.."
)
return await eor(event, success.format("updated", trigger))
return await eor(event, success.format("added", trigger))
@bot.on(d3vil_cmd(pattern=r"rmsnip ?(.*)"))
@bot.on(sudo_cmd(pattern=r"rmsnip ?(.*)", allow_sudo=True))
async def _(event):
input_str = (event.pattern_match.group(1)).lower()
if not input_str:
return await eod(e, "I need a snip name to remove...")
if input_str.startswith("#"):
input_str = input_str.replace("#", "")
sq.rm_note(input_str)
await eor(event, "Removed `#{}` from snips..".format(input_str))
@bot.on(d3vil_cmd(pattern="listsnip"))
@bot.on(sudo_cmd(pattern="listsnip", allow_sudo=True))
async def lsnote(event):
all_snips = sq.get_notes()
OUT_STR = "Available Snips:\n"
if len(all_snips) > 0:
for a_snip in all_snips:
OUT_STR += f"👉 #{a_snip.keyword} \n"
else:
OUT_STR = f"No Snips. Start Saving using `{hl}snips`"
if len(OUT_STR) > 4000:
with io.BytesIO(str.encode(OUT_STR)) as out_file:
out_file.name = "snips.text"
await bot.send_file(
event.chat_id,
out_file,
force_document=True,
allow_cache=False,
caption="Available Snips",
reply_to=event
)
await event.delete()
else:
await edit_or_reply(event, OUT_STR)
CmdHelp("snips").add_command(
"snip", "<reply> <trigger>", "Saves the replied message as a note with given trigger."
).add_command(
"rmsnip", "<trigger>", "Removes the snip from your database."
).add_command(
"listsnip", None, "Get the list of all of the available snips."
).add()