vish790 commited on
Commit
4c7bd5b
Β·
verified Β·
1 Parent(s): 37830e3

Create _bot.py

Browse files
Files changed (1) hide show
  1. plugins/_bot.py +267 -0
plugins/_bot.py ADDED
@@ -0,0 +1,267 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultroid - UserBot
2
+ # Copyright (C) 2021-2025 TeamUltroid
3
+ #
4
+ # This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
5
+ # PLease read the GNU Affero General Public License in
6
+ # <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
7
+
8
+ from . import get_help
9
+
10
+ __doc__ = get_help("help_bot")
11
+
12
+ import os
13
+ import sys
14
+ import time
15
+ from platform import python_version as pyver
16
+ from random import choice
17
+
18
+ from telethon import __version__
19
+ from telethon.errors.rpcerrorlist import (
20
+ BotMethodInvalidError,
21
+ ChatSendMediaForbiddenError,
22
+ )
23
+
24
+ from xteam.version import __version__ as UltVer
25
+
26
+ from . import HOSTED_ON, LOGS
27
+
28
+ try:
29
+ from git import Repo
30
+ except ImportError:
31
+ LOGS.error("bot: 'gitpython' module not found!")
32
+ Repo = None
33
+
34
+ from telethon.utils import resolve_bot_file_id
35
+ from xteam.configs import Var
36
+ from . import (
37
+ ATRA_COL,
38
+ LOGS,
39
+ OWNER_NAME,
40
+ ULTROID_IMAGES,
41
+ Button,
42
+ Carbon,
43
+ Telegraph,
44
+ allcmds,
45
+ asst,
46
+ bash,
47
+ call_back,
48
+ callback,
49
+ def_logs,
50
+ eor,
51
+ get_string,
52
+ heroku_logs,
53
+ in_pattern,
54
+ inline_pic,
55
+ restart,
56
+ shutdown,
57
+ start_time,
58
+ time_formatter,
59
+ udB,
60
+ ultroid_cmd,
61
+ ultroid_version,
62
+ updater,
63
+ )
64
+
65
+
66
+ def ULTPIC():
67
+ return inline_pic() or choice(ULTROID_IMAGES)
68
+
69
+
70
+ buttons = [
71
+ [
72
+ Button.url(get_string("bot_3"), "https://github.com/TeamUltroid/Ultroid"),
73
+ Button.url(get_string("bot_4"), "t.me/UltroidSupportChat"),
74
+ ]
75
+ ]
76
+
77
+ # Will move to strings
78
+ alive_txt = """
79
+ α΄œκœ±α΄‡Κ€Κ™α΄α΄› Ιͺꜱ α΄€ΚŸΙͺᴠᴇ
80
+ ◍ α΄œκœ±α΄‡Κ€Κ™α΄α΄› - {}
81
+ ◍ α΄…α΄€α΄›α΄€Κ™α΄€κœ±α΄‡ - {}
82
+ ◍ α΄›α΄‡ΚŸα΄‡α΄›Κœα΄Ι΄ - {}
83
+ """
84
+
85
+ in_alive = "{}\n\nπŸŒ€ <b>Ultroid Version -><b> <code>{}</code>\nπŸŒ€ <b>xteam -></b> <code>{}</code>\nπŸŒ€ <b>Python -></b> <code>{}</code>\nπŸŒ€ <b>Uptime -></b> <code>{}</code>\nπŸŒ€ <b>Branch -></b>[ {} ]\n\nβ€’ <b>Join @TeamUltroid</b>"
86
+
87
+
88
+ @callback("alive")
89
+ async def alive(event):
90
+ text = alive_txt.format(ultroid_version, UltVer, __version__)
91
+ await event.answer(text, alert=True)
92
+
93
+
94
+ @ultroid_cmd(
95
+ pattern="cmds$",
96
+ )
97
+ async def cmds(event):
98
+ await allcmds(event, Telegraph)
99
+
100
+
101
+ heroku_api = Var.HEROKU_API
102
+
103
+
104
+ @ultroid_cmd(
105
+ pattern="restart$",
106
+ fullsudo=False,
107
+ )
108
+ async def restart(e):
109
+ await e.eor("`Processing...`")
110
+ await bash("git pull && pip3 install -r requirements.txt")
111
+ os.execl(sys.executable, sys.executable, "-m", "xteam")
112
+
113
+ @ultroid_cmd(
114
+ pattern="Restart$",
115
+ fullsudo=False,
116
+ )
117
+ async def restart(e):
118
+ await e.eor("`Processing...`")
119
+ await bash("git pull")
120
+ await e.eor("Done.")
121
+ os.execl(sys.executable, sys.executable, "-m", "xteam")
122
+
123
+ @ultroid_cmd(
124
+ pattern="shutdown$",
125
+ fullsudo=True,
126
+ )
127
+ async def shutdownbot(ult):
128
+ await shutdown(ult)
129
+
130
+
131
+ @ultroid_cmd(
132
+ pattern="logs( (.*)|$)",
133
+ chats=[],
134
+ )
135
+ async def _(event):
136
+ opt = event.pattern_match.group(1).strip()
137
+ file = f"userbot{sys.argv[-1]}.log" if len(sys.argv) > 1 else "userbot.log"
138
+ if opt == "heroku":
139
+ await heroku_logs(event)
140
+ elif opt == "carbon" and Carbon:
141
+ event = await event.eor(get_string("com_1"))
142
+ with open(file, "r") as f:
143
+ code = f.read()[-2500:]
144
+ file = await Carbon(
145
+ file_name="userbot-logs",
146
+ code=code,
147
+ backgroundColor=choice(ATRA_COL),
148
+ )
149
+ if isinstance(file, dict):
150
+ await event.eor(f"`{file}`")
151
+ return
152
+ await event.reply("**Userbot Logs.**", file=file)
153
+ elif opt == "open":
154
+ with open("userbot.log", "r") as f:
155
+ file = f.read()[-4000:]
156
+ return await event.eor(f"`{file}`")
157
+ elif (
158
+ opt.isdigit() and 5 <= int(opt) <= 100
159
+ ): # Check if input is a number between 10 and 100
160
+ num_lines = int(opt)
161
+ with open("userbot.log", "r") as f:
162
+ lines = f.readlines()[-num_lines:]
163
+ file = "".join(lines)
164
+ return await event.eor(f"`{file}`")
165
+ else:
166
+ await def_logs(event, file)
167
+ await event.try_delete()
168
+
169
+
170
+ @in_pattern("alive", owner=True)
171
+ async def inline_alive(ult):
172
+ pic = udB.get_key("ALIVE_PIC")
173
+ if isinstance(pic, list):
174
+ pic = choice(pic)
175
+ uptime = time_formatter((time.time() - start_time) * 1000)
176
+ header = udB.get_key("ALIVE_TEXT") or get_string("bot_1")
177
+ y = Repo().active_branch
178
+ xx = Repo().remotes[0].config_reader.get("url")
179
+ rep = xx.replace(".git", f"/tree/{y}")
180
+ kk = f"<a href={rep}>{y}</a>"
181
+ als = in_alive.format(
182
+ header, f"{ultroid_version} [{HOSTED_ON}]", UltVer, pyver(), uptime, kk
183
+ )
184
+
185
+ if _e := udB.get_key("ALIVE_EMOJI"):
186
+ als = als.replace("πŸŒ€", _e)
187
+ builder = ult.builder
188
+ if pic:
189
+ try:
190
+ if ".jpg" in pic:
191
+ results = [
192
+ await builder.photo(
193
+ pic, text=als, parse_mode="html", buttons=buttons
194
+ )
195
+ ]
196
+ else:
197
+ if _pic := resolve_bot_file_id(pic):
198
+ pic = _pic
199
+ buttons.insert(
200
+ 0, [Button.inline(get_string("bot_2"), data="alive")]
201
+ )
202
+ results = [
203
+ await builder.document(
204
+ pic,
205
+ title="Inline Alive",
206
+ description="@TeamUltroid",
207
+ parse_mode="html",
208
+ buttons=buttons,
209
+ )
210
+ ]
211
+ return await ult.answer(results)
212
+ except BaseException as er:
213
+ LOGS.exception(er)
214
+ result = [
215
+ await builder.article(
216
+ "Alive", text=als, parse_mode="html", link_preview=False, buttons=buttons
217
+ )
218
+ ]
219
+ await ult.answer(result)
220
+
221
+
222
+ @ultroid_cmd(pattern="update( (.*)|$)")
223
+ async def _(e):
224
+ xx = await e.eor(get_string("upd_1"))
225
+ if e.pattern_match.group(1).strip() and (
226
+ "fast" in e.pattern_match.group(1).strip()
227
+ or "soft" in e.pattern_match.group(1).strip()
228
+ ):
229
+ await bash("git pull -f && pip3 install -r requirements.txt")
230
+ call_back()
231
+ await xx.edit(get_string("upd_7"))
232
+ os.execl(sys.executable, "python3", "-m", "xteam")
233
+ # return
234
+ m = await updater()
235
+ branch = (Repo.init()).active_branch
236
+ if m:
237
+ x = await asst.send_file(
238
+ udB.get_key("LOG_CHANNEL"),
239
+ ULTPIC(),
240
+ caption="β€’ **Update Available** β€’",
241
+ force_document=False,
242
+ buttons=Button.inline("Changelogs", data="changes"),
243
+ )
244
+ Link = x.message_link
245
+ await xx.edit(
246
+ f'<strong><a href="{Link}">[ChangeLogs]</a></strong>',
247
+ parse_mode="html",
248
+ link_preview=False,
249
+ )
250
+ else:
251
+ await xx.edit(
252
+ f'<code>Your BOT is </code><strong>up-to-date</strong><code> with </code><strong><a href="https://github.com/TeamUltroid/Ultroid/tree/{branch}">[{branch}]</a></strong>',
253
+ parse_mode="html",
254
+ link_preview=False,
255
+ )
256
+
257
+
258
+ @callback("updtavail", owner=True)
259
+ async def updava(event):
260
+ await event.delete()
261
+ await asst.send_file(
262
+ udB.get_key("LOG_CHANNEL"),
263
+ ULTPIC(),
264
+ caption="β€’ **Update Available** β€’",
265
+ force_document=False,
266
+ buttons=Button.inline("Changelogs", data="changes"),
267
+ )