-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
374 lines (307 loc) · 11.6 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import asyncio
import atexit
import logging
import os
import signal
import threading
import time
from datetime import datetime, timedelta
from pathlib import Path
import aiohttp
import dateutil.parser as dp
import disnake
import pytz
import requests
from disnake import Webhook
from disnake.ext import commands
from dotenv import load_dotenv
from keep_alive import keep_alive
from running_check import check
start_time = time.time()
print(f"Running Disnake {disnake.__version__}")
logging.getLogger("asyncio").setLevel(logging.DEBUG)
load_dotenv()
print(f"Asyncio Debug Mode: {os.getenv('PYTHONASYNCIODEBUG')}")
def goodbye(a=None, b=None):
print("Exiting...")
with open("log.txt", "a", encoding="utf-8") as f:
f.write("Exiting...\n")
atexit.register(goodbye)
signal.signal(signal.SIGTERM, goodbye)
signal.signal(signal.SIGINT, goodbye)
# signal.signal(signal.SIGKILL, goodbye)
# signal.signal(signal.SIGSTOP, goodbye)
signal.signal(signal.SIGHUP, goodbye)
def rate_limit_check():
headers = {"Authorization": f"Bot {os.getenv('TOKEN')}"}
r = requests.get(
url="https://discord.com/api/v9/channels/809247468084133898", headers=headers
)
if r.status_code == 429:
print(f"{(time.time() - start_time):.2f}s - Rate limited again lmao")
else:
print(
f"{(time.time() - start_time):.2f}s - Not rate limited. ({r.status_code})"
)
rlimit = threading.Thread(target=rate_limit_check)
rlimit.start()
intents = disnake.Intents().all()
game = disnake.Activity(name="sus gaming | k.help", type=disnake.ActivityType.playing)
client = commands.Bot(
command_prefix="k.",
intents=intents,
activity=game,
default_install_types=disnake.ApplicationInstallTypes.all(),
default_contexts=disnake.InteractionContextTypes.all(),
)
client.remove_command("help")
async def log(text, print_text=None):
tz = pytz.timezone("Asia/Manila")
curr_time = datetime.now(tz)
clean_time = curr_time.strftime("%m/%d/%Y %I:%M %p")
final = f"{clean_time} - {text}\n"
if not print_text:
pass
else:
print(final)
with open("log.txt", "a", encoding="utf-8") as f:
f.write(final)
client.start_time = start_time
client.log = log
client.sus_on = False
print(f"{(time.time() - start_time):.2f}s - Importing Kur0's modules...")
# Files we don't want to load as an extension
exclude_files = [
"modules/paginator.py",
"modules/template.py",
"myfunctions/file_handler.py",
"myfunctions/msg_link_grabber.py",
"myfunctions/greenscreen.py",
"myfunctions/subprocess_runner.py",
"myfunctions/my_db.py",
"myfunctions/async_wrapper.py",
]
for folder in ["modules", "myfunctions"]:
for file in Path(folder).rglob("*.py"):
if str(file) not in exclude_files:
module = str(file.parent).replace("/", ".") + "." + file.stem
client.load_extension(module)
print(f"{(time.time() - start_time):.2f}s - {module} loaded")
print(f"{(time.time() - start_time):.2f}s - Done!")
@client.before_invoke
async def common(ctx):
text = (
f"k.{ctx.invoked_with} | {ctx.author.name}#{ctx.author.discriminator} | "
f'"{ctx.guild.name}" - "{ctx.channel.name}"'
)
await log(str(text))
@client.command()
async def stream(ctx, link, noembed=None):
if link.startswith("https://youtu.be"):
idd = link.split("/")[-1].split("?")[0]
wrong = False
elif link.startswith("https://www.youtube.com/"):
idd = link.split("=")[1].split("&")[0]
wrong = False
else:
await ctx.send("Not a YT link!", delete_after=3.0)
wrong = True
print(idd)
if not wrong:
params = {
"part": "liveStreamingDetails,snippet",
"key": os.getenv("YT_API_KEY"),
"id": idd,
}
url = "https://www.googleapis.com/youtube/v3/videos"
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as resp:
r = await resp.json()
isotime = r["items"][0]["liveStreamingDetails"]["scheduledStartTime"]
title = r["items"][0]["snippet"]["title"]
author = r["items"][0]["snippet"]["channelTitle"]
try:
thumbnail = r["items"][0]["snippet"]["thumbnails"]["maxres"]["url"]
except Exception:
try:
thumbnail = r["items"][0]["snippet"]["thumbnails"]["standard"]["url"]
except Exception:
try:
thumbnail = r["items"][0]["snippet"]["thumbnails"]["high"]["url"]
except Exception:
try:
thumbnail = r["items"][0]["snippet"]["thumbnails"]["medium"][
"url"
]
except Exception:
thumbnail = r["items"][0]["snippet"]["thumbnails"]["default"][
"url"
]
channelid = r["items"][0]["snippet"]["channelId"]
parsed_t = dp.parse(isotime)
t_in_seconds = parsed_t.timestamp()
reltime = f"<t:{str(t_in_seconds).split('.', maxsplit=1)[0]}:R>"
dttime = datetime.strptime(isotime, "%Y-%m-%dT%H:%M:%S%z")
with open(
"list.txt", "a", encoding="utf-8"
) as f: # add stream url and time to list.txt
f.write(f"{link} {parsed_t.strftime('%a %b %d %Y %H:%M:%S')}\n")
# a_file = open("list.txt", "r")
# a_file.close()
# ^^why did i have this? i'm reading the file then doing nothing???
params2 = {
"part": "snippet",
"key": os.getenv("YT_API_KEY"),
"id": channelid,
}
url = "https://www.googleapis.com/youtube/v3/channels"
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params2) as resp:
r2 = await resp.json()
pfp = r2["items"][0]["snippet"]["thumbnails"]["default"]["url"]
e = disnake.Embed(title=title, timestamp=dttime, description=reltime, url=link)
e.set_author(
name=author,
icon_url=pfp,
url=f"https://www.youtube.com/channel/{channelid}",
)
e.set_image(url=thumbnail)
if noembed != "noembed":
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url(
"https://discord.com/api/webhooks/880667610323234877/oc31FGZ3SPfu7BCru4iOd2ULJA"
"vyOdMi1SOaqNF58sHKBknFbdhK5zfqSZhxS4NZF9pU",
session=session,
)
await webhook.send(embed=e)
else:
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url(
"https://discord.com/api/webhooks/880667610323234877/oc31FGZ3SPfu7BCru4iOd2ULJA"
"vyOdMi1SOaqNF58sHKBknFbdhK5zfqSZhxS4NZF9pU",
session=session,
)
await webhook.send(f"{reltime} **{author}** - [{title}](<{link}>)")
await ctx.message.delete()
try:
client.loop.create_task(
run_at(parsed_t.replace(tzinfo=None), open_url(link), link)
)
except Exception as e:
await ctx.send(f"Error: {e}")
async def clear_list(url):
with open("list.txt", "r", encoding="utf-8") as a_file:
lines = a_file.read().splitlines()
with open("list.txt", "w+", encoding="utf-8") as r:
for i in lines:
if i.split(" ")[0] != url:
r.write(f"{i}\n")
async def open_url(url):
print(f"{url} is starting!")
with open("log.txt", "a", encoding="utf-8") as f:
f.write(f"open_url running {url}\n")
avi_guild = client.get_guild(603147860225032192)
while avi_guild is None:
avi_guild = client.get_guild(603147860225032192)
await asyncio.sleep(1)
print("got guild!")
sched_ch = avi_guild.get_channel(879702977898741770)
print("got channel!")
messages = await sched_ch.history(limit=200).flatten()
print("got messages")
count = 0
for msg in messages:
if msg.reference is not None and not msg.is_system():
msg_id = int(msg.reference.message_id)
msgg = await sched_ch.fetch_message(msg_id)
for i in msgg.embeds:
if i.url == url:
print(i.url)
count += 1
print(f"{count} times")
if count == 0:
for msg in messages:
for i in msg.embeds:
if i.url == url:
print("found specific message")
print(msg.jump_url.split("/")[-1])
msg_id = int(msg.jump_url.split("/")[-1])
msg = await sched_ch.fetch_message(msg_id)
await msg.reply("<@&888794254837706804> Starting!")
await clear_list(url)
@client.command()
@commands.is_owner()
async def sched(ctx, url):
sched_ch = client.get_guild(603147860225032192).get_channel(879702977898741770)
messages = await sched_ch.history(limit=200).flatten()
count = 0
for msg in messages:
if msg.reference is not None and not msg.is_system():
msg_id = int(msg.reference.message_id)
msgg = await sched_ch.fetch_message(msg_id)
for i in msgg.embeds:
if i.url == url:
print(i.url)
count += 1
print(f"{count} times")
@client.command()
async def tasks(ctx):
client.loop.set_debug(True)
@client.command()
async def ping(ctx):
start_time = time.time()
msg = await ctx.send(f"My ping is {round (client.latency * 1000)}ms")
send_time = (time.time() - start_time) * 1000
await msg.edit(
content=f"{msg.content} but it took {send_time:.2f}ms to send this message"
)
async def wait_until(dt):
# sleep until the specified datetime
now = datetime.now()
await asyncio.sleep((dt - now).total_seconds())
async def run_at(dt, coro, url):
now = datetime.now()
nowstr = now.strftime("%m/%d/%Y %H:%M:%S")
print(f"{url} is scheduled!")
with open("log.txt", "a", encoding="utf-8") as f:
f.write(f"{url} - scheduled at {nowstr}\n")
await wait_until(dt)
with open("log.txt", "a", encoding="utf-8") as f:
f.write(f"{url} starting!\n")
return await coro
def precheck():
if not os.path.isfile("list.txt"):
with open(
"list.txt", "w", encoding="utf-8"
) as file: # creates txt if doesn't exist
pass
with open("list.txt", "r", encoding="utf-8") as a_file: # reads the txt
lines = a_file.read().splitlines()
with open("list.txt", "w+", encoding="utf-8") as r:
for i in lines:
later = datetime.strptime(i.split(" ", 1)[1], "%a %b %d %Y %H:%M:%S")
now = datetime.now()
if later > now:
r.write(i + "\n")
url = i.split(" ")[0]
if later > now + timedelta(days=6):
print("more than 1 week")
else:
client.loop.create_task(run_at(later, open_url(url), url))
tcheck = threading.Thread(target=precheck)
tcheck.start()
print(f"{(time.time() - start_time):.2f}s - schedules checked!")
keep_alive()
proc_id = os.getpid()
print(f"{(time.time() - start_time):.2f}s - Process ID: {proc_id}")
asyncio.run(log(f"Process ID: {proc_id}", False))
check(start_time, proc_id)
loop = client.loop
try:
print(f"{(time.time() - start_time):.2f}s - Connecting to bot...")
loop.run_until_complete(client.start(os.getenv("TOKEN")))
except KeyboardInterrupt:
loop.run_until_complete(client.close())
# cancel all tasks lingering
finally:
loop.close()