diff options
author | 2020-09-20 11:49:45 +0530 | |
---|---|---|
committer | 2020-09-20 11:49:45 +0530 | |
commit | 5bb192ccf912e0190a40e92af6f4d30e2553023a (patch) | |
tree | 8cc8d29677977410ab7c78414b6aaf31f3a811a4 /bot/exts | |
parent | corrected typo (diff) |
added static method and added genrator expression and list comprehension
Diffstat (limited to 'bot/exts')
-rw-r--r-- | bot/exts/evergreen/wikipedia.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index e1fdf481..8cee46c4 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -21,6 +21,12 @@ class WikipediaCog(commands.Cog): self.bot = bot self.http_session = bot.http_session + @staticmethod + def formatted_wiki_urls(index: int, titles: str) -> List[str]: + """Making formatted wikipedia links list.""" + titles = f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' + return titles + async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: """Search wikipedia and return the first page found.""" async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: @@ -43,9 +49,6 @@ class WikipediaCog(commands.Cog): @commands.command(name="wikipedia", aliases=["wiki"]) async def wikipedia_search_command(self, ctx: commands.Context, *, search: str) -> None: """Return list of results containing your search query from wikipedia.""" - titles_no_underscore: List[str] = [] - s_desc = '' - titles = await self.search_wikipedia(search) def check(message: Message) -> bool: @@ -55,14 +58,11 @@ class WikipediaCog(commands.Cog): await ctx.send("Sorry, we could not find a wikipedia article using that search term") return - for title in titles: - title_for_creating_link = title.replace(" ", "_") # wikipedia uses "_" as spaces - titles_no_underscore.append(title_for_creating_link) - log.info("Finished appending titles to titles_no_underscore list") - async with ctx.typing(): - for index, title in enumerate(titles, start=1): - s_desc += f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})\n' + titles_no_underscore = [title.replace(" ", "_") for title in titles] # wikipedia uses "_" as spaces + log.info("Finished appending titles to titles_no_underscore list") + + s_desc = "\n".join(self.formatted_wiki_urls(index, title)for index, title in enumerate(titles, start=1)) embed = Embed(colour=Color.blue(), title=f"Wikipedia results for `{search}`", description=s_desc) embed.timestamp = datetime.datetime.utcnow() await ctx.send(embed=embed) @@ -79,7 +79,7 @@ class WikipediaCog(commands.Cog): else: error_msg = 'Please try again by using `.wiki` command' try: - message: Message = await ctx.bot.wait_for('message', timeout=60.0, check=check) + message = await ctx.bot.wait_for('message', timeout=60.0, check=check) response_from_user = await self.bot.get_context(message) if response_from_user.command: return |