This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.py
381 lines (318 loc) · 13.9 KB
/
bot.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
375
376
377
378
379
380
381
import asyncio
import discord
import pytube
import datetime
import os
import urllib.parse
import moviepy.editor
datetime_ = datetime.datetime.now()
timedate = datetime_.strftime("%d.%m.%Y-%H.%M.%S" + ": ")
bot_name = "LcsTh's YT Downloader"
http = "https://"
ip_domain = "lcsth.de"
github = http + "bot.lcsth.de"
dc_invite = http + "dc.lcsth.de"
server_id = "570967338946527262"
channel_id = "823996526966734849"
test_server_id = "825350098603081750"
test_channel_id = "825350098603081753"
path = "/home/ec2-user/http-data"
token_path = "/home/ec2-user/dc-token.txt"
server_log = "server_log.txt"
server_ids = "server_ids.txt"
msg_log = "msg_log.txt"
log = "log.txt"
purged_log = "purged_log.txt"
purge_time = "03:00"
falscher_discord_nachricht = "Dieser Bot kann momentan nur auf dem ApfelPlayer Discord verwendet werdent. Wenn ein anderer Server den Bot benutzen will, kann der Besitzer mir (LcsTh#9195) gerne eine Nachricht schreiben."
keine_rechte_nachricht = "Dazu hast du keine Rechte"
command = "$yt"
audio_command = "$audio"
purge_command = "$purge"
help_command = "$help"
help_command2 = "$hilfe"
client = discord.Client()
async def del_vids():
for file in os.listdir(path):
if file.endswith(".mp4") or file.endswith(".mp3"):
os.remove(path + "/" + file)
async def purge_all():
channel = client.get_channel(int(channel_id))
test_channel = client.get_channel(int(test_channel_id))
await channel.purge()
await test_channel.purge()
async def dc_purge():
while True:
zeit = datetime.datetime.now()
stundeminute = zeit.strftime("%H:%M")
if str(stundeminute) == purge_time:
await purge_all()
await del_vids()
with open(purged_log, "a") as f:
f.write(f"{timedate} Auto Purge\n")
print(timedate + "Auto Purge!")
await del_vids()
await asyncio.sleep(500)
else:
await asyncio.sleep(50)
with open(token_path, "r") as f:
token = f.readline()
@client.event
async def on_ready():
print("'{0.user}' has logged in!".format(client))
await client.change_presence(
activity=discord.Activity(type=discord.ActivityType.listening, name=command)
)
await purge_all()
with open(purged_log, "a") as f:
f.write(f"{timedate} Start Purge\n")
print(timedate + "Start Purge!")
await del_vids()
await dc_purge()
@client.event
async def on_message(msg):
def is_user(m):
return m.author == usr
def is_me(m):
return m.author == client.user
if msg.author != client.user:
if isinstance(msg.channel, discord.channel.DMChannel):
embed = discord.Embed(
title=falscher_discord_nachricht,
description="[ApfelPlayer Discord]("
+ dc_invite
+ ") | [GitHub]("
+ github
+ ")",
color=0x00FF26,
)
embed.set_footer(text=bot_name)
await msg.channel.send(embed=embed)
with open(msg_log, "a") as f:
f.write(f"{timedate + str(msg.channel)} \n")
print(timedate + str(msg.channel))
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
elif str(msg.guild.id) != server_id and str(msg.guild.id) != test_server_id:
embed = discord.Embed(
title=falscher_discord_nachricht,
description="[ApfelPlayer Discord]("
+ dc_invite
+ ") | [GitHub]("
+ github
+ ")",
color=0x00FF26,
)
embed.set_footer(text=bot_name)
logged = (
str(timedate)
+ "\nChannel: "
+ msg.channel.name
+ "\nBenutzer: "
+ msg.author.name
+ "#"
+ str(msg.author.discriminator)
+ " ("
+ str(msg.author.id)
+ ")\nServer Name: "
+ msg.guild.name
+ "\nID: "
+ str(msg.guild.id)
+ "\nMitglieder: "
+ str(msg.guild.member_count)
+ "\n\n"
)
if str(msg.content).lower().startswith("$"):
usr = msg.author
await msg.channel.send(embed=embed)
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
await msg.channel.purge(limit=1, check=is_user)
with open(server_ids, "r") as datei:
text = datei.read().strip().split()
if str(msg.guild.id) in text:
return
else:
with open(server_ids, "a") as dateiA:
dateiA.write(str(msg.guild.id) + "\n")
with open(server_log, "a") as f:
f.write(f"{logged}\n")
else:
if str(msg.content).lower() == purge_command:
if msg.author.guild_permissions.administrator:
await msg.channel.purge()
for file in os.listdir(path):
if file.endswith(".mp4") or file.endswith(".mp3"):
os.remove(path + "/" + file)
with open(log, "a") as f:
f.write(
f"{timedate}{msg.author.name} # {msg.author.discriminator} hat {purge_command} ausgeführt!\n"
)
print(
timedate
+ msg.author.name
+ "#"
+ msg.author.discriminator
+ " hat "
+ purge_command
+ " ausgeführt!"
)
else:
await msg.channel.purge(limit=1)
await msg.channel.send(
keine_rechte_nachricht + "{}".format(msg.author.mention)
)
with open(log, "a") as f:
f.write(
f"{timedate}{msg.author.name} # {msg.author.discriminator} hat versucht {purge_command} auszuführen!\n"
)
print(
timedate
+ msg.author.name
+ "#"
+ msg.author.discriminator
+ " hat versucht "
+ purge_command
+ " auszuführen!"
)
await asyncio.sleep(5)
await msg.channel.purge(limit=1)
else:
if (
str(msg.channel.id) != channel_id
and str(msg.channel.id) != test_channel_id
):
if (
str(msg.content).lower().startswith(command)
or str(msg.content).lower().startswith(audio_command)
or str(msg.content).lower().startswith(help_command)
or str(msg.content).lower().startswith(help_command2)
):
await msg.channel.purge(limit=1)
if str(msg.guild.id) == server_id:
text_channel = client.get_channel(int(channel_id))
elif str(msg.guild.id) == test_server_id:
text_channel = client.get_channel(int(test_channel_id))
await msg.channel.send(
"{0} Falscher Kanal! Bitte den {1} Kanal benutzen!".format(
msg.author.mention, text_channel.mention
)
)
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
return
else:
return
if msg.author == client.user:
return
if str(msg.content).lower().startswith(help_command) or str(
msg.content
).lower().startswith(help_command2):
await msg.channel.purge(limit=1, check=is_user)
await msg.channel.send(command + " <URL>")
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
elif str(msg.content).lower().startswith(command) or str(
msg.content
).lower().startswith(audio_command):
if str(msg.content).lower() == command:
usr = msg.author
await msg.channel.purge(limit=1, check=is_user)
await msg.channel.send(command + " <URL>")
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
else:
os.system("docker restart httpd")
usr = msg.author
url = str(msg.content.split(" ")[1])
yt = pytube.YouTube(url)
zahl = int(yt.length / 60 / 60)
if zahl != 0:
await msg.channel.purge(check=is_user)
embed = discord.Embed(
title="Video „"
+ yt.title
+ "“ von „"
+ yt.author
+ "“ ist zu lange um heruntergeladen zu werden. Es sollte unter einer Stunde lang sein",
description="[GitHub](" + github + ")",
color=0x00FF26,
)
embed.set_footer(text=bot_name)
await msg.channel.send(embed=embed)
await msg.channel.send("{}".format(msg.author.mention))
await msg.channel.purge(limit=1, check=is_me)
await asyncio.sleep(5)
await msg.channel.purge(limit=1, check=is_me)
else:
with open(log, "a") as f:
f.write(
f"{timedate}Downloading: {yt.title} (Requested by {msg.author.name})\n"
)
print(
timedate
+ "Downloading: "
+ yt.title
+ " (Requested by "
+ str(msg.author.name)
+ ")"
)
await msg.channel.send(
"Starte Herunterladen {}".format(msg.author.mention)
)
title_patched = urllib.parse.quote(yt.title).replace(
"%", "_"
)
for numbers in title_patched:
if numbers.isdigit():
title_patched = title_patched.replace(numbers, "")
title_patched = title_patched.replace("/", "_")
title_patched = title_patched.replace("\\", "_")
yt.streams.filter(
progressive=True
).get_highest_resolution().download(
output_path=path, filename=title_patched
)
video = moviepy.editor.VideoFileClip(
path + "/" + title_patched + ".mp4"
)
video.audio.write_audiofile(
path + "/" + title_patched + ".mp3"
)
with open(log, "a") as f:
f.write(f"{timedate}Done with: {yt.title}\n")
print(timedate + "Done with: " + yt.title)
link = http + ip_domain + "/" + title_patched
await msg.channel.purge(limit=1, check=is_me)
auflösung = (
yt.streams.filter(progressive=True)
.get_highest_resolution()
.resolution
)
embed = discord.Embed(
title="Video „"
+ yt.title
+ "“ von „"
+ yt.author
+ "“ heruntergeladen",
description="[Video]("
+ link
+ ".mp4) ("
+ auflösung
+ ") | [Audio]("
+ link
+ ".mp3) | [GitHub]("
+ github
+ ")",
color=0x00FF26,
)
embed.set_footer(text=bot_name)
await msg.channel.send(embed=embed)
await msg.channel.send("{}".format(msg.author.mention))
await msg.channel.purge(limit=1, check=is_me)
await msg.channel.purge(check=is_user)
else:
return
else:
return
client.run(token)