-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
147 lines (101 loc) · 4.31 KB
/
handlers.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
from datetime import datetime
from telegram import ReplyKeyboardMarkup, ReplyKeyboardHide
from models import User, Item, Room, db, Inventory
from config import logger, HELP_TEXT
default_keyboard = [
['[Мой профиль]'],
['[Инвентарь]', '[Магазин]'],
['[Скрыть]']
]
def start(bot, update):
user_id = update.message.from_user.id
user_name = update.message.from_user.username
user = User(user_id=user_id).get()
# Creating menu keyboard
reply_markup = ReplyKeyboardMarkup(default_keyboard)
if user:
msg = 'С возвращением, %s\nПомощь - /help' % user_name
else:
db.add(User(user_id, user_name))
db.commit()
logger.info('User {}({}) added bot'.format(user_name, user_id))
msg = 'Добро пожаловать в ChatRoo!\nПомощь - /help'
bot.sendMessage(chat_id=update.message.chat_id, text=msg, reply_markup=reply_markup)
def help(bot, update):
bot.sendMessage(update.message.chat_id,
text='\n'.join(HELP_TEXT))
def menu(bot, update):
reply_markup = ReplyKeyboardMarkup(default_keyboard)
bot.sendMessage(chat_id=update.message.chat_id, text='Меню открыто', reply_markup=reply_markup)
def profile(bot, update):
user_id = update.message.from_user.id
bot.sendMessage(update.message.chat_id,
text=User(user_id=user_id).profile())
def inventory(bot, update):
bot.sendMessage(chat_id=update.message.chat_id,
text=str(Inventory(user_id=update.message.from_user.id).get()))
def shop(bot, update): # TODO
pass
def hide(bot, update):
reply_markup = ReplyKeyboardHide()
bot.sendMessage(chat_id=update.message.chat_id,
text="Меню скрыто. /menu чтобы открыть", reply_markup=reply_markup)
def room(bot, update):
user_id = update.message.from_user.id
r = str(update.message.text).split(' ')
if len(r) == 2 and r[1].isdigit():
msg = 'Вы перешли в комнату {} ({})'.format(Room(r[1]).get().title, r[1])
Room(r[1]).change(user_id)
else:
msg = 'Используйте /room [комната]'
bot.sendMessage(chat_id=update.message.chat_id,
text=msg)
def info(bot, update):
user_id = update.message.from_user.id
r = str(update.message.text).split(' ')
# Checking second argument (user ID)
if len(r) == 2 and r[1].isdigit():
msg = User(user_id=r[1]).profile()
elif len(r) == 2:
msg = User(user_name=r[1]).profile()
else:
msg = User(user_id=user_id).profile()
bot.sendMessage(chat_id=update.message.chat_id,
text=msg)
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
def msg(bot, update):
text = update.message.text
if text == "[Мой профиль]":
profile(bot, update)
elif text == "[Инвентарь]":
inventory(bot, update)
elif text == "[Магазин]":
pass
elif text == "[Скрыть]":
hide(bot, update)
else:
echo(bot, update)
def echo(bot, update):
user_id = update.message.from_user.id
user_name = update.message.from_user.username
current_user = User(user_id=user_id).get()
q = db.query(User).filter(User.user_id != user_id, User.current_room == current_user.current_room).all()
for user in q:
now = datetime.strftime(update.message.date, "%H:%M:%S")
message = "@%s, [%s]\n%s" % (user_name, now, update.message.text)
bot.sendMessage(user.user_id,
text=message)
def error(update, err):
logger.warn('Update "%s" caused error "%s"' % (update, err))
def test(bot, update):
# For debugging
bot.sendMessage(update.message.chat_id,
text=str(update.message.from_user))
def business_pay(bot):
q = db.query(Inventory).filter(Inventory.business != None).all()
for inv in q:
item = Item(inv.business).get(model=True)
money = User(user_id=inv.user_id).get()
User(user_id=inv.user_id).update({'money': money + item.value})
logger.info("Money added to user {}".format(inv.user_id))
bot.sendMessage(inv.user_id, "Вам было начислено {}$ за ваш бизнес".format(item.value))