Skip to content

Commit 41f305b

Browse files
committed
Merge pull request #59 from d-Rickyy-b/dev
Some optimizations
2 parents 33277eb + 83d0f39 commit 41f305b

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

main.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def game_commands(bot, update):
135135

136136
def error(bot, update, error):
137137
"""Log Errors caused by Updates."""
138+
if update is None:
139+
return
140+
138141
logger.warning('Update "%s" caused error "%s"', update, error)
139142

140143
db = DBwrapper.get_instance()
@@ -203,7 +206,7 @@ def start_cmd(bot, update):
203206
if game.players[0].user_id == user_id:
204207
game.start_game()
205208
else:
206-
update.message.reply_text("Only the creator can start the game")
209+
message.reply_text("Only the creator ({}) can start the game".format(game.players[0].first_name))
207210

208211

209212
def stop_cmd(bot, update):
@@ -219,8 +222,6 @@ def stop_cmd(bot, update):
219222

220223
def help_cmd(bot, update):
221224
# Explains commands to user
222-
223-
chat_id = update.message.chat_id
224225
db = DBwrapper.get_instance()
225226
lang_id = db.get_lang_id(update.message.from_user.id)
226227

@@ -287,23 +288,21 @@ def language_cmd(bot, update):
287288
reply_markup=lang_keyboard, message_id=update.message.message_id)
288289

289290

290-
def comment_cmd(bot, update):
291+
def comment_cmd(bot, update, args):
291292
user_id = update.message.from_user.id
292293
chat_id = update.message.chat_id
293294
first_name = update.message.from_user.first_name
294295
last_name = update.message.from_user.last_name
295296
username = update.message.from_user.username
296297
db = DBwrapper.get_instance()
297298
lang_id = db.get_lang_id(user_id)
298-
text = update.message.text
299-
params = text.split()
300299

301300
state_handler = StateHandler.get_instance()
302301
user = state_handler.get_user(user_id)
303302

304303
if user.get_state() == UserState.IDLE:
305-
if len(params) > 1:
306-
text = " ".join(params[1:])
304+
if len(args) > 1:
305+
text = " ".join(args)
307306
logger.debug("New comment! {}!".format(user_id))
308307

309308
bot.sendMessage(chat_id=chat_id, text=translate("userComment", lang_id))
@@ -312,7 +311,6 @@ def comment_cmd(bot, update):
312311
"New comment:\n\n{}\n\n{} | {} | {} | @{} | {}".format(text, user_id, first_name,
313312
last_name, username,
314313
lang_id))
315-
logger.debug("Set {}'s state to IDLE!".format(user_id))
316314
user.set_state(UserState.IDLE)
317315
else:
318316
# The user just wrote "/comment" -> Ask him to send a message
@@ -345,9 +343,7 @@ def cancel_cmd(bot, update):
345343

346344
def hide_cmd(bot, update):
347345
"""Hides the keyboard in the specified chat."""
348-
chat_id = update.message.chat_id
349-
reply_markup = ReplyKeyboardRemove()
350-
bot.sendMessage(chat_id=chat_id, text="\U0001F44D", reply_markup=reply_markup)
346+
update.message.reply_text("\U0001F44D", reply_markup=ReplyKeyboardRemove())
351347

352348

353349
def mentions_cmd(bot, update):
@@ -444,7 +440,7 @@ def restart(bot, update):
444440
hide_handler = CommandHandler('hide', hide_cmd)
445441
stats_handler = CommandHandler('stats', stats_cmd)
446442
language_handler = CommandHandler('language', language_cmd)
447-
comment_handler = CommandHandler('comment', comment_cmd)
443+
comment_handler = CommandHandler('comment', comment_cmd, pass_args=True)
448444
callback_handler = CallbackQueryHandler(callback_eval)
449445
users_handler = CommandHandler('users', users)
450446
answer_handler = CommandHandler('answer', answer)

userstate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# -*- coding: utf-8 -*-
2+
import logging
23

34

45
class UserState(object):
56
IDLE = 0
67
COMMENTING = 1
78
PLAYING = 2
9+
state_names = ["IDLE", "COMMENTING", "PLAYING"]
810

911
def __init__(self, user_id: int):
12+
self.logger = logging.getLogger(__name__)
1013
self._user_id = user_id
1114
self._state = UserState.IDLE
1215

1316
def get_state(self) -> int:
1417
return self._state
1518

1619
def set_state(self, state: int) -> None:
20+
self.logger.debug("Set {}'s state to {}!".format(self._user_id, self.state_names[state]))
1721
self._state = state
1822

1923
def get_userid(self) -> int:

0 commit comments

Comments
 (0)