-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.py
270 lines (226 loc) · 10.6 KB
/
helpers.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
import random
from datetime import datetime
import builtins
import os
import discord
from fuzzywuzzy import process
from colorama import Fore
import requests
import config
from bs4 import BeautifulSoup
import importlib.util
from config import tenor_key, giphy_key
last_gif = None
platforms = platforms_with_local = ["tenor"]
gif_blacklist = [
"https://media.tenor.com/images/a9c021d6e32d1de2c117e8cf0e4b5a57/tenor.gif",
]
dir_path = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(dir_path + '/logs'):
os.mkdir(dir_path + '/logs')
log_file = open(dir_path + "/logs/log_{}.txt".format(datetime.now().strftime('%H_%M_%S_%d_%m_%Y')), "w", encoding="utf8")
# noinspection PyShadowingBuiltins
def print(*args, log_level=0, end="\n"):
if log_level == 0:
log_prefix = f"{Fore.LIGHTGREEN_EX}INFO {Fore.RESET}"
elif log_level == 1:
log_prefix = f"{Fore.YELLOW}WARN {Fore.RESET}"
elif log_level == 2:
log_prefix = f"{Fore.RED}ERROR {Fore.RESET}"
else:
log_prefix = " "
log_prefix = "[" + log_prefix + "] "
print_string = f"[{Fore.WHITE}" + datetime.now().strftime('%H:%M:%S.%f') + f"{Fore.RESET}] " + log_prefix + " ".join(map(str, args)).replace("\n", "")
builtins.print(print_string, end=end)
log_file.write(print_string + end)
log_file.flush()
async def check_admin_permissions(message):
member: discord.Member = message.guild.get_member(message.author.id)
if message.author._user.id not in config.admin_ids: # and not (member.guild_permissions.administrator or member.guild_permissions.manage_messages or member.guild_permissions.manage_roles):
e = discord.Embed()
e.title = '❗ Fehler'
e.description = "Du hast nicht die erforderlichen Berechtigungen um diesen Befehl zu benutzen.\n" \
"Nur Server-Mods und vom Bot-Autor ausgewählte Personen können diesen Befehl ausführen.\n" \
"Du kannst dir authorisierte Personen mit ``+op`` anzeigen lassen."
await message.channel.send(embed=e)
return False
else:
return True
def get_server_webhooks(server_id):
webhooks = {}
server_path = dir_path + "/server_webhooks/" + str(server_id)
if not os.path.exists(server_path):
os.mkdir(server_path)
for itm in sorted(os.listdir(server_path)):
if itm.startswith("__"):
continue
f = open(server_path + "/" + itm)
wh = [x for x in f.read().replace("\r", "").split("\n") if x.strip() != ""]
webhooks[itm.replace(".txt", "")] = wh
f.close()
return webhooks
def get_server_actions(server_id):
actions = []
command_actions = {}
server_path = dir_path + "/server_actions/" + str(server_id)
if not os.path.exists(server_path):
os.mkdir(server_path)
for itm in sorted(os.listdir(server_path)):
if itm.startswith("__"):
continue
spec = importlib.util.spec_from_file_location(itm, server_path + "/" + itm)
loaded_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(loaded_module)
actions.append(loaded_module)
for command in loaded_module.commands:
command_actions[command] = loaded_module
return command_actions, actions
def parse(message: discord.Message):
split = message.content.split(' ')
command = split[0][1:]
channel = message.channel
params = split[1:]
mentions = message.mentions
author = message.author
return command.lower(), channel, params, mentions, author
def get_gif(search_term, lmt=10, pos=None, wo_anime=False, platform=None, check_last=True):
global last_gif
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Get")
if True: # platform is None:
if os.path.exists("cache/") and search_term in os.listdir("cache/"):
platform = random.choice(platforms_with_local)
else:
platform = random.choice(platforms)
if platform == "tenor":
if pos is None:
pos = random.randint(0, 100)
if not wo_anime:
search_term = 'anime ' + search_term
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Using: Tenor |", search_term, lmt, pos)
r = requests.get("https://api.tenor.com/v1/search?q=%s&key=%s&limit=%s&contentfilter=medium&pos=%s" % (search_term, tenor_key, lmt, pos))
if r.status_code == 200:
gifs = r.json()
options = [itm["media"][0]['gif']["url"] for itm in gifs["results"]]
if last_gif in options and check_last:
options.remove(last_gif)
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in options", len(options))
sel = random.choice(options)
last_gif = sel
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "Selected gif:", sel, "<-", options)
return sel
else:
return None
elif platform == "giphy":
if pos is None:
pos = random.randint(0, 25)
if not wo_anime:
search_term = 'anime ' + search_term
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Using: Giphy |", search_term, lmt, pos)
r = requests.get("https://api.giphy.com/v1/gifs/search?api_key=%s&q=%s&limit=%s&offset=%s&rating=PG-13" % (giphy_key, search_term, lmt, pos))
if r.status_code == 200:
gifs = r.json()
options = [itm["images"]["original"]["url"] for itm in gifs["data"]]
if last_gif in options:
options.remove(last_gif)
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in options", len(options))
sel = random.choice(options)
last_gif = sel
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "Selected gif:", sel, "<-", options)
return sel
elif platform == "local":
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Using: Local |", search_term)
options = os.listdir(f"cache/{search_term}/tenor")
if last_gif in options:
print(len(options))
options.remove(last_gif)
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in options", len(options))
sel = random.choice(options)
last_gif = sel
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", dir_path + f"/cache/{search_term}/tenor/" + sel, "<-", f"cache/{search_term}/tenor")
return dir_path + f"/cache/{search_term}/tenor/" + sel
elif platform == "discord":
if pos is None:
pos = random.randint(0, 100)
if not wo_anime:
search_term = 'anime ' + search_term
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Using: Discord |", search_term, lmt, pos)
r = requests.get(f"https://discordapp.com/api/v6/gifs/search?q={search_term}&media_format=gif")
if r.status_code == 200:
gifs = [x["src"] for x in r.json()]
if pos == 0 and lmt is not None:
gifs = gifs[:lmt]
for blocked_gif in gif_blacklist:
if blocked_gif in gifs:
gifs.remove(blocked_gif)
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "blocked_gif in gifs", len(gifs), blocked_gif)
if last_gif in gifs:
gifs.remove(last_gif)
# print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in gifs", len(gifs))
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in gifs", len(gifs))
if len(gifs) == 0:
return None
sel = random.choice(gifs)
last_gif = sel
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "Selected gif:", sel) #, "<-", gifs)
return sel
else:
return None
elif platform == "fallback":
if pos is None:
pos = random.randint(0, 25)
if not wo_anime:
search_term = 'anime ' + search_term
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}] Using: Fallback |", search_term, lmt, pos)
headers = {
'authority': 'tenor.com',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'dnt': '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'sec-fetch-dest': 'document',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'accept-language': 'en-DE,en;q=0.9,de-DE;q=0.8,de;q=0.7,en-US;q=0.6',
}
r = requests.get(f'https://tenor.com/search/{search_term.replace(" ", "-")}-gifs', headers=headers)
if r.status_code == 200:
s = BeautifulSoup(r.text, 'html.parser')
gifs = gifs = [x["src"] for x in s.find_all("img") if x["src"].startswith("https://media.tenor.com/")]
if pos == 0 and lmt is not None:
gifs = gifs[:lmt]
if last_gif in gifs:
gifs.remove(last_gif)
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "last_gif in gifs", len(gifs))
sel = random.choice(gifs)
last_gif = sel
print(f"[{Fore.MAGENTA}{'System - Gif':20}{Fore.RESET}]", "Selected gif:", sel, "<-", gifs)
return sel
def get_goat():
goats = [g for g in os.listdir(dir_path + '/assets/goats/') if not g.endswith('.mp4') and not g.endswith('.db')]
goat = random.choice(goats)
print(goat, "<-", len(goats))
return goat
def get_maxi():
maxis = [g for g in os.listdir(dir_path + '/assets/maxitogo/') if not g.endswith('.mp4') and not g.endswith('.db')]
maxi = random.choice(maxis)
print(maxi, "<-", len(maxis))
return maxi
def get_islieb(term=None):
comics = [g for g in os.listdir(dir_path + '/assets/islieb/') if not g.endswith('.mp4') and not g.endswith('.db')]
if term is None:
print("Getting random comic")
comic = random.choice(comics)
print(comic, "<-", len(comics))
else:
print("Searching for comic:", term)
term_comics = [x for x in process.extract(term, comics, limit=15) if x[1] > 75]
if len(term_comics) > 0:
comic = random.choice(term_comics)[0]
else:
print("using random")
comic = random.choice(comics)
print(comic, "<-", term)
return comic