async def collect_category(self, ctx): """Presents an Embed and finds the category from the Reaction the user provided""" embed = discord.Embed(type="rich", colour=utils.generate_random_colour()) embed.set_author(name="Role Categories") embed.description = "Click on an emoji at the bottom of this panel that corresponds with the category of roles you want to assign yourself. Another panel will pop up that will ask you for the specific roles." current_emoji_index = 0 for cat, roles_array in ROLE_CATEGORIES.items(): embed.add_field(name="{} {}".format(utils.resolve_emoji_from_alphabet(utils.ALPHABET[current_emoji_index]), cat), value="`{}`".format("`\n`".join(roles_array))) current_emoji_index += 1 reaction_message = await self.bot.send_message(ctx.message.channel, embed=embed) for i, cat in enumerate(list(ROLE_CATEGORIES)): await self.bot.add_reaction(reaction_message, utils.resolve_emoji_from_alphabet(utils.ALPHABET[i])) def check(reaction, user): if user.id != ctx.message.author.id: return False letter = utils.resolve_letter_from_emoji(reaction.emoji) for i in range(0, len(ROLE_CATEGORIES)): if letter == utils.ALPHABET[i]: return True return False res = await self.bot.wait_for_reaction(message=reaction_message, check=check, timeout=30) index = utils.ALPHABET.index(utils.resolve_letter_from_emoji(res.reaction.emoji)) return { "category": list(ROLE_CATEGORIES)[index], "message": reaction_message }
async def define(self, ctx): """Get the definition of a specified word, like '!define computer'.""" await self.bot.send_typing(ctx.message.channel) if len(ctx.message.content.split(' ')) == 1: return await self.bot.send_message(ctx.message.channel, "See how to use this command by running `{}help define`".format(ctx.prefix)) user_word = ' '.join(ctx.message.content.split(' ')[1:]) embed = discord.Embed(colour=utils.generate_random_colour()) embed.timestamp = datetime.now() embed.set_author(name="Definitions for {}".format(user_word), url=OXFORD_BASE_SEARCH + user_word, icon_url=ctx.message.author.avatar_url) embed.set_thumbnail(url=OXFORD_IMAGE_URL) embed.set_footer(text="Powered by the Oxford Dictionary API") lemmatron_data = await self.run_lemmatron(ctx) if isinstance(lemmatron_data, int) and lemmatron_data > 0: return definitions = await self.fetch_definitions(ctx, parse.quote('_'.join(lemmatron_data["headword"].split(' ')).lower())) if isinstance(definitions, int) and definitions > 0: return for word in definitions: senses = [] for entry in word["entries"]: for i, sense in enumerate(entry["senses"]): if "definitions" not in sense: continue definition = "\n{} `{}`".format(utils.resolve_emoji_from_alphabet(utils.ALPHABET[i]), sense["definitions"][0]) examples = list(map(lambda x: x["text"], sense["examples"] if "examples" in sense else [])) senses.append({ "definition": definition, "examples": examples }) definition_text = "" for sense in senses: definition_text += "\n{}{}{}{}".format(sense["definition"], "\n• *" if sense["examples"] else "", "*\n• *".join(sense["examples"]), '*' if sense["examples"] else "") name = "{} ({})".format(word["text"], word["lexicalCategory"]) embed.add_field(name=name, value=definition_text, inline=False) if lemmatron_data["message"]: await self.bot.edit_message(lemmatron_data["message"], embed=embed) else: await self.bot.send_message(ctx.message.channel, embed=embed)
async def on_reaction_remove(reaction, user): """The 'on_reaction_remove' event""" if user.id == BOT.user.id: return if reaction.message.id in BOT.ongoing_polls: current_poll = BOT.ongoing_polls[reaction.message.id] # If the reaction is the same as any of the existing reactions if any(utils.resolve_emoji_from_alphabet(option) == reaction.emoji for option in current_poll.results.keys()): # Remove the user from the respective result object deselected_option = utils.resolve_letter_from_emoji(reaction.emoji) if user.id in current_poll.results[deselected_option]: current_poll.remove_vote(deselected_option, user) # Update the original poll message return await BOT.edit_message(current_poll.question_message, embed=current_poll.embed)
async def collect_roles(self, category, reaction_message, ctx): """Presents another Embed and gives the user the roles they selected""" embed = discord.Embed(type="rich", colour=utils.generate_random_colour()) embed.set_author(name="Select roles from {}".format(category)) embed.description = "Click on the emojis at the bottom of this panel that correspond with the roles you want added to your profile. The panel will close itself in 30 seconds, allowing you to select as many roles as you want." if category is list(ROLE_CATEGORIES)[0]: embed.set_footer(text="Please select one role.") else: embed.set_footer(text="You have 30 seconds to choose your roles") roles_list = [] for i, r in enumerate(ROLE_CATEGORIES[category]): roles_list.append("{} {}".format(utils.resolve_emoji_from_alphabet(utils.ALPHABET[i]), r)) embed.add_field(name="Available Roles", value="\n".join(roles_list)) await self.bot.clear_reactions(reaction_message) await self.bot.edit_message(reaction_message, embed=embed) for i, r in enumerate(ROLE_CATEGORIES[category]): await self.bot.add_reaction(reaction_message, utils.resolve_emoji_from_alphabet(utils.ALPHABET[i])) # Allow only one role for Years / Course Levels if category is list(ROLE_CATEGORIES)[0]: def check(reaction, user): if user.id != ctx.message.author.id: return False letter = utils.resolve_letter_from_emoji(reaction.emoji) for i in range(0, len(ROLE_CATEGORIES[list(ROLE_CATEGORIES)[0]])): if letter == utils.ALPHABET[i]: return True return False res = await self.bot.wait_for_reaction(message=reaction_message, check=check, timeout=30) index = utils.ALPHABET.index(utils.resolve_letter_from_emoji(res.reaction.emoji)) role_to_add = ROLE_CATEGORIES[list(ROLE_CATEGORIES)[0]][index] role_to_add = next((r for r in ctx.message.server.roles if r.name == role_to_add), None) await self.handle_years(role_to_add, ctx.message.author) return [role_to_add] else: await self.bot.wait_until_ready() await asyncio.sleep(30) # Update the message that collects the reactions so we can access `.reactions` reaction_message = await self.bot.get_message(reaction_message.channel, reaction_message.id) selected_emojis = reaction_message.reactions.copy() reaction_users = [] for i, reaction in enumerate(reaction_message.reactions): reaction_users.append(await self.bot.get_reaction_users(reaction)) for i, reaction in enumerate(reaction_message.reactions): id_list = list(map(lambda x: x.id, reaction_users[i])) if ctx.message.author.id not in id_list: selected_emojis.remove(reaction) roles_selected = list(map(lambda r: utils.ALPHABET.index(utils.resolve_letter_from_emoji(r.emoji)), selected_emojis)) roles_to_add = list(map(lambda i: ROLE_CATEGORIES[category][i], roles_selected)) for i, role_string in enumerate(roles_to_add): roles_to_add[i] = next((r for r in ctx.message.server.roles if r.name == role_string), None) # Remove all potential `None` from the list roles_to_add = [x for x in roles_to_add if x is not None] return roles_to_add
async def add_reactions(self): """Adds the respective reactions for each option for users to react to""" for i in range(len(self.options)): await BOT.add_reaction( self.question_message, utils.resolve_emoji_from_alphabet(utils.ALPHABET[i].lower()))