Skip to content

Commit

Permalink
✨ Promote some other UNO bots in /start, /help and on game end
Browse files Browse the repository at this point in the history
  • Loading branch information
jh0ker committed Nov 11, 2023
1 parent fc39cad commit d190a5c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from player import Player
from errors import (AlreadyJoinedError, LobbyClosedError, NoGameInChatError,
NotEnoughPlayersError)

from promotions import send_promotion_async

class GameManager(object):
""" Manages all running games by using a confusing amount of dicts """
Expand Down Expand Up @@ -143,6 +143,7 @@ def end_game(self, chat, user):
"""

self.logger.info("Game in chat " + str(chat.id) + " ended")
send_promotion_async(chat, chance=0.15)

# Find the correct game instance to end
player = self.player_for_user_in_chat(user, chat)
Expand Down
34 changes: 34 additions & 0 deletions promotions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Promote other UNO bots
"""
import random


# Promotion messages and their weights
PROMOTIONS = {
"""
For a more modern UNO experience, <a href="https://t.me/uno9bot/uno">try out</a> the new <a href="https://t.me/uno9bot?start=ref-unobot">@uno9bot</a>.
""": 2.0,
"""
Also check out @UnoDemoBot, a newer version of this bot with exclusive modes and features!
""": 1.0,
}

def get_promotion():
""" Get a random promotion message """
return random.choices(list(PROMOTIONS.keys()), weights=list(PROMOTIONS.values()))[0]

def send_promotion(chat, chance=1.0):
""" (Maybe) send a promotion message """
if random.random() <= chance:
chat.send_message(get_promotion(), parse_mode='HTML')


def send_promotion_async(chat, chance=1.0):
""" Send a promotion message asynchronously """

from utils import dispatcher, error
try:
dispatcher.run_async(send_promotion, chat, chance=chance)
except Exception as e:
error(None, None, e)
12 changes: 10 additions & 2 deletions simple_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from utils import send_async
from shared_vars import dispatcher
from internationalization import _, user_locale
from promotions import send_promotion

@user_locale
def help_handler(update: Update, context: CallbackContext):
Expand Down Expand Up @@ -64,8 +65,15 @@ def help_handler(update: Update, context: CallbackContext):
"<a href=\"https://telegram.me/unobotnews\">update channel</a>"
" and buy an UNO card game.")

send_async(context.bot, update.message.chat_id, text=help_text,
parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def _send():
update.message.chat.send_message(
help_text,
parse_mode=ParseMode.HTML,
disable_web_page_preview=True,
)
send_promotion(update.effective_chat)

context.dispatcher.run_async(_send)

@user_locale
def modes(update: Update, context: CallbackContext):
Expand Down

0 comments on commit d190a5c

Please sign in to comment.