"""Has a single class: Player""" # discord imports import discord class Player: """This (abstract) class is a template for player objects for games""" @classmethod async def make(cls, member, game): p = cls() p.member = member p.dm = member.dm_channel or await member.create_dm() p.game = game return p def name(self): return self.member.name def __str__(self): return self.name() def reset(self): pass def other_players(self): return [p for p in self.game.player_list if p != self] async def send_normal(self, message): await self.dm.send(message) async def send_embed(self, desc, color): await self.dm.send(embed=discord.Embed(description=desc, color=color)) async def send_wrong(self, message): await self.send_embed(message, 0xff8000) async def send_confirmation(self, message): await self.send_embed(message, 0x00ff00) async def send_info(self, message): await self.send_embed(message, 0x00ffff) # TODO: refactor this function to make it understandable async def ask_choice(self, question, options): text = f"{question}\n" + f"{'='*len(question)}\n\n" + '\n'.join(f"[{str(i)}]({str(options[i])})" for i in range(len(options))) await self.dm.send(f"```md\n{text}```") # TODO: Basic Converters (https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#basic-converters) def check_num(self, choice, N): if not choice.isdigit(): raise ValueError(f"Your choice {choice} is not a number") if not 0 <= int(choice) < N: raise ValueError(f"Your choice {choice} is not in range 0 - {N-1}") # TODO: seems hacky, figure out a nicer way async def receive_choice(self, options, n_ans=1): while True: def check(choice): return choice.channel == self.dm and choice.author == self.member choice = (await self.game.bot.wait_for('message', check=check)).content.split() if not len(choice) == n_ans: await self.send_wrong(f"Please give {n_ans} numbers not {len(choice)}") continue try: for c in choice: self.check_num(c, len(options)) except ValueError as error: await self.send_wrong(str(error)) continue await self.send_confirmation(f"Received: {', '.join(choice)}") return [int(c) for c in choice] async def get_choice(self, question, options): await self.ask_choice(question, options) return (await self.receive_choice(options))[0]