From b9403a85a3fe1ca4580d3c784f2de19cf39a65a7 Mon Sep 17 00:00:00 2001 From: qwewqa <198e559dbd446d973355f415bdfa34@gmail.com> Date: Sat, 19 Dec 2020 23:22:09 -0500 Subject: [PATCH] add reaction chart difficulty selectors --- main.py | 3 + miyu_bot/commands/cogs/card.py | 14 +++++ miyu_bot/commands/cogs/music.py | 104 +++++++++++++++++++++----------- 3 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 miyu_bot/commands/cogs/card.py diff --git a/main.py b/main.py index 3889942..6051f04 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,10 @@ with open('config.json') as f: bot_token = json.load(f)['token'] bot = commands.Bot(command_prefix='!', case_insensitive=True) + asset_manager = AssetManager('assets') + +bot.load_extension('miyu_bot.commands.cogs.card') bot.load_extension('miyu_bot.commands.cogs.music') bot.load_extension('miyu_bot.commands.cogs.utility') diff --git a/miyu_bot/commands/cogs/card.py b/miyu_bot/commands/cogs/card.py new file mode 100644 index 0000000..ffe5702 --- /dev/null +++ b/miyu_bot/commands/cogs/card.py @@ -0,0 +1,14 @@ +import logging + +from discord.ext import commands + + +class Card(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + self.logger = logging.getLogger(__name__) + + + +def setup(bot): + bot.add_cog(Card(bot)) diff --git a/miyu_bot/commands/cogs/music.py b/miyu_bot/commands/cogs/music.py index 1bb4455..f9411b0 100644 --- a/miyu_bot/commands/cogs/music.py +++ b/miyu_bot/commands/cogs/music.py @@ -1,3 +1,4 @@ +import asyncio import logging import discord @@ -111,42 +112,73 @@ class Music(commands.Cog): return self.logger.info(f'Found "{song}" ({romanize(song.name)[1]}).') - chart: ChartMaster = song.charts[difficulty] - - chart_data = chart.load_chart_data() - note_counts = chart_data.get_note_counts() - - thumb = discord.File(song.jacket_path, filename='jacket.png') - render = discord.File(chart.image_path, filename='render.png') - - embed = discord.Embed(title=f'{song.name} [{difficulty.name}]') - embed.set_thumbnail(url=f'attachment://jacket.png') - embed.set_image(url=f'attachment://render.png') - - embed.add_field(name='Info', - value=f'Level: {chart.display_level}\n' - f'Unit: {song.special_unit_name or song.unit.name}\n' - f'Category: {song.category.name}\n' - f'BPM: {song.bpm}', - inline=False) - embed.add_field(name='Combo', - value=f'Max Combo: {chart.note_counts[ChartSectionType.Full].count}\n' - f'Taps: {note_counts["tap"]} (dark: {note_counts["tap1"]}, light: {note_counts["tap2"]})\n' - f'Scratches: {note_counts["scratch"]} (left: {note_counts["scratch_left"]}, right: {note_counts["scratch_right"]})\n' - f'Stops: {note_counts["stop"]} (head: {note_counts["stop_start"]}, tail: {note_counts["stop_end"]})\n' - f'Long: {note_counts["long"]} (head: {note_counts["long_start"]}, tail: {note_counts["long_end"]})\n' - f'Slide: {note_counts["slide"]} (tick: {note_counts["slide_tick"]}, flick {note_counts["slide_flick"]})', - inline=True) - embed.add_field(name='Ratings', - value=f'NTS: {round(chart.trends[0] * 100, 2)}%\n' - f'DNG: {round(chart.trends[1] * 100, 2)}%\n' - f'SCR: {round(chart.trends[2] * 100, 2)}%\n' - f'EFT: {round(chart.trends[3] * 100, 2)}%\n' - f'TEC: {round(chart.trends[4] * 100, 2)}%\n', - inline=True) - embed.set_footer(text='1 column = 10 seconds') - - await ctx.send(files=[thumb, render], embed=embed) + embeds, files = self.get_chart_embed_info(song) + + message = await ctx.send(files=files, embed=embeds[difficulty - 1]) + + reaction_emote_ids = [ + 790050636568723466, + 790050636489555998, + 790050636548276252, + 790050636225052694, + ] + + for emote_id in reaction_emote_ids: + await message.add_reaction(self.bot.get_emoji(emote_id)) + + def check(rxn, usr): + return usr == ctx.author and rxn.emoji.id in reaction_emote_ids + + while True: + try: + reaction, user = await self.bot.wait_for('reaction_add', timeout=60, check=check) + self.logger.debug(f'Reaction {reaction} from user {user}.') + emote_index = reaction_emote_ids.index(reaction.emoji.id) + await message.edit(embed=embeds[emote_index]) + await message.remove_reaction(reaction, user) + except asyncio.TimeoutError: + break + + def get_chart_embed_info(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 = song.charts[difficulty] + 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/{chart.image_path.relative_to(asset_manager.path).as_posix()}' + ) + + chart_data = chart.load_chart_data() + note_counts = chart_data.get_note_counts() + + embed.add_field(name='Info', + value=f'Level: {chart.display_level}\n' + f'Unit: {song.special_unit_name or song.unit.name}\n' + f'Category: {song.category.name}\n' + f'BPM: {song.bpm}', + inline=False) + embed.add_field(name='Combo', + value=f'Max Combo: {chart.note_counts[ChartSectionType.Full].count}\n' + f'Taps: {note_counts["tap"]} (dark: {note_counts["tap1"]}, light: {note_counts["tap2"]})\n' + f'Scratches: {note_counts["scratch"]} (left: {note_counts["scratch_left"]}, right: {note_counts["scratch_right"]})\n' + f'Stops: {note_counts["stop"]} (head: {note_counts["stop_start"]}, tail: {note_counts["stop_end"]})\n' + f'Long: {note_counts["long"]} (head: {note_counts["long_start"]}, tail: {note_counts["long_end"]})\n' + f'Slide: {note_counts["slide"]} (tick: {note_counts["slide_tick"]}, flick {note_counts["slide_flick"]})', + inline=True) + embed.add_field(name='Ratings', + value=f'NTS: {round(chart.trends[0] * 100, 2)}%\n' + f'DNG: {round(chart.trends[1] * 100, 2)}%\n' + f'SCR: {round(chart.trends[2] * 100, 2)}%\n' + f'EFT: {round(chart.trends[3] * 100, 2)}%\n' + f'TEC: {round(chart.trends[4] * 100, 2)}%\n', + inline=True) + embed.set_footer(text='1 column = 10 seconds') + + embeds.append(embed) + + return embeds, files def setup(bot):