async def p1p1(ctx: MtgContext) -> None: """`!p1p1` Summon a pack 1, pick 1 game.""" if is_p1p1_ready(ctx.channel.id): with ctx.typing(): lock(ctx.channel.id ) #Do not allow more than one p1p1 at the same time. cards = [ oracle.cards_by_name()[name] for name in random.sample(oracle.legal_cards(), 15) ] image_fetcher.download_image( cards ) #Preload the cards to reduce the delay encountered between introduction and the cards. await ctx.send( "Let's play the pack 1, pick 1 game. The rules are simple. You are drafting and you open this as your first pack. What do you take?" ) await ctx.post_cards(cards[0:5]) await ctx.post_cards(cards[5:10]) await ctx.post_cards(cards[10:]) unlock(ctx.channel.id) else: print( 'Pack1Pick1 was denied as it was still processing another one.' ) #This command will be heavy enough by itself, make sure the bot doesn't process it too much.
def test_imagedownload() -> None: filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='island.jpg') if fetcher_internal.acceptable_file(filepath): os.remove(filepath) c = [oracle.load_card('Island')] assert image_fetcher.download_image(c) is not None
async def post_cards( client: Client, cards: List[Card], channel: Channel, replying_to: Optional[Member] = None, additional_text: str = '' ) -> None: await client.send_typing(channel) if len(cards) == 0: await post_no_cards(client, channel, replying_to) return disable_emoji = channel.id in configuration.get_str('not_pd').split(',') cards = uniqify_cards(cards) if len(cards) > MAX_CARDS_SHOWN: cards = cards[:DEFAULT_CARDS_SHOWN] if len(cards) == 1: text = single_card_text_internal(client, cards[0], disable_emoji) else: text = ', '.join('{name} {legal} {price}'.format(name=card.name, legal=((emoji.legal_emoji(card)) if not disable_emoji else ''), price=((fetcher.card_price_string(card, True)) if card.get('mode', None) == '$' else '')) for card in cards) if len(cards) > MAX_CARDS_SHOWN: image_file = None else: image_file = image_fetcher.download_image(cards) if image_file is None: text += '\n\n' if len(cards) == 1: text += emoji.replace_emoji(cards[0].text, client) else: text += 'No image available.' text += additional_text if image_file is None: await client.send_message(channel, text) else: await send_image_with_retry(client, channel, image_file, text)
def test_fallbackimagedownload() -> None: filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='nalathni-dragon.jpg') if fetch_tools.acceptable_file(filepath): os.remove(filepath) c = [oracle.load_card('Nalathni Dragon')] assert image_fetcher.download_image(c) is not None
def test_split_cards() -> None: cards = oracle.load_cards(['Armed // Dangerous']) assert len(cards) == 1 assert image_fetcher.download_image(cards) is not None names = command.parse_queries('[Toil // Trouble]', False) assert len(names) == 1 results = command.results_from_queries(names) assert len(results) == 1
def test_noimageavailable() -> None: c = Card({ 'name': "Barry's Land", 'id': 0, 'multiverseid': 0, 'names': "Barry's Land" }) assert image_fetcher.download_image([c]) is None
def test_imagedownload(): filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='island.jpg') if fetcher_internal.acceptable_file(filepath): os.remove(filepath) c = [] c.extend(oracle.cards_from_query('Island')) assert image_fetcher.download_image(c) is not None
def test_split_cards(): cards = oracle.cards_from_query('Armed // Dangerous') assert len(cards) == 1 assert image_fetcher.download_image(cards) is not None names = command.parse_queries('[Toil // Trouble]') assert len(names) == 1 cards = command.cards_from_queries(names) assert len(cards) == 1
def test_noimageavailable() -> None: c = Card({ 'name': 'Made Up Card Name', 'id': 0, 'multiverseid': 0, 'names': 'Made Up Card Name', 'layout': 'normal' }) assert image_fetcher.download_image([c]) is None
def test_split_cards(): cards = oracle.load_cards(['Armed // Dangerous']) assert len(cards) == 1 assert image_fetcher.download_image(cards) is not None names = command.parse_queries('[Toil // Trouble]') assert len(names) == 1 results = command.results_from_queries(names, whoosh_search.WhooshSearcher())[0] assert len(results) == 1
async def post_cards(self, cards, channel, replying_to=None, additional_text=''): await self.client.send_typing(channel) not_pd = configuration.get('not_pd').split(',') disable_emoji = False if channel.id in not_pd: # or (channel.server and channel.server.id in not_pd): disable_emoji = True if len(cards) == 0: if replying_to is not None: text = '{author}: No matches.'.format(author=replying_to.mention) else: text = 'No matches.' await self.client.send_message(channel, text) return cards = command.uniqify_cards(cards) more_text = '' if len(cards) > 10: more_text = ' and ' + str(len(cards) - 4) + ' more.' cards = cards[:4] if len(cards) == 1: card = cards[0] mana = emoji.replace_emoji(''.join(card.mana_cost or []), self.client) legal = ' — ' + emoji.legal_emoji(card, True) if disable_emoji: legal = '' text = '{name} {mana} — {type}{legal}'.format(name=card.name, mana=mana, type=card.type, legal=legal) if card.bug_desc is not None: text += '\n:beetle:{rank} bug: {bug}'.format(bug=card.bug_desc, rank=card.bug_class) now_ts = dtutil.dt2ts(dtutil.now()) if card.bug_last_confirmed < now_ts - 60 * 60 * 24 * 60: text += ' (Last confirmed {time} ago.)'.format(time=dtutil.display_time(now_ts - card.bug_last_confirmed, 1)) else: text = ', '.join('{name} {legal}'.format(name=card.name, legal=(emoji.legal_emoji(card)) if not disable_emoji else '') for card in cards) text += more_text if len(cards) > 10: image_file = None else: image_file = image_fetcher.download_image(cards) if image_file is None: text += '\n\n' if len(cards) == 1: text += emoji.replace_emoji(cards[0].text, self.client) else: text += 'No image available.' text += '\n' + additional_text if image_file is None: await self.client.send_message(channel, text) else: message = await self.client.send_file(channel, image_file, content=text) if message and message.attachments and message.attachments[0]['size'] == 0: print('Message size is zero so resending') await self.client.delete_message(message) await self.client.send_file(channel, image_file, content=text)
def image(c: str = '') -> Response: names = c.split('|') try: requested_cards = oracle.load_cards(names) path = image_fetcher.download_image(requested_cards) if path is None: raise InternalServerError( f'Failed to get image for {c}') # type: ignore return send_file( os.path.abspath(path) ) # Send abspath to work around monolith root versus web root. except TooFewItemsException as e: print(e) return '', 400
def image(c: str = '') -> wrappers.Response: names = c.split('|') try: requested_cards = oracle.load_cards(names) path = image_fetcher.download_image(requested_cards) if path is None: raise InternalServerError(f'Failed to get image for {c}') return send_file( os.path.abspath(path) ) # Send abspath to work around monolith root versus web root. except TooFewItemsException as e: logger.info(f'Did not find an image for {c}: {e}') if len(names) == 1: return redirect( f'https://api.scryfall.com/cards/named?exact={c}&format=image', code=303) return make_response('', 400)
def image(c: str = '') -> Union[Tuple[str, int], wrappers.Response]: names = c.split('|') try: requested_cards = oracle.load_cards(names) path = image_fetcher.download_image(requested_cards) if path is None: raise InternalServerError( f'Failed to get image for {c}') # type: ignore return send_file( os.path.abspath(path) ) # Send abspath to work around monolith root versus web root. except TooFewItemsException as e: print(e) if len(names) == 1: return redirect( f'https://api.scryfall.com/cards/named?exact={c}&format=image', code=303) return '', 400