forked from i32ropie/lol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
298 lines (237 loc) · 8.13 KB
/
config.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#################################################
# BOT TOKEN #
#################################################
import os
import telebot
from telebot import types
from colorclass import Color
import json
import time
import six
import sys
import traceback
import re
import socket
from collections import OrderedDict
from pymongo import MongoClient
import requests
from random import choice
#################################################
# USEFUL FUNCTIONS AND DATAS #
#################################################
with open('extra_data/extra.json', 'r') as f:
extra = json.load(f)
bot = telebot.TeleBot(extra['token'])
logBot = telebot.TeleBot(extra['token_logbot'])
admins = extra['admins']
filtered = list()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
MESSAGE = extra['udp_message']
UDP_IP = extra['udp_ip']
UDP_PORT = extra['udp_port']
client = MongoClient('localhost:27017')
db = client.users
with open('responses.json', encoding='utf-8') as f:
responses = json.load(f, object_pairs_hook=OrderedDict)
def send_udp(txt):
sock.sendto(MESSAGE.format(txt).encode(), (UDP_IP, UDP_PORT))
userStep = dict()
def is_recent(m):
return (time.time() - m.date) < 60
def is_banned(uid):
if is_user(uid):
return db.usuarios.find_one(str(uid))['banned']
else:
return False
def next_step_handler(uid):
""" Función para controlar los steps dentro de las diferentes funciones """
if uid not in userStep or is_banned(uid):
userStep[uid] = 0
return userStep[uid]
def lang(uid):
if db.usuarios.find_one(str(uid)):
return db.usuarios.find_one(str(uid))['lang']
else:
return 'en'
def is_beta(uid):
""" Función para comprobar si un ID es beta """
return uid in extra['beta']
def is_user(cid):
return db.usuarios.find_one(str(cid)) is not None
def is_admin(cid):
""" Función para comprobar si un ID es admin """
return int(cid) in admins
def remove_tag(text):
TAG_RE = re.compile(r'<[^>]+>')
return TAG_RE.sub('', text)
def isint(s):
if not s:
return False
if s[0] in ('-', '+'):
return s[1:].isdigit()
return s.isdigit()
def escape_markup(text):
if not isinstance(text, str):
return text
characters = ['_', '*', '[']
for character in characters:
text = text.replace(character, '\\' + character)
return text
def contact_format(m):
name = m.from_user.first_name
alias = str(m.from_user.username)
cid = str(m.chat.id)
uid = str(m.from_user.id)
m_id = str(m.message_id)
msg = m.text
if cid == uid:
txt = "Nuevo mensaje\n\nNombre: " + name + "\nAlias: @" + alias + "\nIdioma: " + \
lang(cid) + "\nM_ID: " + m_id + "\nID: " + cid + "\n\nMensaje: " + msg
else:
txt = "Nuevo mensaje\n\nNombre: " + name + "\nAlias: @" + alias + "\nIdioma: " + lang(
cid) + "\nM_ID: " + m_id + "\nID: " + cid + "\nUID: " + uid + "\n\nMensaje: " + msg
return txt
with open('extra_data/file_ids.json', 'r') as f:
file_ids = json.load(f)
with open('extra_data/sorcio.json', 'r') as f:
sorcio = json.load(f)
data = dict()
for x in [
'es',
'en',
'de',
'it',
'fr',
'pl',
'pt',
'ru',
'el',
'th',
'tr',
'ro']:
with open('champs_%s.json' % x, 'r') as f:
data[x] = json.load(f)
with open('champs_en.json', 'r') as f:
data['fa'] = data['ar'] = json.load(f)
with open('champs_keys.json', 'r') as f:
data['keys'] = json.load(f)
def line(alt=False):
if alt:
return u'\n—————————————————————————\n'
else:
return u'\n`—————————————————————————`\n'
def send_exception(exception):
exc_type, exc_obj, exc_tb = sys.exc_info()
tb = traceback.extract_tb(exc_tb, 4)
message = '\n`' + str(exc_type) + '`'
message += '\n\n`' + str(exc_obj) + '`'
for row in tb:
message += line()
for val in row:
message += '`' + str(val) + '`\n'
return message
def to_json(m):
d = {}
for x, y in six.iteritems(m.__dict__):
if hasattr(y, '__dict__'):
d[x] = to_json(y)
else:
d[x] = y
return d
def static_versions():
return '8.13.1'
base_regions = {'euw': 'euw1',
'ru': 'ru',
'kr': 'kr',
'br': 'br1',
'oce': 'oc1',
'na': 'na1',
'eune': 'eun1',
'lan': 'la1',
'las': 'la2',
'tr': 'tr1'}
def update_region(reg):
return base_regions.get(reg)
def get_3_best_champs(summonerId, region, cid):
url = 'https://{}.api.riotgames.com/lol/champion-mastery/v3/champion-masteries/by-summoner/{}'.format(
update_region(region.lower()), summonerId)
params = {
"api_key": extra['lol_api']
}
jstr = requests.get(
url=url,
params=params
)
if jstr.status_code != 200:
return None
else:
return OrderedDict([(data[lang(cid)][data['keys'][str(x['championId'])]['key']][
'name'], str(x['championLevel'])) for x in jstr.json()[:3]])
def get_summoner(name, region):
url = "https://{}.api.riotgames.com/lol/summoner/v3/summoners/by-name/{}".format(region, name)
params = {
'api_key': extra['lol_api']
}
r = requests.get(url, params)
if r.status_code != 200:
raise Exception("Error finding {} in {}".format(name, region))
return r.json()
def get_current_game(summoner_id, region):
url = "https://{}.api.riotgames.com/lol/spectator/v3/active-games/by-summoner/{}".format(region, summoner_id)
params = {
'api_key': extra['lol_api']
}
r = requests.get(url=url, params=params)
if r.status_code != 200:
raise Exception("The summoner with ID {} is not in game.".format(summoner_id))
return r.json()
def get_summoner_info(invocador, region, cid):
try:
summoner = get_summoner(name=invocador, region=update_region(region))
except:
txt = responses['summoner_error'][
lang(cid)] % (invocador, region.upper())
return txt
lattest_version = static_versions()
icon_id = summoner['profileIconId']
icon_url = "http://ddragon.leagueoflegends.com/cdn/{}/img/profileicon/{}.png".format(
lattest_version, icon_id)
summoner_name = summoner['name']
summoner_id = summoner['id']
lolking = "http://www.lolking.net/summoner/" + \
region + "/" + str(summoner_id)
summoner_level = summoner['summonerLevel']
txt = responses['summoner_30_beta_1'][lang(cid)].format(icon_url, summoner_name, lolking, summoner_level)
if summoner_level > 29:
url = "https://{}.api.riotgames.com/lol/league/v3/positions/by-summoner/{}".format(update_region(region), summoner_id)
params = {'api_key': extra['lol_api']}
r = requests.get(url, params)
if r.status_code != 200:
txt = "ERROR EN LÍNEA 89 DE me.py"
return txt
r_json = r.json()
if not r_json:
aux = {}
if len(r_json) == 1:
aux = {r_json[0]['queueType']:r_json[0]}
else:
aux = {x['queueType']:x for x in r_json}
for x in aux:
txt += responses['summoner_30_beta_2'][lang(cid)].format(
"SoloQ" if x == "RANKED_SOLO_5x5" else "FlexQ" if x == "RANKED_FLEX_SR" else x,
responses['tier'][lang(cid)][aux[x]['tier']],
aux[x]['rank'],
aux[x]['wins'],
aux[x]['losses'],
aux[x]['leaguePoints'])
try:
bst = get_3_best_champs(summoner['id'], region, cid)
if bst:
txt += '\n\n*' + responses['best_champs'][lang(cid)] + '*:'
for x, y in bst.items():
txt += '\n- ' + x + ' _(Level: ' + y + ')_'
except Exception as e:
bot.send_message(52033876, send_exception(e), parse_mode="Markdown")
return txt