async def emoji(self, context: commands.Context, lottery_id: int, emoji: typing.Union[discord.Emoji, str]): message, channel, previous_emoji, nb_winners, time, organizer = \ await self.get_message_env(lottery_id, raise_if_not_found=True) if context.author != organizer: checker.has_any_mod_role(context, print_error=True) if isinstance(emoji, str) and emojis.emojis.count(emoji) != 1: raise exceptions.ForbiddenEmoji(emoji) previous_reaction = utils.try_get( message.reactions, error=exceptions.MissingEmoji(previous_emoji), emoji=previous_emoji) await previous_reaction.remove(zbot.bot.user) await message.add_reaction(emoji) embed = self.build_announce_embed(emoji, nb_winners, organizer, time, self.guild.roles) await message.edit(embed=embed) job_id = self.pending_lotteries[message.id]['_id'] lottery_data = { 'emoji_code': emoji if isinstance(emoji, str) else emoji.id } zbot.db.update_job_data(self.JOBSTORE, job_id, lottery_data) self.pending_lotteries[message.id].update(lottery_data) await context.send( f"Émoji du tirage au sort d'identifiant `{lottery_id}` remplacé par \"{emoji}\" : " f"<{message.jump_url}>")
async def count_votes(message, emoji_list, is_exclusive, required_role_name): """Count the votes and compute the results of the poll.""" reactions = [ utils.try_get(message.reactions, error=exceptions.MissingEmoji(emoji), emoji=emoji) for emoji in emoji_list ] votes = { reaction.emoji: await reaction.users().flatten() for reaction in reactions } results = {} for emoji, voters in votes.items(): valid_votes_count = 0 for voter in voters: if voter.bot: continue # Exclude bot votes if is_exclusive and any([ voter in votes[other_emoji] for other_emoji in votes if other_emoji != emoji ]): continue # Only count vote of voters having voted once in exclusive mode if required_role_name and not checker.has_guild_role( message.guild, voter, required_role_name): continue # Only count vote of voters having the required role, if set valid_votes_count += 1 results[emoji] = valid_votes_count return reactions, results
async def draw(message, emoji, nb_winners, seed=None): await Lottery.prepare_seed(seed) reaction = utils.try_get(message.reactions, error=exceptions.MissingEmoji(emoji), emoji=emoji) players, winners = await Lottery.pick_winners(message.guild, reaction, nb_winners) return players, reaction, winners
async def emojis(self, context: commands.Context, poll_id: int, emoji_list: converter.to_emoji_list, *, options=""): message, channel, previous_emoji_list, is_exclusive, required_role_name, time, organizer = \ await self.get_message_env(poll_id, raise_if_not_found=True) if context.author != organizer: checker.has_any_mod_role(context, print_error=True) if not context.author.permissions_in(channel).send_messages: raise exceptions.ForbiddenChannel(channel) if not emoji_list: raise commands.MissingRequiredArgument( context.command.params['emoji_list']) if len(emoji_list) > 20: raise exceptions.OversizedArgument(f"{len(emoji_list)} emojis", "20 emojis") required_role_name = utils.get_option_value(options, 'role') if required_role_name: utils.try_get( # Raise if role does not exist self.guild.roles, error=exceptions.UnknownRole(required_role_name), name=required_role_name) is_exclusive = utils.is_option_enabled(options, 'exclusive') previous_reactions = [ utils.try_get(message.reactions, error=exceptions.MissingEmoji(previous_emoji), emoji=previous_emoji) for previous_emoji in previous_emoji_list ] for previous_reaction in previous_reactions: await previous_reaction.remove(zbot.bot.user) for emoji in emoji_list: await message.add_reaction(emoji) embed = self.build_announce_embed(message.embeds[0].description, is_exclusive, required_role_name, organizer, time, self.guild.roles) await message.edit(embed=embed) job_id = self.pending_polls[message.id]['_id'] poll_data = { 'emoji_codes': list(map(lambda e: e if isinstance(e, str) else e.id, emoji_list)), 'is_exclusive': is_exclusive, 'required_role_name': required_role_name } zbot.db.update_poll_data(job_id, poll_data) self.pending_polls[message.id].update(poll_data) await context.send( f"Émojis du sondage d'identifiant `{poll_id}` mis à jour : <{message.jump_url}>" )
async def cancel(self, context: commands.Context, poll_id: int): message, _, emoji_list, _, _, _, organizer = await self.get_message_env( poll_id, raise_if_not_found=False) if context.author != organizer: checker.has_any_mod_role(context, print_error=True) if message: for emoji in emoji_list: reaction = utils.try_get( message.reactions, error=exceptions.MissingEmoji(emoji), emoji=emoji ) await reaction.remove(zbot.bot.user) embed = discord.Embed( title=f"Sondage __annulé__ par {context.author.display_name}", description=message.embeds[0].description if message.embeds[0].description else "", color=Poll.EMBED_COLOR ) embed.set_author(name=f"Organisateur : @{organizer.display_name}", icon_url=organizer.avatar_url) await message.edit(embed=embed) await message.unpin() self.remove_pending_poll(message.id, cancel_job=True) await context.send(f"Sondage d'identifiant `{poll_id}` annulé : <{message.jump_url}>")
async def cancel(self, context: commands.Context, lottery_id: int): message, _, emoji, _, _, organizer = await self.get_message_env( lottery_id, raise_if_not_found=False) if context.author != organizer: checker.has_any_mod_role(context, print_error=True) if message: reaction = utils.try_get(message.reactions, error=exceptions.MissingEmoji(emoji), emoji=emoji) await reaction.remove(zbot.bot.user) embed = discord.Embed( title= f"Tirage au sort __annulé__ par {context.author.display_name}", color=Lottery.EMBED_COLOR) embed.set_author(name=f"Organisateur : @{organizer.display_name}", icon_url=organizer.avatar_url) await message.edit(embed=embed) self.remove_pending_lottery(message.id, cancel_job=True) await context.send( f"Tirage au sort d'identifiant `{lottery_id}` annulé : <{message.jump_url}>" )