-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
298 lines (272 loc) · 16.8 KB
/
main.py
File metadata and controls
298 lines (272 loc) · 16.8 KB
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
# -*- coding: utf-8 -*-
import json
import logging
from threading import Thread
from telegram import Bot, Update, InlineKeyboardButton, InlineKeyboardMarkup, ParseMode
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler, JobQueue
from telegram.ext.dispatcher import run_async
from core.functions.bosses import boss_leader, boss_zhalo, boss_monoeye, boss_hydra
from core.functions.orders import order, orders, MSG_ORDER_ACCEPT
from core.functions.admins import list_admins, admins_for_users, set_admin, del_admin, set_global_admin, \
set_super_admin, del_global_admin
from core.functions.common import help_msg, ping, start, error, kick, admin_panel, stock_compare, trade_compare, \
check_bot_in_chats, delete_msg, delete_user
from core.functions.inline_keyboard_handling import callback_query, send_status, generate_ok_markup, send_order, \
QueryType
from core.functions.pin import pin, not_pin_all, pin_all, silent_pin
from core.functions.triggers import set_trigger, add_trigger, del_trigger, list_triggers, enable_trigger_all, \
disable_trigger_all, trigger_show, set_global_trigger, add_global_trigger, del_global_trigger
from core.functions.welcome import welcome, set_welcome, show_welcome, enable_welcome, disable_welcome
from core.functions.order_groups import group_list, add_group
from core.types import Session, Group, Order, Squad, Admin
from core.utils import add_user, send_async
from config import TOKEN, API_PORT, GOVERNMENT_CHAT
from core.regexp import profile, hero
import re
from core.functions.profile import char_update, char_show, find_by_username
from core.functions.squad import add_squad, del_squad, set_invite_link, set_squad_name, enable_thorns, disable_thorns, \
squad_list, squad_request, list_squad_requests, open_hiring, close_hiring, remove_from_squad, add_to_squad
from core.functions.activity import day_activity, week_activity, battle_activity
from datetime import datetime, time
from core.functions.api import app
last_welcome = 0
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def battle_time():
now = datetime.now().time()
_BATTLE_TIME = [[time(7, 57), time(8, 0)],
[time(11, 57), time(12, 0)],
[time(15, 57), time(16, 0)],
[time(19, 57), time(20, 0)],
[time(23, 57), time(0, 0)]]
for time_start, time_end in _BATTLE_TIME:
if time_start <= time_end and time_start <= now <= time_end or \
not time_start <= time_end and (time_start <= now or now <= time_end):
return True
return False
def del_msg(bot, job):
bot.delete_message(job.context[0], job.context[1])
@run_async
def manage_all(bot: Bot, update: Update, chat_data, job_queue):
try:
add_user(update.message.from_user)
if update.message.chat.type in ['group', 'supergroup', 'channel']:
session = Session()
squad = session.query(Squad).filter_by(chat_id=update.message.chat.id).first()
admin = session.query(Admin).filter(Admin.user_id == update.message.from_user.id and
Admin.admin_group in [update.message.chat.id, 0]).first()
if squad is not None and admin is None and battle_time():
bot.delete_message(update.message.chat.id, update.message.message_id)
if update.message.text and update.message.text.upper().startswith('Приветствие:'.upper()):
set_welcome(bot, update)
elif update.message.text and 'Твои результаты в бою:' in update.message.text and \
update.message.forward_from and update.message.forward_from.id == 265204902:
job_queue.run_once(del_msg, 2, (update.message.chat.id, update.message.message_id))
elif update.message.text and update.message.text.upper() == 'Помощь'.upper():
help_msg(bot, update)
elif update.message.text and update.message.text.upper() == 'Покажи приветствие'.upper():
show_welcome(bot, update)
elif update.message.text and update.message.text.upper() == 'Включи приветствие'.upper():
enable_welcome(bot, update)
elif update.message.text and update.message.text.upper() == 'Выключи приветствие'.upper():
disable_welcome(bot, update)
elif update.message.text and update.message.text.upper().startswith('Затриггерь:'.upper()):
set_trigger(bot, update)
elif update.message.text and update.message.text.upper().startswith('Разтриггерь:'.upper()):
del_trigger(bot, update)
elif update.message.text and update.message.text.upper() == 'Список триггеров'.upper():
list_triggers(bot, update)
elif update.message.text and update.message.text.upper() == 'Список админов'.upper():
list_admins(bot, update)
elif update.message.text and update.message.text.upper() == 'Пинг'.upper():
ping(bot, update)
elif update.message.text and update.message.text.upper() == 'Статистика за день'.upper():
day_activity(bot, update)
elif update.message.text and update.message.text.upper() == 'Статистика за неделю'.upper():
week_activity(bot, update)
elif update.message.text and update.message.text.upper() == 'Статистика за бой'.upper():
battle_activity(bot, update)
elif update.message.text and update.message.text.upper() == 'Разрешить триггерить всем'.upper():
enable_trigger_all(bot, update)
elif update.message.text and update.message.text.upper() == 'Запретить триггерить всем'.upper():
disable_trigger_all(bot, update)
elif update.message.text and update.message.text.upper() in ['Админы'.upper(), 'офицер'.upper()]:
admins_for_users(bot, update)
elif update.message.text and update.message.text.upper() == 'Пинят все'.upper():
pin_all(bot, update)
elif update.message.text and update.message.text.upper() == 'Хорош пинить'.upper():
not_pin_all(bot, update)
elif update.message.text and update.message.text.upper() == 'Пин'.upper() and update.message.reply_to_message is not None:
pin(bot, update)
elif update.message.text and update.message.text.upper() == 'сайлентпин'.upper() and update.message.reply_to_message is not None:
silent_pin(bot, update)
elif update.message.text and update.message.text.upper() in ['бандит'.upper(), 'краб'.upper()]:
boss_leader(bot, update)
elif update.message.text and update.message.text.upper() in ['жало'.upper(), 'королева роя'.upper()]:
boss_zhalo(bot, update)
elif update.message.text and update.message.text.upper() in ['циклоп'.upper(), 'борода'.upper()]:
boss_monoeye(bot, update)
elif update.message.text and update.message.text.upper() in ['гидра'.upper(), 'лич'.upper()]:
boss_hydra(bot, update)
elif update.message.text and update.message.text.upper() == 'открыть набор'.upper():
open_hiring(bot, update)
elif update.message.text and update.message.text.upper() == 'закрыть набор'.upper():
close_hiring(bot, update)
elif update.message.text and update.message.text.upper() == 'удоли'.upper() and update.message.reply_to_message is not None:
delete_msg(bot, update)
elif update.message.text and update.message.text.upper() == 'свали'.upper() and update.message.reply_to_message is not None:
delete_user(bot, update)
elif update.message.text:
trigger_show(bot, update)
elif update.message.chat.type == 'private':
if update.message.text and update.message.text.upper() == 'Статус'.upper():
send_status(bot, update)
elif update.message.text and update.message.text.upper() == 'хочу в отряд'.upper():
squad_request(bot, update)
elif update.message.text and update.message.text.upper() == 'заявки в отряд'.upper():
list_squad_requests(bot, update)
elif update.message.text and update.message.text.upper() in ['Приказы'.upper(), 'пин'.upper()]:
orders(bot, update, chat_data)
elif update.message.text and update.message.text.upper() in ['список отряда'.upper(), 'список'.upper()]:
Thread(target=squad_list, args=(bot, update)).start()
elif update.message.text and update.message.text.upper() == 'Группы'.upper():
group_list(bot, update)
elif update.message.text and update.message.text.upper() == 'чистка отряда'.upper():
remove_from_squad(bot, update)
elif update.message.forward_from and update.message.forward_from.id == 265204902 and \
update.message.text.startswith('📦Содержимое склада'):
stock_compare(bot, update, chat_data)
elif update.message.forward_from and update.message.forward_from.id == 278525885 and \
'📦Твой склад с материалами:' in update.message.text:
trade_compare(bot, update, chat_data)
elif 'wait_group_name' in chat_data and chat_data['wait_group_name']:
add_group(bot, update, chat_data)
elif update.message.text and update.message.forward_from and update.message.forward_from.id == 265204902 and \
(re.search(profile, update.message.text) or re.search(hero, update.message.text)):
char_update(bot, update)
else:
order(bot, update, chat_data)
except Exception as e:
Session.rollback()
@run_async
def ready_to_battle(bot, job_queue):
session = Session()
try:
group = session.query(Squad).all()
for item in group:
if item.chat_id == -1001062678288:
continue
order = Order()
order.text = 'К битве готовсь!'
order.chat_id = item.chat_id
order.date = datetime.now()
order.confirmed_msg = 0
session.add(order)
session.commit()
markup = InlineKeyboardMarkup([[InlineKeyboardButton('ГРАБЬНАСИЛУЙУБИВАЙ!',
callback_data=json.dumps({'t': QueryType.OrderOk.value, 'id': order.id}))]])
msg = send_order(bot, order.text, 0, order.chat_id, markup)
try:
msg = msg.result().result()
bot.request.post(bot.base_url + '/pinChatMessage',
{'chat_id': order.chat_id, 'message_id': msg.message_id,
'disable_notification': False})
except Exception as e:
print(e)
except Exception as e:
Session.rollback()
@run_async
def ready_to_battle_result(bot, job_queue):
session = Session()
try:
group = session.query(Squad).all()
full_attack = 0
full_defence = 0
full_text = ''
full_count = 0
for item in group:
order = session.query(Order).filter_by(chat_id=item.chat_id, text='К битве готовсь!').order_by(Order.date.desc()).first()
if order is not None:
attack = 0
defence = 0
for clear in order.cleared:
if clear.user.character:
attack += clear.user.character.attack
defence += clear.user.character.defence
text = '{} бойцов отряда <b>{}</b> к битве готовы!\n{}⚔ {}🛡'\
.format(len(order.cleared), item.squad_name, attack, defence)
send_async(bot, chat_id=item.chat_id, text=text, parse_mode=ParseMode.HTML)
full_text += '<b>{}</b>: {}👥 {}⚔ {}🛡\n'.format(item.squad_name, len(order.cleared), attack, defence)
full_attack += attack
full_defence += defence
full_count += len(order.cleared)
send_async(bot, chat_id=GOVERNMENT_CHAT, text=full_text + '\n<b>Всего</b>: {}👥 {}⚔ {}🛡'
.format(full_count, full_attack, full_defence), parse_mode=ParseMode.HTML)
except Exception as e:
Session.rollback()
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("admin", admin_panel))
dp.add_handler(CommandHandler("help", help_msg))
dp.add_handler(CommandHandler("ping", ping))
dp.add_handler(CommandHandler("set_global_trigger", set_global_trigger))
dp.add_handler(CommandHandler("add_global_trigger", add_global_trigger))
dp.add_handler(CommandHandler("del_global_trigger", del_global_trigger))
dp.add_handler(CommandHandler("set_trigger", set_trigger))
dp.add_handler(CommandHandler("add_trigger", add_trigger))
dp.add_handler(CommandHandler("del_trigger", del_trigger))
dp.add_handler(CommandHandler("list_triggers", list_triggers))
dp.add_handler(CommandHandler("set_welcome", set_welcome))
dp.add_handler(CommandHandler("enable_welcome", enable_welcome))
dp.add_handler(CommandHandler("disable_welcome", disable_welcome))
dp.add_handler(CommandHandler("show_welcome", show_welcome))
dp.add_handler(CommandHandler("add_admin", set_admin))
dp.add_handler(CommandHandler("add_global_admin", set_global_admin))
dp.add_handler(CommandHandler("del_global_admin", del_global_admin))
dp.add_handler(CommandHandler("add_super_admin", set_super_admin))
dp.add_handler(CommandHandler("del_admin", del_admin))
dp.add_handler(CommandHandler("list_admins", list_admins))
dp.add_handler(CommandHandler("kick", kick))
dp.add_handler(CommandHandler("enable_trigger", enable_trigger_all))
dp.add_handler(CommandHandler("disable_trigger", disable_trigger_all))
dp.add_handler(CommandHandler("me", char_show))
dp.add_handler(CommandHandler("check_bot_in_chats", check_bot_in_chats))
dp.add_handler(CommandHandler("add_squad", add_squad))
dp.add_handler(CommandHandler("del_squad", del_squad))
dp.add_handler(CommandHandler("enable_thorns", enable_thorns))
dp.add_handler(CommandHandler("disable_thorns", disable_thorns))
dp.add_handler(CommandHandler("set_squad_name", set_squad_name))
dp.add_handler(CommandHandler("set_invite_link", set_invite_link))
dp.add_handler(CommandHandler("find", find_by_username))
dp.add_handler(CommandHandler("add", add_to_squad))
dp.add_handler(CallbackQueryHandler(callback_query, pass_chat_data=True))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.status_update, welcome))
# dp.add_handler(MessageHandler(Filters.text, manage_text, pass_chat_data=True))
dp.add_handler(MessageHandler(Filters.all, manage_all, pass_chat_data=True, pass_job_queue=True))
# log all errors
dp.add_error_handler(error)
updater.job_queue.run_daily(ready_to_battle, time(hour=7, minute=50))
updater.job_queue.run_daily(ready_to_battle_result, time(hour=7, minute=55))
updater.job_queue.run_daily(ready_to_battle, time(hour=11, minute=50))
updater.job_queue.run_daily(ready_to_battle_result, time(hour=11, minute=55))
updater.job_queue.run_daily(ready_to_battle, time(hour=15, minute=50))
updater.job_queue.run_daily(ready_to_battle_result, time(hour=15, minute=55))
updater.job_queue.run_daily(ready_to_battle, time(hour=19, minute=50))
updater.job_queue.run_daily(ready_to_battle_result, time(hour=19, minute=55))
updater.job_queue.run_daily(ready_to_battle, time(hour=23, minute=50))
updater.job_queue.run_daily(ready_to_battle_result, time(hour=23, minute=55))
# Start the Bot
updater.start_polling()
# app.run(port=API_PORT)
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()