2016-05-22 20:45:51 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Telegram bot to play UNO in group chats
|
|
|
|
# Copyright (c) 2016 Jannes Höke <uno@jhoeke.de>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2022-02-10 14:15:15 +08:00
|
|
|
from telegram import ReplyKeyboardMarkup, Update
|
|
|
|
from telegram.ext import CommandHandler, Filters, MessageHandler, CallbackContext
|
2016-05-22 20:45:51 +08:00
|
|
|
|
|
|
|
from utils import send_async
|
|
|
|
from user_setting import UserSetting
|
|
|
|
from shared_vars import dispatcher
|
2016-05-24 21:49:23 +08:00
|
|
|
from locales import available_locales
|
|
|
|
from internationalization import _, user_locale
|
2016-05-22 20:45:51 +08:00
|
|
|
|
2016-05-23 01:21:51 +08:00
|
|
|
|
2016-05-22 20:45:51 +08:00
|
|
|
@user_locale
|
2022-02-10 14:15:15 +08:00
|
|
|
def show_settings(update: Update, context: CallbackContext):
|
2016-05-22 20:45:51 +08:00
|
|
|
chat = update.message.chat
|
|
|
|
|
|
|
|
if update.message.chat.type != 'private':
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id,
|
2016-05-22 20:45:51 +08:00
|
|
|
text=_("Please edit your settings in a private chat with "
|
|
|
|
"the bot."))
|
|
|
|
return
|
|
|
|
|
|
|
|
us = UserSetting.get(id=update.message.from_user.id)
|
|
|
|
|
|
|
|
if not us:
|
|
|
|
us = UserSetting(id=update.message.from_user.id)
|
|
|
|
|
|
|
|
if not us.stats:
|
2017-08-19 05:36:30 +08:00
|
|
|
stats = '📊' + ' ' + _("Enable statistics")
|
2016-05-22 20:45:51 +08:00
|
|
|
else:
|
2017-08-19 05:36:30 +08:00
|
|
|
stats = '❌' + ' ' + _("Delete all statistics")
|
2016-05-22 20:45:51 +08:00
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
kb = [[stats], ['🌍' + ' ' + _("Language")]]
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id, text='🔧' + ' ' + _("Settings"),
|
2016-05-22 20:45:51 +08:00
|
|
|
reply_markup=ReplyKeyboardMarkup(keyboard=kb,
|
|
|
|
one_time_keyboard=True))
|
|
|
|
|
|
|
|
|
|
|
|
@user_locale
|
2022-02-10 14:15:15 +08:00
|
|
|
def kb_select(update: Update, context: CallbackContext):
|
2016-05-22 20:45:51 +08:00
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
2022-02-10 14:15:15 +08:00
|
|
|
option = context.match[1]
|
2016-05-22 20:45:51 +08:00
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
if option == '📊':
|
2016-05-22 20:45:51 +08:00
|
|
|
us = UserSetting.get(id=user.id)
|
|
|
|
us.stats = True
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id, text=_("Enabled statistics!"))
|
2016-05-22 20:45:51 +08:00
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
elif option == '🌍':
|
2016-05-24 21:49:23 +08:00
|
|
|
kb = [[locale + ' - ' + descr]
|
|
|
|
for locale, descr
|
|
|
|
in sorted(available_locales.items())]
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id, text=_("Select locale"),
|
2016-05-24 21:49:23 +08:00
|
|
|
reply_markup=ReplyKeyboardMarkup(keyboard=kb,
|
2016-05-22 20:45:51 +08:00
|
|
|
one_time_keyboard=True))
|
|
|
|
|
2017-08-19 05:36:30 +08:00
|
|
|
elif option == '❌':
|
2016-05-22 20:45:51 +08:00
|
|
|
us = UserSetting.get(id=user.id)
|
|
|
|
us.stats = False
|
|
|
|
us.first_places = 0
|
|
|
|
us.games_played = 0
|
|
|
|
us.cards_played = 0
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id, text=_("Deleted and disabled statistics!"))
|
2016-05-22 20:45:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
@user_locale
|
2022-02-10 14:15:15 +08:00
|
|
|
def locale_select(update: Update, context: CallbackContext):
|
2016-05-22 20:45:51 +08:00
|
|
|
chat = update.message.chat
|
|
|
|
user = update.message.from_user
|
2022-02-10 14:15:15 +08:00
|
|
|
option = context.match[1]
|
2016-05-22 20:45:51 +08:00
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
if option in available_locales:
|
2016-05-22 20:45:51 +08:00
|
|
|
us = UserSetting.get(id=user.id)
|
|
|
|
us.lang = option
|
|
|
|
_.push(option)
|
2022-02-10 14:15:15 +08:00
|
|
|
send_async(context.bot, chat.id, text=_("Set locale!"))
|
2016-05-22 20:45:51 +08:00
|
|
|
_.pop()
|
|
|
|
|
2016-05-24 21:49:23 +08:00
|
|
|
def register():
|
|
|
|
dispatcher.add_handler(CommandHandler('settings', show_settings))
|
2022-02-10 14:15:15 +08:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.regex('^([' + '📊' +
|
|
|
|
'🌍' +
|
|
|
|
'❌' + ']) .+$'),
|
|
|
|
kb_select))
|
|
|
|
dispatcher.add_handler(MessageHandler(Filters.regex(r'^(\w\w_\w\w) - .*'),
|
|
|
|
locale_select))
|