async def discard(self, ctx, name, group=None): id_idol = await self.can_give(ctx, ctx.author, name, group) if not id_idol: return def check(message): return message.author == ctx.author \ and message.channel == ctx.message.channel \ and (message.content.lower() == 'yes' or message.content.lower() == 'y' or message.content.lower() == 'no' or message.content.lower() == 'n') await ctx.send( f'{ctx.author.mention}, are you sure you want to discard **{name}**? (y|yes or n|no)\n' ) try: msg = await self.bot.wait_for('message', timeout=30, check=check) except asyncio.TimeoutError: await ctx.message.add_reaction(u"\u274C") await ctx.send('Discard is cancelled.') else: if msg.content.lower() == 'y' or msg.content.lower() == 'yes': DatabaseDeck.get().give_to(ctx.guild.id, id_idol, ctx.author.id, None) await ctx.message.add_reaction(u"\u2705") await msg.add_reaction(u"\u2705") else: await ctx.send('Discard is cancelled.')
async def wishlist(self, ctx): ids = DatabaseDeck.get().get_wishlist(ctx.guild.id, ctx.author.id) description = '' username = ctx.author.name if ctx.author.nick is None else ctx.author.nick nb_wish = DatabaseDeck.get().get_nb_wish(ctx.guild.id, ctx.author.id) max_wish = DatabaseDeck.get().get_max_wish(ctx.guild.id, ctx.author.id) for id_idol in ids: current_image = DatabaseDeck.get().get_idol_current_image( ctx.guild.id, id_idol) idol = DatabaseIdol.get().get_idol_information( id_idol, current_image) id_owner = DatabaseDeck.get().idol_belongs_to( ctx.guild.id, id_idol) emoji = '' if id_owner: if id_owner == ctx.author.id: emoji = u"\u2705" else: emoji = u"\u274C" description += f'**{idol["name"]}** *{idol["group"]}* {emoji}\n' await ctx.send(embed=discord.Embed( title=f'Wish list of {username} ({nb_wish}/{max_wish})', description=description))
async def set_time_to_claim(self, ctx, time_to_claim): try: time_to_claim = int(time_to_claim) except ValueError: await ctx.send('Please enter time to claim as number.') await ctx.message.add_reaction(u"\u274C") return DatabaseDeck.get().set_time_to_claim(ctx.guild.id, time_to_claim) await ctx.message.add_reaction(u"\u2705")
async def set_nb_rolls_per_hour(self, ctx, nb_rolls): try: nb_rolls = int(nb_rolls) except ValueError: await ctx.send('Please enter number of rolls as number.') await ctx.message.add_reaction(u"\u274C") return DatabaseDeck.get().set_nb_rolls_per_hour(ctx.guild.id, nb_rolls) await ctx.message.add_reaction(u"\u2705")
async def set_claiming_interval(self, ctx, interval): try: interval = int(interval) except ValueError: await ctx.send('Please enter minutes as number.') await ctx.message.add_reaction(u"\u274C") return DatabaseDeck.get().set_claiming_interval(ctx.guild.id, interval) await ctx.message.add_reaction(u"\u2705")
async def can_give(ctx, author, name, group=None): """Return idol id if the user can give, None otherwise.""" ## Find idol id name = name.strip() if group: group = group.strip() id_idol = None if group: id_idol = DatabaseIdol.get().get_idol_group_id(name, group) else: ids = DatabaseIdol.get().get_idol_ids(name) if ids: id_idol = ids[0] if not id_idol: msg = f'I searched everywhere for **{name}**' if group: msg += f' in the group *{group}*' msg += ' and I couldn\'t find anything.\nPlease check the command.' await ctx.send(msg) return None ## Check if idol belongs to author owner = DatabaseDeck.get().idol_belongs_to(ctx.guild.id, id_idol) if not owner or owner != author.id: await ctx.message.add_reaction(u"\u274C") await ctx.send( f'You don\'t own **{name}**{" from *" + group + "* " if group else ""}...' ) return None return id_idol
async def wishremove(self, ctx, name, group=None): name = name.strip() if group: group = group.strip() id_idol = None if group: id_idol = DatabaseIdol.get().get_idol_group_id(name, group) else: ids = DatabaseIdol.get().get_idol_ids(name) if ids: id_idol = ids[0] if not id_idol: await ctx.message.add_reaction(u"\u274C") await ctx.send( f'Idol **{name}**{" from *" + group + "* " if group else ""} not found.' ) return if DatabaseDeck.get().remove_from_wishlist(ctx.guild.id, id_idol, ctx.author.id): # Green mark await ctx.message.add_reaction(u"\u2705") else: # Red cross await ctx.message.add_reaction(u"\u274C") await ctx.send('You don\'t have this idol in your wish list.')
async def set_max_wish(self, ctx, max_wish): if not ctx.message.mentions: await ctx.send('Please mention a user.') await ctx.message.add_reaction(u"\u274C") return user = ctx.message.mentions[0] try: max_wish = int(max_wish) except ValueError: await ctx.send('Please enter a number.') await ctx.message.add_reaction(u"\u274C") return DatabaseDeck.get().set_max_wish(ctx.guild.id, user.id, max_wish) await ctx.message.add_reaction(u"\u2705")
async def show_configuration(self, ctx): config = DatabaseDeck.get().get_server_configuration(ctx.guild.id) description = f'Claim interval: {config["claim_interval"]} minutes\n' \ f'Time to claim an idol: {config["time_to_claim"]} seconds\n' \ f'Number of rolls per hour: {config["rolls_per_hour"]}' embed = discord.Embed(title=f'Server *{ctx.guild.name}* configuration', description=description) await ctx.send(embed=embed)
async def list(self, ctx, *, name): ids = DatabaseIdol.get().get_idol_ids(name) description = '' if ids else 'No idols found' for id_idol in ids: image_number = DatabaseDeck.get().get_idol_current_image(ctx.guild.id, id_idol) idol = DatabaseIdol.get().get_idol_information(id_idol, image_number) description += f'**{idol["name"]}** *{idol["group"]}*\n' embed = discord.Embed(title=f'{name} idols', description=description) await ctx.send(embed=embed)
async def discard_all(self, ctx): letters = string.ascii_letters random_string = 'cancel' while random_string == 'cancel': random_string = ''.join(secrets.choice(letters) for i in range(5)) def check(message): return message.author == ctx.author \ and message.channel == ctx.message.channel \ and (message.content == random_string or message.content.lower() == 'cancel') await ctx.send( f'{ctx.author.mention}, are you sure you want to discard **all your deck**?\n' f'This cannot be undone! Please type *{random_string}* (with case) to confirm ' f'or *cancel* to cancel.') try: msg = await self.bot.wait_for('message', timeout=30, check=check) except asyncio.TimeoutError: await ctx.message.add_reaction(u"\u274C") await ctx.send('Discard is cancelled.') else: if msg.content.lower() == 'cancel': await ctx.message.add_reaction(u"\u274C") await ctx.send('Discard is cancelled.') return ids_deck = DatabaseDeck.get().get_user_deck( ctx.guild.id, ctx.author.id) for id_idol in ids_deck: DatabaseDeck.get().give_to(ctx.guild.id, id_idol, ctx.author.id, None) await ctx.message.add_reaction(u"\u2705") await msg.add_reaction(u"\u2705")
async def profile(self, ctx): user = ctx.author if not ctx.message.mentions else ctx.message.mentions[ 0] ids_deck = DatabaseDeck.get().get_user_deck(ctx.guild.id, user.id) async def send_embed(desc): embed = discord.Embed( title=user.name if user.nick is None else user.nick, description=desc) embed.set_thumbnail(url=user.avatar_url) await ctx.send(embed=embed) idols_text = [] description = '' for id_idol in ids_deck: current_image = DatabaseDeck.get().get_idol_current_image( ctx.guild.id, id_idol) idol = DatabaseIdol.get().get_idol_information( id_idol, current_image) idols_text.append(f'**{idol["name"]}** *{idol["group"]}*') idols_text.sort() current_page = 1 nb_per_page = 20 max_page = math.ceil(len(idols_text) / float(nb_per_page)) embed = discord.Embed( title=user.name if user.nick is None else user.nick, description='\n'.join([ idol for idol in idols_text[(current_page - 1) * nb_per_page:current_page * nb_per_page] ])) embed.set_thumbnail(url=user.avatar_url) embed.set_footer(text=f'{current_page} \\ {max_page}') msg = await ctx.send(embed=embed) if max_page > 1: # Page handler left_emoji = '\U00002B05' right_emoji = '\U000027A1' await msg.add_reaction(left_emoji) await msg.add_reaction(right_emoji) def check(reaction, user): return user != self.bot.user and (str(reaction.emoji) == left_emoji or str(reaction.emoji) == right_emoji) \ and reaction.message.id == msg.id timeout = False while not timeout: try: reaction, user = await self.bot.wait_for('reaction_add', timeout=60, check=check) except asyncio.TimeoutError: await msg.clear_reaction(left_emoji) await msg.clear_reaction(right_emoji) timeout = True else: old_page = current_page if reaction.emoji == left_emoji: current_page = current_page - 1 if current_page > 1 else max_page if reaction.emoji == right_emoji: current_page = current_page + 1 if current_page < max_page else 1 await msg.remove_reaction(reaction.emoji, user) # Refresh embed message with the new text if old_page != current_page: embed = discord.Embed( title=user.name if user.nick is None else user.nick, description='\n'.join([ idol for idol in idols_text[ (current_page - 1) * nb_per_page:current_page * nb_per_page] ])) embed.set_thumbnail(url=user.avatar_url) embed.set_footer(text=f'{current_page} \\ {max_page}') await msg.edit(embed=embed)