From 4200658897a3c90d0c5e7d0faaa1332740fe1ebe Mon Sep 17 00:00:00 2001 From: qwewqa <198e559dbd446d973355f415bdfa34@gmail.com> Date: Sat, 23 Jan 2021 13:35:32 -0500 Subject: [PATCH] add base asset url to bot class --- miyu_bot/bot/bot.py | 2 + miyu_bot/commands/cogs/card.py | 8 ++- miyu_bot/commands/cogs/event.py | 18 ++----- miyu_bot/commands/cogs/music.py | 54 ++++++-------------- miyu_bot/commands/common/asset_paths.py | 2 +- miyu_bot/commands/common/reaction_message.py | 2 - 6 files changed, 26 insertions(+), 60 deletions(-) diff --git a/miyu_bot/bot/bot.py b/miyu_bot/bot/bot.py index 4b03c91..ecf8254 100644 --- a/miyu_bot/bot/bot.py +++ b/miyu_bot/bot/bot.py @@ -10,6 +10,8 @@ class D4DJBot(commands.Bot): asset_filters: MasterFilterManager aliases: NameAliases + asset_url = 'https://qwewqa.github.io/d4dj-dumps/' + def __init__(self, assets, asset_filters, *args, **kwargs): self.assets = assets self.asset_filters = asset_filters diff --git a/miyu_bot/commands/cogs/card.py b/miyu_bot/commands/cogs/card.py index 206a4d8..9ed489b 100644 --- a/miyu_bot/commands/cogs/card.py +++ b/miyu_bot/commands/cogs/card.py @@ -10,6 +10,7 @@ from discord.ext import commands from miyu_bot.bot.bot import D4DJBot from miyu_bot.commands.common.argument_parsing import ParsedArguments, parse_arguments, ArgumentError +from miyu_bot.commands.common.asset_paths import get_card_icon_path, get_card_art_path from miyu_bot.commands.common.emoji import rarity_emoji_ids, attribute_emoji_ids_by_attribute_id, \ unit_emoji_ids_by_unit_id, parameter_bonus_emoji_ids_by_parameter_id from miyu_bot.commands.common.formatting import format_info @@ -173,11 +174,8 @@ class Card(commands.Cog): def get_card_embed(self, card: CardMaster, limit_break): embed = discord.Embed(title=self.format_card_name(card)) - card_hash = hash_master(card) - icon_path = card.icon_path(limit_break) - thumb_url = f'https://qwewqa.github.io/d4dj-dumps/cards/icons/{icon_path.stem}_{card_hash}{icon_path.suffix}' - art_path = card.art_path(limit_break) - art_url = f'https://qwewqa.github.io/d4dj-dumps/cards/art/{art_path.stem}_{card_hash}{art_path.suffix}' + thumb_url = self.bot.asset_url + get_card_icon_path(card, limit_break) + art_url = self.bot.asset_url + get_card_art_path(card, limit_break) embed.set_thumbnail(url=thumb_url) embed.set_image(url=art_url) diff --git a/miyu_bot/commands/cogs/event.py b/miyu_bot/commands/cogs/event.py index 52835f6..5bef60c 100644 --- a/miyu_bot/commands/cogs/event.py +++ b/miyu_bot/commands/cogs/event.py @@ -12,6 +12,7 @@ from pytz import UnknownTimeZoneError from miyu_bot.bot.bot import D4DJBot from miyu_bot.commands.common.argument_parsing import parse_arguments, ArgumentError +from miyu_bot.commands.common.asset_paths import get_event_logo_path from miyu_bot.commands.common.emoji import attribute_emoji_ids_by_attribute_id, unit_emoji_ids_by_unit_id, \ parameter_bonus_emoji_ids_by_parameter_id, \ event_point_emoji_id @@ -85,10 +86,7 @@ class Event(commands.Cog): def get_event_embed(self, event, timezone): embed = discord.Embed(title=event.name) - event_hash = hash_master(event) - event_logo_path = event.logo_path - embed.set_thumbnail( - url=f'https://qwewqa.github.io/d4dj-dumps/events/logos/{event_logo_path.stem}_{event_hash}{event_logo_path.suffix}') + embed.set_thumbnail(url=self.bot.asset_url + get_event_logo_path(event)) duration_hour_part = round((event.duration.seconds / 3600), 2) duration_hour_part = duration_hour_part if not duration_hour_part.is_integer() else int(duration_hour_part) @@ -178,10 +176,7 @@ class Event(commands.Cog): embed = discord.Embed(title=event.name) - event_hash = hash_master(event) - event_logo_path = event.logo_path - embed.set_thumbnail( - url=f'https://qwewqa.github.io/d4dj-dumps/events/logos/{event_logo_path.stem}_{event_hash}{event_logo_path.suffix}') + embed.set_thumbnail(url=self.bot.asset_url + get_event_logo_path(event)) progress = None @@ -238,12 +233,9 @@ class Event(commands.Cog): async with aiohttp.ClientSession() as session: async with session.get('http://www.projectdivar.com/eventdata/t20') as resp: leaderboard = await resp.json(encoding='utf-8') - event = self.bot.asset_filters.events.get_latest_event(ctx) - event_hash = hash_master(event) - event_logo_path = event.logo_path - embed = discord.Embed(title=f'{event.name} t20').set_thumbnail( - url=f'https://qwewqa.github.io/d4dj-dumps/events/logos/{event_logo_path.stem}_{event_hash}{event_logo_path.suffix}') + embed = discord.Embed(title=f'{event.name} t20') + embed.set_thumbnail(url=self.bot.asset_url + get_event_logo_path(event)) max_points_digits = len(str(leaderboard[0]['points'])) nl = "\n" update_date = dateutil.parser.isoparse(leaderboard[0]["date"]).replace(microsecond=0) diff --git a/miyu_bot/commands/cogs/music.py b/miyu_bot/commands/cogs/music.py index 18236b4..7aafc7e 100644 --- a/miyu_bot/commands/cogs/music.py +++ b/miyu_bot/commands/cogs/music.py @@ -14,10 +14,10 @@ from discord.ext import commands from miyu_bot.bot.bot import D4DJBot from miyu_bot.commands.common.argument_parsing import parse_arguments, ArgumentError, list_operator_for +from miyu_bot.commands.common.asset_paths import get_chart_image_path, get_music_jacket_path from miyu_bot.commands.common.emoji import difficulty_emoji_ids from miyu_bot.commands.common.formatting import format_info from miyu_bot.commands.common.fuzzy_matching import romanize -from miyu_bot.bot.master_asset_manager import hash_master from miyu_bot.commands.common.reaction_message import run_tabbed_message, run_paged_message @@ -63,14 +63,8 @@ class Music(commands.Cog): return self.logger.info(f'Found song "{song}" ({romanize(song.name)}).') - try: - thumb = discord.File(song.jacket_path, filename='jacket.png') - except FileNotFoundError: - # Just a fallback - thumb = discord.File(self.bot.assets.path / 'ondemand/stamp/stamp_10006.png', filename='jacket.png') - embed = discord.Embed(title=song.name) - embed.set_thumbnail(url=f'attachment://jacket.png') + embed.set_thumbnail(url=self.bot.asset_url + get_music_jacket_path(song)) artist_info = { 'Lyricist': song.lyricist, @@ -97,7 +91,7 @@ class Music(commands.Cog): value=format_info(music_info), inline=False) - await ctx.send(files=[thumb], embed=embed) + await ctx.send(embed=embed) @commands.command(name='chart', aliases=[], @@ -116,10 +110,10 @@ class Music(commands.Cog): return self.logger.info(f'Found song "{song}" ({romanize(song.name)}).') - embeds, files = self.get_chart_embed_info(song) + embeds = self.get_chart_embeds(song) # Difficulty enum easy-expert are 1-4, one more than the embed index - asyncio.ensure_future(run_tabbed_message(ctx, self.reaction_emojis, embeds, files, difficulty - 1)) + asyncio.ensure_future(run_tabbed_message(ctx, self.reaction_emojis, embeds, None, difficulty - 1)) @commands.command(name='sections', aliases=['mixes'], @@ -143,9 +137,9 @@ class Music(commands.Cog): return self.logger.info(f'Found song "{song}" ({romanize(song.name)}).') - embeds, files = self.get_mix_embed_info(song) + embeds = self.get_mix_embeds(song) - asyncio.ensure_future(run_tabbed_message(ctx, self.reaction_emojis, embeds, files, difficulty - 1)) + asyncio.ensure_future(run_tabbed_message(ctx, self.reaction_emojis, embeds, None, difficulty - 1)) @commands.command(name='songs', aliases=['songsearch', 'song_search'], @@ -224,27 +218,14 @@ class Music(commands.Cog): embed = discord.Embed(title=f'Song Search "{arg}"' if arg else 'Songs') asyncio.ensure_future(run_paged_message(ctx, embed, listing)) - def get_chart_embed_info(self, song): + def get_chart_embeds(self, song): embeds = [] - try: - thumb = discord.File(song.jacket_path, filename='jacket.png') - except FileNotFoundError: - # fallback - thumb = discord.File(self.bot.assets.path / 'ondemand/stamp/stamp_10006.png', filename='jacket.png') - - files = [thumb] - for difficulty in [ChartDifficulty.Easy, ChartDifficulty.Normal, ChartDifficulty.Hard, ChartDifficulty.Expert]: chart = song.charts[difficulty] - chart_hash = hash_master(chart) - chart_path = chart.image_path embed = discord.Embed(title=f'{song.name} [{chart.difficulty.name}]') - embed.set_thumbnail(url=f'attachment://jacket.png') - embed.set_image( - url=f'https://qwewqa.github.io/d4dj-dumps/music/charts/{chart_path.stem}_{chart_hash}{chart_path.suffix}' - ) - + embed.set_thumbnail(url=self.bot.asset_url + get_music_jacket_path(song)) + embed.set_image(url=self.bot.asset_url + get_chart_image_path(chart)) chart_data = chart.load_chart_data() note_counts = chart_data.get_note_counts() @@ -274,21 +255,16 @@ class Music(commands.Cog): embeds.append(embed) - return embeds, files + return embeds - def get_mix_embed_info(self, song): + def get_mix_embeds(self, song): embeds = [] - files = [discord.File(song.jacket_path, filename=f'jacket.png')] for difficulty in [ChartDifficulty.Easy, ChartDifficulty.Normal, ChartDifficulty.Hard, ChartDifficulty.Expert]: chart: ChartMaster = song.charts[difficulty] - chart_hash = hash_master(chart) - mix_path = chart.mix_path embed = discord.Embed(title=f'Mix: {song.name} [{chart.difficulty.name}]') - embed.set_thumbnail(url=f'attachment://jacket.png') - embed.set_image( - url=f'https://qwewqa.github.io/d4dj-dumps/music/charts/{mix_path.stem}_{chart_hash}{mix_path.suffix}' - ) + embed.set_thumbnail(url=self.bot.asset_url + get_music_jacket_path(song)) + embed.set_image(url=self.bot.asset_url + get_chart_image_path(chart)) note_counts = chart.note_counts mix_info = chart.mix_info @@ -329,7 +305,7 @@ class Music(commands.Cog): embeds.append(embed) - return embeds, files + return embeds def parse_chart_args(self, arg: str) -> Tuple[str, ChartDifficulty]: split_args = arg.split() diff --git a/miyu_bot/commands/common/asset_paths.py b/miyu_bot/commands/common/asset_paths.py index cda7990..a07634a 100644 --- a/miyu_bot/commands/common/asset_paths.py +++ b/miyu_bot/commands/common/asset_paths.py @@ -9,7 +9,7 @@ from miyu_bot.bot.master_asset_manager import hash_master def _get_asset_path(master, parent, path): - return Path(parent) / f'{path.stem}_{hash_master(master)}{path.suffix}' + return str((Path(parent) / f'{path.stem}_{hash_master(master)}{path.suffix}').as_posix()) music_dir = Path('.') / 'music' diff --git a/miyu_bot/commands/common/reaction_message.py b/miyu_bot/commands/common/reaction_message.py index 486d0be..8d64812 100644 --- a/miyu_bot/commands/common/reaction_message.py +++ b/miyu_bot/commands/common/reaction_message.py @@ -10,8 +10,6 @@ AnyEmoji = Union[str, Emoji] async def run_tabbed_message(ctx: Context, emojis: List[AnyEmoji], embeds: List[Embed], files=None, starting_index=0, timeout=300): - if not files: - files = [] if len(emojis) != len(embeds): raise ValueError('Emojis and embeds must have the same number of elements.')