From 82a7c5dcb836f767c958389cdbfb3c0ec9f0f718 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Fri, 4 Sep 2020 14:28:03 +0530 Subject: added period in line 43 to solve D415 --- bot/exts/evergreen/wikipedia.py | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 bot/exts/evergreen/wikipedia.py diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py new file mode 100644 index 00000000..36600c56 --- /dev/null +++ b/bot/exts/evergreen/wikipedia.py @@ -0,0 +1,81 @@ +import asyncio +import datetime +import logging +import discord +from typing import Optional + +from discord.ext import commands + +log = logging.getLogger(__name__) + +SEARCH_API = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={search_term}&format=json" +WIKIPEDIA_URL = "https://en.wikipedia.org/wiki/{title}" + + +class WikipediaCog(commands.Cog): + """Get info from wikipedia.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + self.http_session = bot.http_session + + async def search_wikipedia(self, search_term: str) -> Optional[str]: + """Search wikipedia and return the first page found.""" + async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: + data = await response.json() + page = [] + + search_result = data["query"]["search"] + if len(search_result) == 0: + return None + + # we dont like "may refere to" pages. + for a in search_result: + if "may refer to" in a["snippet"]: + pass + else: + page.append(a["title"]) + log.info("hit a 'may refer to' page, taking result 2 (index 1)") + return page + + @commands.command(name="wikipedia", aliases=["wiki"]) + async def w_pedia(self, ctx: commands.Context, *, search: str) -> None: + """Gives list of item.""" + final = [] + s = '' + + title = await self.search_wikipedia(search) + + def check(user) -> bool: + return user.author.id == ctx.author.id + + if title is None: + await ctx.send("Sorry, we could not find a wikipedia article using that search term") + return + + for i in title: + t = i.replace(" ", "_") # wikipedia uses "_" as spaces + final.append(t) + + async with ctx.typing(): + for index, (a, j) in enumerate(zip(title, final), start=1): + s = s + f'`{index}` [{a}]({WIKIPEDIA_URL.format(title=j)})\n' + embed = discord.Embed(colour=discord.Color.blue(), title=f"Wikipedia results for `{search}`", description=s) + embed.timestamp = datetime.datetime.utcnow() + await ctx.send(embed=embed) + + embed = discord.Embed(colour=discord.Color.green(), description="Enter number to choose") + msg = await ctx.send(embed=embed) + + try: + user = await ctx.bot.wait_for('message', timeout=60.0, check=check) + await ctx.send(WIKIPEDIA_URL.format(title=final[int(user.content) - 1])) + + except asyncio.TimeoutError: + embed = discord.Embed(colour=discord.Color.red(), description=f"Time's up {ctx.author.mention}") + await msg.edit(embed=embed) + + +def setup(bot: commands.Bot) -> None: + """Uptime Cog load.""" + bot.add_cog(WikipediaCog(bot)) -- cgit v1.2.3 From 7c60922b689f07c4a07872e074f590f23ed77776 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Fri, 4 Sep 2020 14:55:11 +0530 Subject: added value error exception --- bot/exts/evergreen/wikipedia.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 36600c56..24360d28 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -1,9 +1,9 @@ import asyncio import datetime import logging -import discord -from typing import Optional +from typing import Optional +from discord import Color, Embed from discord.ext import commands log = logging.getLogger(__name__) @@ -35,7 +35,7 @@ class WikipediaCog(commands.Cog): pass else: page.append(a["title"]) - log.info("hit a 'may refer to' page, taking result 2 (index 1)") + log.info("appening titles") return page @commands.command(name="wikipedia", aliases=["wiki"]) @@ -60,11 +60,11 @@ class WikipediaCog(commands.Cog): async with ctx.typing(): for index, (a, j) in enumerate(zip(title, final), start=1): s = s + f'`{index}` [{a}]({WIKIPEDIA_URL.format(title=j)})\n' - embed = discord.Embed(colour=discord.Color.blue(), title=f"Wikipedia results for `{search}`", description=s) + embed = Embed(colour=Color.blue(), title=f"Wikipedia results for `{search}`", description=s) embed.timestamp = datetime.datetime.utcnow() await ctx.send(embed=embed) - embed = discord.Embed(colour=discord.Color.green(), description="Enter number to choose") + embed = Embed(colour=Color.green(), description="Enter number to choose") msg = await ctx.send(embed=embed) try: @@ -72,9 +72,12 @@ class WikipediaCog(commands.Cog): await ctx.send(WIKIPEDIA_URL.format(title=final[int(user.content) - 1])) except asyncio.TimeoutError: - embed = discord.Embed(colour=discord.Color.red(), description=f"Time's up {ctx.author.mention}") + embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") await msg.edit(embed=embed) + except ValueError: + await ctx.send("I am sorry but that isn't int value") + def setup(bot: commands.Bot) -> None: """Uptime Cog load.""" -- cgit v1.2.3 From d6b33d69f4c0bdd1f65910d5013dc4bb7a8e748d Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Fri, 4 Sep 2020 15:10:58 +0530 Subject: corrected import order and added annotation for user --- bot/exts/evergreen/wikipedia.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 24360d28..08ce35c2 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -1,9 +1,9 @@ import asyncio import datetime import logging - from typing import Optional -from discord import Color, Embed + +from discord import Color, Embed, Member from discord.ext import commands log = logging.getLogger(__name__) @@ -46,7 +46,7 @@ class WikipediaCog(commands.Cog): title = await self.search_wikipedia(search) - def check(user) -> bool: + def check(user: Member) -> bool: return user.author.id == ctx.author.id if title is None: -- cgit v1.2.3 From 5a6ed991e4290dfb5e95d071deb23d8775963a2a Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Fri, 4 Sep 2020 20:00:38 +0530 Subject: changed search_description to s_desc cuz its failing E501 --- bot/exts/evergreen/wikipedia.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 08ce35c2..4dec8bdf 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -25,16 +25,16 @@ class WikipediaCog(commands.Cog): data = await response.json() page = [] - search_result = data["query"]["search"] - if len(search_result) == 0: + search_results = data["query"]["search"] + if len(search_results) == 0: return None # we dont like "may refere to" pages. - for a in search_result: - if "may refer to" in a["snippet"]: + for search_result in search_results: + if "may refer to" in search_result["snippet"]: pass else: - page.append(a["title"]) + page.append(search_result["title"]) log.info("appening titles") return page @@ -42,25 +42,25 @@ class WikipediaCog(commands.Cog): async def w_pedia(self, ctx: commands.Context, *, search: str) -> None: """Gives list of item.""" final = [] - s = '' + s_desc = '' - title = await self.search_wikipedia(search) + titles = await self.search_wikipedia(search) def check(user: Member) -> bool: return user.author.id == ctx.author.id - if title is None: + if titles is None: await ctx.send("Sorry, we could not find a wikipedia article using that search term") return - for i in title: + for i in titles: t = i.replace(" ", "_") # wikipedia uses "_" as spaces final.append(t) async with ctx.typing(): - for index, (a, j) in enumerate(zip(title, final), start=1): - s = s + f'`{index}` [{a}]({WIKIPEDIA_URL.format(title=j)})\n' - embed = Embed(colour=Color.blue(), title=f"Wikipedia results for `{search}`", description=s) + for index, title in enumerate(titles, start=1): + s_desc += f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})\n' + embed = Embed(colour=Color.blue(), title=f"Wikipedia results for `{search}`", description=s_desc) embed.timestamp = datetime.datetime.utcnow() await ctx.send(embed=embed) -- cgit v1.2.3 From c908cea40177f15648825525bdbe9af98daff06d Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Sat, 5 Sep 2020 21:28:53 +0530 Subject: changed final to titles_no_underscore Co-authored-by: Shirayuki Nekomata --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 4dec8bdf..0463e7a7 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -41,7 +41,7 @@ class WikipediaCog(commands.Cog): @commands.command(name="wikipedia", aliases=["wiki"]) async def w_pedia(self, ctx: commands.Context, *, search: str) -> None: """Gives list of item.""" - final = [] + titles_no_underscore: List[str] = [] s_desc = '' titles = await self.search_wikipedia(search) -- cgit v1.2.3 From adc00a9e6e0394f349ee7f68fc3529c6e477677c Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 6 Sep 2020 13:03:32 +0530 Subject: corrected indexing problem --- bot/exts/evergreen/wikipedia.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 4dec8bdf..05bfbe66 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -69,7 +69,11 @@ class WikipediaCog(commands.Cog): try: user = await ctx.bot.wait_for('message', timeout=60.0, check=check) - await ctx.send(WIKIPEDIA_URL.format(title=final[int(user.content) - 1])) + response = int(user.content) + if response <= 0: + await ctx.send("Please enter a valid response") + else: + await ctx.send(WIKIPEDIA_URL.format(title=final[response - 1])) except asyncio.TimeoutError: embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") -- cgit v1.2.3 From a06d6127f62cd1dd48086c9cd117f049aa3c77f2 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 6 Sep 2020 18:12:26 +0530 Subject: corrected index error and solve issue spam issue of wiki command --- bot/__init__.py | 2 +- bot/bot.py | 1 + bot/exts/evergreen/wikipedia.py | 28 +++++++++++++++++++--------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index 6976e089..a9a0865e 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -37,7 +37,7 @@ os.makedirs(log_dir, exist_ok=True) # File handler rotates logs every 5 MB file_handler = logging.handlers.RotatingFileHandler( - log_file, maxBytes=5*(2**20), backupCount=10) + log_file, maxBytes=5 * (2**20), backupCount=10) file_handler.setLevel(logging.TRACE if Client.debug else logging.DEBUG) # Console handler prints to terminal diff --git a/bot/bot.py b/bot/bot.py index ffaf4284..b94013bc 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -69,6 +69,7 @@ class SeasonalBot(commands.Bot): """Check command errors for UserInputError and reset the cooldown if thrown.""" if isinstance(exception, commands.UserInputError): context.command.reset_cooldown(context) + else: await super().on_command_error(context, exception) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 05bfbe66..b6714747 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -1,7 +1,7 @@ import asyncio import datetime import logging -from typing import Optional +from typing import Optional, List from discord import Color, Embed, Member from discord.ext import commands @@ -38,10 +38,11 @@ class WikipediaCog(commands.Cog): log.info("appening titles") return page + @commands.cooldown(1, 10, commands.BucketType.user) @commands.command(name="wikipedia", aliases=["wiki"]) async def w_pedia(self, ctx: commands.Context, *, search: str) -> None: - """Gives list of item.""" - final = [] + """Returns list of your search query from wikipedia.""" + titles_no_underscore: List[str] = [] s_desc = '' titles = await self.search_wikipedia(search) @@ -53,9 +54,9 @@ class WikipediaCog(commands.Cog): await ctx.send("Sorry, we could not find a wikipedia article using that search term") return - for i in titles: - t = i.replace(" ", "_") # wikipedia uses "_" as spaces - final.append(t) + for title in titles: + title_for_creating_link = title.replace(" ", "_") # wikipedia uses "_" as spaces + titles_no_underscore.append(title_for_creating_link) async with ctx.typing(): for index, title in enumerate(titles, start=1): @@ -69,18 +70,27 @@ class WikipediaCog(commands.Cog): try: user = await ctx.bot.wait_for('message', timeout=60.0, check=check) + response = await self.bot.get_context(user) + + if response.command: + return + response = int(user.content) + if response <= 0: - await ctx.send("Please enter a valid response") + await ctx.send("You cant send negative Index buddy") else: - await ctx.send(WIKIPEDIA_URL.format(title=final[response - 1])) + await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) except asyncio.TimeoutError: embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") await msg.edit(embed=embed) except ValueError: - await ctx.send("I am sorry but that isn't int value") + await ctx.send("sorry u cant do that I will only accept int") + + except IndexError: + await ctx.send("sorry but you are exceeding the limit please select from above list") def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 8ea3217e30d52e5ebadc093a55d9de851dd46906 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 6 Sep 2020 18:23:11 +0530 Subject: corrected error msg of index error --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index c697ea85..7b348bc4 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -90,7 +90,7 @@ class WikipediaCog(commands.Cog): await ctx.send("sorry u cant do that I will only accept int") except IndexError: - await ctx.send("sorry but you are exceeding the limit please select from above list") + await ctx.send("sorry but you are exceeding the limit please select the range from above given list") def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 67fded1acf7c5bcb410ccfd26899036f49d97c06 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Sun, 6 Sep 2020 21:25:10 +0530 Subject: Corrected grammer Co-authored-by: Shirayuki Nekomata --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 7b348bc4..fd91ce14 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -87,7 +87,7 @@ class WikipediaCog(commands.Cog): await msg.edit(embed=embed) except ValueError: - await ctx.send("sorry u cant do that I will only accept int") + await ctx.send("Sorry, but you cannot do that, I will only accept an integer.") except IndexError: await ctx.send("sorry but you are exceeding the limit please select the range from above given list") -- cgit v1.2.3 From a71109dec599205284753a5c3ba65f9d7b583cbe Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 7 Sep 2020 15:28:26 +0530 Subject: removed extra space --- bot/bot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/bot.py b/bot/bot.py index b94013bc..ffaf4284 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -69,7 +69,6 @@ class SeasonalBot(commands.Bot): """Check command errors for UserInputError and reset the cooldown if thrown.""" if isinstance(exception, commands.UserInputError): context.command.reset_cooldown(context) - else: await super().on_command_error(context, exception) -- cgit v1.2.3 From 548f173929872c1a6dffa0f0db48297bf4265a6e Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 7 Sep 2020 15:52:17 +0530 Subject: changes chance var to chances --- bot/exts/evergreen/wikipedia.py | 60 ++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index fd91ce14..1d7feb8b 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -19,7 +19,7 @@ class WikipediaCog(commands.Cog): self.bot = bot self.http_session = bot.http_session - async def search_wikipedia(self, search_term: str) -> Optional[list]: + 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: data = await response.json() @@ -31,11 +31,12 @@ class WikipediaCog(commands.Cog): # we dont like "may refere to" pages. for search_result in search_results: + log.info("trying to appening titles") if "may refer to" in search_result["snippet"]: pass else: page.append(search_result["title"]) - log.info("appening titles") + log.info("Finished appening titles") return page @commands.cooldown(1, 10, commands.BucketType.user) @@ -57,6 +58,7 @@ class WikipediaCog(commands.Cog): 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 appening titles with no underscore") async with ctx.typing(): for index, title in enumerate(titles, start=1): @@ -64,33 +66,41 @@ class WikipediaCog(commands.Cog): embed = Embed(colour=Color.blue(), title=f"Wikipedia results for `{search}`", description=s_desc) embed.timestamp = datetime.datetime.utcnow() await ctx.send(embed=embed) - embed = Embed(colour=Color.green(), description="Enter number to choose") msg = await ctx.send(embed=embed) + chances = 0 + l_of_list = len(titles_no_underscore) # getting length of list - try: - user = await ctx.bot.wait_for('message', timeout=60.0, check=check) - response = await self.bot.get_context(user) - - if response.command: - return - - response = int(user.content) - - if response <= 0: - await ctx.send("You cant send negative Index buddy") + while chances <= 3: + chances += 1 + if chances < 3: + error_msg = f'You have `{3 - chances}/3` chances left' else: - await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) - - except asyncio.TimeoutError: - embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") - await msg.edit(embed=embed) - - except ValueError: - await ctx.send("Sorry, but you cannot do that, I will only accept an integer.") - - except IndexError: - await ctx.send("sorry but you are exceeding the limit please select the range from above given list") + error_msg = 'Please try again by using `.wiki` command' + try: + user = await ctx.bot.wait_for('message', timeout=60.0, check=check) + response = await self.bot.get_context(user) + if response.command: + return + response = int(user.content) + if response < 0: + await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") + elif response == 0: + await ctx.send(f"Sorry, please give the range between `1` to `{len(l_of_list)}`, {error_msg}") + else: + await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + break + + except asyncio.TimeoutError: + embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") + await msg.edit(embed=embed) + break + + except ValueError: + await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") + + except IndexError: + await ctx.send(f"Sorry, please give the range between `1` to {l_of_list}, {error_msg}") def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 891d77ffd5ef50f7b81739788aff47eb65016481 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 7 Sep 2020 23:02:17 +0530 Subject: Corrected typo Co-authored-by: Shirayuki Nekomata --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 1d7feb8b..bd38ffc9 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -31,7 +31,7 @@ class WikipediaCog(commands.Cog): # we dont like "may refere to" pages. for search_result in search_results: - log.info("trying to appening titles") + log.info("trying to append titles") if "may refer to" in search_result["snippet"]: pass else: -- cgit v1.2.3 From 7779a19fb3e75448c26daa448062619a2d17b6b2 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 17:26:18 +0530 Subject: made two variable of response --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 1d7feb8b..5380d6e9 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -79,8 +79,8 @@ class WikipediaCog(commands.Cog): error_msg = 'Please try again by using `.wiki` command' try: user = await ctx.bot.wait_for('message', timeout=60.0, check=check) - response = await self.bot.get_context(user) - if response.command: + response_from_user = await self.bot.get_context(user) + if response_from_user.command: return response = int(user.content) if response < 0: -- cgit v1.2.3 From b3a43afc807cc69cc0afc598483947649d4952fa Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 18:08:59 +0530 Subject: added wikipedia class which has total_chance --- bot/constants.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bot/constants.py b/bot/constants.py index bf6c5a40..133db56c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -253,3 +253,6 @@ POSITIVE_REPLIES = [ "Aye aye, cap'n!", "I'll allow it.", ] + +class Wikipedia: + total_chance = 3 -- cgit v1.2.3 From 60e675373941b742a1e3670a1a22f085a2711388 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 18:11:20 +0530 Subject: corrected 0 option error,now wikipedia chance is not hardcoded --- bot/exts/evergreen/wikipedia.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 91350300..ba39fc94 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -3,9 +3,11 @@ import datetime import logging from typing import List, Optional -from discord import Color, Embed, Member +from discord import Color, Embed, Message from discord.ext import commands +from bot.constants import Wikipedia + log = logging.getLogger(__name__) SEARCH_API = "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={search_term}&format=json" @@ -36,20 +38,20 @@ class WikipediaCog(commands.Cog): pass else: page.append(search_result["title"]) - log.info("Finished appening titles") + log.info("Finished appending titles") return page @commands.cooldown(1, 10, commands.BucketType.user) @commands.command(name="wikipedia", aliases=["wiki"]) - async def w_pedia(self, ctx: commands.Context, *, search: str) -> None: + async def wikipedia_search_command(self, ctx: commands.Context, *, search: str) -> None: """Returns list of your search query from wikipedia.""" titles_no_underscore: List[str] = [] s_desc = '' titles = await self.search_wikipedia(search) - def check(user: Member) -> bool: - return user.author.id == ctx.author.id + def check(message: Message) -> bool: + return message.author.id == ctx.author.id if titles is None: await ctx.send("Sorry, we could not find a wikipedia article using that search term") @@ -69,24 +71,25 @@ class WikipediaCog(commands.Cog): embed = Embed(colour=Color.green(), description="Enter number to choose") msg = await ctx.send(embed=embed) chances = 0 + total_chances = Wikipedia.total_chance l_of_list = len(titles_no_underscore) # getting length of list - while chances <= 3: + while chances <= total_chances: chances += 1 - if chances < 3: - error_msg = f'You have `{3 - chances}/3` chances left' + if chances < total_chances: + error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' else: error_msg = 'Please try again by using `.wiki` command' try: - user = await ctx.bot.wait_for('message', timeout=60.0, check=check) - response_from_user = await self.bot.get_context(user) + message: 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 - response = int(user.content) + response = int(message.content) if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") elif response == 0: - await ctx.send(f"Sorry, please give the range between `1` to `{len(l_of_list)}`, {error_msg}") + await ctx.send(f"Sorry, please give the range between `1` to `{l_of_list}`, {error_msg}") else: await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) break -- cgit v1.2.3 From 26105f38e1a3c13cc19ece9e5fe45eb3cafc3f88 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Tue, 8 Sep 2020 19:05:05 +0530 Subject: Update bot/exts/evergreen/wikipedia.py Co-authored-by: Shirayuki Nekomata --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ba39fc94..ae3b24d5 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -89,7 +89,7 @@ class WikipediaCog(commands.Cog): if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") elif response == 0: - await ctx.send(f"Sorry, please give the range between `1` to `{l_of_list}`, {error_msg}") + await ctx.send(f"Sorry, please give an integer between `1` and `{l_of_list}`, {error_msg}") else: await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) break -- cgit v1.2.3 From e2e58c1d7893e0d9b36c43590815930f4b12f309 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 21:03:30 +0530 Subject: corrected typo --- bot/exts/evergreen/wikipedia.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ae3b24d5..dd06204d 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): 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 appening titles with no underscore") + log.info("Finished appening titles_no_underscore") async with ctx.typing(): for index, title in enumerate(titles, start=1): @@ -89,7 +89,7 @@ class WikipediaCog(commands.Cog): if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") elif response == 0: - await ctx.send(f"Sorry, please give an integer between `1` and `{l_of_list}`, {error_msg}") + await ctx.send(f"Sorry, please give an integer between `1` to `{l_of_list}`, {error_msg}") else: await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) break @@ -103,7 +103,7 @@ class WikipediaCog(commands.Cog): await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") except IndexError: - await ctx.send(f"Sorry, please give the range between `1` to {l_of_list}, {error_msg}") + await ctx.send(f"Sorry, please give an integer between `1` to `{l_of_list}`, {error_msg}") def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 0f2570e2f4826416a65ff3293f27587a840ed009 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 21:05:32 +0530 Subject: corrected typo --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index dd06204d..e6c1bf5b 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): 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 appening titles_no_underscore") + log.info("Finished appending titles_no_underscore") async with ctx.typing(): for index, title in enumerate(titles, start=1): -- cgit v1.2.3 From 48a19c1ba6318a8e7aac97d721cedc6392e9a9c6 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Tue, 8 Sep 2020 21:07:20 +0530 Subject: corrected typo --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index e6c1bf5b..6ddcdbe9 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): 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_no_underscore") + log.info("Finished appending titles to titles_no_underscore list") async with ctx.typing(): for index, title in enumerate(titles, start=1): -- cgit v1.2.3 From 48a4d42c974f39381ef8940b79c906d9646168a9 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:22:16 +0530 Subject: Changed len(searched_result)-> searched result Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 6ddcdbe9..83feee88 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -28,7 +28,7 @@ class WikipediaCog(commands.Cog): page = [] search_results = data["query"]["search"] - if len(search_results) == 0: + if not search_results: return None # we dont like "may refere to" pages. -- cgit v1.2.3 From 1284d3e6c7d113d353565a70f53439b732eaf6eb Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:23:06 +0530 Subject: Corrected grammer Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 83feee88..85121907 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -31,7 +31,7 @@ class WikipediaCog(commands.Cog): if not search_results: return None - # we dont like "may refere to" pages. + # Ignore pages with "may refer to" for search_result in search_results: log.info("trying to append titles") if "may refer to" in search_result["snippet"]: -- cgit v1.2.3 From fcdc0cbbc7b9632afd1e65f3ec12c91ca8340aee Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:26:07 +0530 Subject: changed title is None to not titles Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 85121907..127fab47 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -53,7 +53,7 @@ class WikipediaCog(commands.Cog): def check(message: Message) -> bool: return message.author.id == ctx.author.id - if titles is None: + if not titles: await ctx.send("Sorry, we could not find a wikipedia article using that search term") return -- cgit v1.2.3 From 2c532e0f5eeb988a09622eb77b80dea9a6076ca0 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:33:45 +0530 Subject: Removed unnecessary pass Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 127fab47..eb74aee9 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -34,9 +34,7 @@ class WikipediaCog(commands.Cog): # Ignore pages with "may refer to" for search_result in search_results: log.info("trying to append titles") - if "may refer to" in search_result["snippet"]: - pass - else: + if "may refer to" not in search_result["snippet"]: page.append(search_result["title"]) log.info("Finished appending titles") return page -- cgit v1.2.3 From 734558f9634e8f11495993fcf3dabf7118845653 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Thu, 17 Sep 2020 19:55:03 +0530 Subject: corrected doc string ,changed l_of_list to titles_len and add author channel check --- bot/exts/evergreen/wikipedia.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 6ddcdbe9..9c4e900e 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -44,14 +44,14 @@ class WikipediaCog(commands.Cog): @commands.cooldown(1, 10, commands.BucketType.user) @commands.command(name="wikipedia", aliases=["wiki"]) async def wikipedia_search_command(self, ctx: commands.Context, *, search: str) -> None: - """Returns list of your search query from wikipedia.""" + """Returns 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: - return message.author.id == ctx.author.id + return message.author.id == ctx.author.id and message.channel == ctx.channel if titles is None: await ctx.send("Sorry, we could not find a wikipedia article using that search term") @@ -72,7 +72,7 @@ class WikipediaCog(commands.Cog): msg = await ctx.send(embed=embed) chances = 0 total_chances = Wikipedia.total_chance - l_of_list = len(titles_no_underscore) # getting length of list + titles_len = len(titles_no_underscore) # getting length of list while chances <= total_chances: chances += 1 @@ -89,7 +89,7 @@ class WikipediaCog(commands.Cog): if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") elif response == 0: - await ctx.send(f"Sorry, please give an integer between `1` to `{l_of_list}`, {error_msg}") + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") else: await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) break @@ -103,9 +103,9 @@ class WikipediaCog(commands.Cog): await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") except IndexError: - await ctx.send(f"Sorry, please give an integer between `1` to `{l_of_list}`, {error_msg}") + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") def setup(bot: commands.Bot) -> None: - """Uptime Cog load.""" + """Wikipedia Cog load.""" bot.add_cog(WikipediaCog(bot)) -- cgit v1.2.3 From 6ab4e2a59076152553f2f97734e579fce3df8e26 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Thu, 17 Sep 2020 22:13:46 +0530 Subject: corrected typo --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 9f92c620..e1fdf481 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -42,7 +42,7 @@ class WikipediaCog(commands.Cog): @commands.cooldown(1, 10, commands.BucketType.user) @commands.command(name="wikipedia", aliases=["wiki"]) async def wikipedia_search_command(self, ctx: commands.Context, *, search: str) -> None: - """Returns list of results containing your search query from wikipedia.""" + """Return list of results containing your search query from wikipedia.""" titles_no_underscore: List[str] = [] s_desc = '' -- cgit v1.2.3 From 5bb192ccf912e0190a40e92af6f4d30e2553023a Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 20 Sep 2020 11:49:45 +0530 Subject: added static method and added genrator expression and list comprehension --- bot/exts/evergreen/wikipedia.py | 22 +++++++++++----------- 1 file 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 -- cgit v1.2.3 From 06e6c0d10bd3ff8ac610e291b8a9416b4ddfedcd Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 20 Sep 2020 12:13:30 +0530 Subject: removed while loop and added for loop --- bot/exts/evergreen/wikipedia.py | 55 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 8cee46c4..a4284aaa 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -72,36 +72,41 @@ class WikipediaCog(commands.Cog): total_chances = Wikipedia.total_chance titles_len = len(titles_no_underscore) # getting length of list - while chances <= total_chances: - chances += 1 - if chances < total_chances: - error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' - else: - error_msg = 'Please try again by using `.wiki` command' - try: - 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 - response = int(message.content) - if response < 0: - await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") - elif response == 0: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + for _ in range(Wikipedia.total_chance): + if chances <= total_chances: + chances += 1 + if chances < total_chances: + error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' else: - await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + error_msg = 'Please try again by using `.wiki` command' + try: + 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 + response = int(message.content) + if response < 0: + await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") + elif response == 0: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + else: + await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + break + + except asyncio.TimeoutError: + embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") + await msg.edit(embed=embed) break - except asyncio.TimeoutError: - embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") - await msg.edit(embed=embed) - break + except ValueError: + await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") - except ValueError: - await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") + except IndexError: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") - except IndexError: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + except Exception as e: + log.info(e) + break def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 53eb775d1d8c633c0a223755cec34da5c74a1110 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Sun, 20 Sep 2020 12:35:49 +0530 Subject: changed chance_left -> retry_count, and tweak the code according to pure --- bot/exts/evergreen/wikipedia.py | 61 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index a4284aaa..205f5894 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -68,45 +68,42 @@ class WikipediaCog(commands.Cog): await ctx.send(embed=embed) embed = Embed(colour=Color.green(), description="Enter number to choose") msg = await ctx.send(embed=embed) - chances = 0 - total_chances = Wikipedia.total_chance titles_len = len(titles_no_underscore) # getting length of list - for _ in range(Wikipedia.total_chance): - if chances <= total_chances: - chances += 1 - if chances < total_chances: - error_msg = f'You have `{total_chances - chances}/{total_chances}` chances left' + for retry_count in range(1, Wikipedia.total_chance + 1): + retries_left = Wikipedia.total_chance - retry_count + if retry_count < Wikipedia.total_chance: + error_msg = f"You have `{retries_left}/{Wikipedia.total_chance}` chances left" + else: + error_msg = 'Please try again by using `.wiki` command' + try: + 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 + response = int(message.content) + if response < 0: + await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") + elif response == 0: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") else: - error_msg = 'Please try again by using `.wiki` command' - try: - 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 - response = int(message.content) - if response < 0: - await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") - elif response == 0: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") - else: - await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) - break - - except asyncio.TimeoutError: - embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") - await msg.edit(embed=embed) + await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) break - except ValueError: - await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") + except asyncio.TimeoutError: + embed = Embed(colour=Color.red(), description=f"Time's up {ctx.author.mention}") + await msg.edit(embed=embed) + break - except IndexError: - await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + except ValueError: + await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") - except Exception as e: - log.info(e) - break + except IndexError: + await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") + + except Exception as e: + log.info(e) + break def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From e92deaab6c1d1a1e51526e97abe05ef325d10f22 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:08:27 +0530 Subject: Changed List[str]->Optional[List[str]] Co-authored-by: PureFunctor --- bot/exts/evergreen/wikipedia.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 205f5894..ab01e1ee 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -27,15 +27,13 @@ class WikipediaCog(commands.Cog): titles = f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' return titles - async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: + async def search_wikipedia(self, search_term: str) -> 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: data = await response.json() page = [] search_results = data["query"]["search"] - if not search_results: - return None # Ignore pages with "may refer to" for search_result in search_results: -- cgit v1.2.3 From b23a9ce3ee4d2adc4c01f64b719526f023fe52f6 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 11:43:35 +0530 Subject: changed List[str]->Optional[List[str]] --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ab01e1ee..babf6620 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -22,7 +22,7 @@ class WikipediaCog(commands.Cog): self.http_session = bot.http_session @staticmethod - def formatted_wiki_urls(index: int, titles: str) -> List[str]: + def formatted_wiki_urls(index: int, titles: str) -> Optional[List[str]]: """Making formatted wikipedia links list.""" titles = f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' return titles -- cgit v1.2.3 From 72ee81302f12536f3952635ef03d90c0315fc20e Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 11:45:28 +0530 Subject: changed log.info(e)-> log.info(f'Caught exception {e}, breaking out of retry loop') --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index babf6620..ecd797bb 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -100,7 +100,7 @@ class WikipediaCog(commands.Cog): await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") except Exception as e: - log.info(e) + log.info(f"Caught exception {e}, breaking out of retry loop") break -- cgit v1.2.3 From 9081d890767007af42d232e1b5db4a7996f091d6 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 11:49:59 +0530 Subject: applied optional to correct function --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ecd797bb..ee632b67 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -22,12 +22,12 @@ class WikipediaCog(commands.Cog): self.http_session = bot.http_session @staticmethod - def formatted_wiki_urls(index: int, titles: str) -> Optional[List[str]]: + 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) -> List[str]: + 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: data = await response.json() -- cgit v1.2.3 From 8e6b3470381947b64c7c0551ccafae92a7f2ef14 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:55:36 +0530 Subject: changed titles_no_underscore -> titles Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ab01e1ee..6087a014 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -85,7 +85,7 @@ class WikipediaCog(commands.Cog): elif response == 0: await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") else: - await ctx.send(WIKIPEDIA_URL.format(title=titles_no_underscore[response - 1])) + await ctx.send(WIKIPEDIA_URL.format(title=titles[response - 1].replace(" ", "_"))) break except asyncio.TimeoutError: -- cgit v1.2.3 From 5528dddaf9da08b70c5ccba9c7477190427970c1 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:56:33 +0530 Subject: Removed the title_no_underscore list with titles Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 6087a014..7e568392 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -66,7 +66,7 @@ class WikipediaCog(commands.Cog): await ctx.send(embed=embed) embed = Embed(colour=Color.green(), description="Enter number to choose") msg = await ctx.send(embed=embed) - titles_len = len(titles_no_underscore) # getting length of list + titles_len = len(titles) # getting length of list for retry_count in range(1, Wikipedia.total_chance + 1): retries_left = Wikipedia.total_chance - retry_count -- cgit v1.2.3 From 095468c6ee72098ca6a79b6eaee1d03fbb60487c Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:57:04 +0530 Subject: Update bot/exts/evergreen/wikipedia.py Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 7e568392..7fac3575 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): 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)) + s_desc = "\n".join(self.formatted_wiki_url(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) -- cgit v1.2.3 From 398425dbd881e7a3b01c3a821819873852567578 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:58:49 +0530 Subject: made code more readable Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 7fac3575..e18c0b79 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -77,8 +77,10 @@ class WikipediaCog(commands.Cog): try: 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 + response = int(message.content) if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") -- cgit v1.2.3 From a4e949123816150f86c8dbf8a36a4a67741a4f0c Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:59:55 +0530 Subject: changed f_wiki_urls -> f_wiki_url Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index e18c0b79..c565a404 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -22,10 +22,9 @@ class WikipediaCog(commands.Cog): 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 + def formatted_wiki_url(index: int, titles: str) -> str: + """Making formatted wikipedia link..""" + return f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})' async def search_wikipedia(self, search_term: str) -> List[str]: """Search wikipedia and return the first page found.""" -- cgit v1.2.3 From 4900150b8edee0b0e6ce24a97999c227f868c329 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 12:31:48 +0530 Subject: changing and corrected linting --- bot/exts/evergreen/wikipedia.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index ebc8305f..95311784 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -24,7 +24,7 @@ class WikipediaCog(commands.Cog): @staticmethod def formatted_wiki_url(index: int, titles: str) -> str: """Making formatted wikipedia link..""" - return f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})' + return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: """Search wikipedia and return the first page found.""" @@ -56,7 +56,6 @@ class WikipediaCog(commands.Cog): return async with ctx.typing(): - 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_url(index, title) for index, title in enumerate(titles, start=1)) @@ -76,10 +75,10 @@ class WikipediaCog(commands.Cog): try: 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 - + response = int(message.content) if response < 0: await ctx.send(f"Sorry, but you can't give negative index, {error_msg}") -- cgit v1.2.3 From 74c737984b7a206941e87733f189f40e628b6eb0 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 13:21:44 +0530 Subject: changed page ->pages Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 95311784..2ca5ac52 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -26,11 +26,12 @@ class WikipediaCog(commands.Cog): """Making formatted wikipedia link..""" return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' - async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: - """Search wikipedia and return the first page found.""" + async def search_wikipedia(self, search_term: str) -> List[str]: + """Search wikipedia and return the first 10 pages found.""" async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: data = await response.json() - page = [] + + pages = [] search_results = data["query"]["search"] @@ -38,9 +39,10 @@ class WikipediaCog(commands.Cog): for search_result in search_results: log.info("trying to append titles") if "may refer to" not in search_result["snippet"]: - page.append(search_result["title"]) + pages.append(search_result["title"]) + log.info("Finished appending titles") - return page + return pages @commands.cooldown(1, 10, commands.BucketType.user) @commands.command(name="wikipedia", aliases=["wiki"]) -- cgit v1.2.3 From ef9211a068ba3978fa129f484d9794fe66b8b071 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 13:22:25 +0530 Subject: removed extra . Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 2ca5ac52..26ce8a03 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -23,7 +23,7 @@ class WikipediaCog(commands.Cog): @staticmethod def formatted_wiki_url(index: int, titles: str) -> str: - """Making formatted wikipedia link..""" + """Making formatted wikipedia link.""" return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' async def search_wikipedia(self, search_term: str) -> List[str]: -- cgit v1.2.3 From 14553174d8a572936222860549b309a799f5f9e4 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 13:41:26 +0530 Subject: changed List[str]->Optional[List[str]] --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 26ce8a03..588db9d6 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -26,7 +26,7 @@ class WikipediaCog(commands.Cog): """Making formatted wikipedia link.""" return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' - async def search_wikipedia(self, search_term: str) -> List[str]: + async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: """Search wikipedia and return the first 10 pages found.""" async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: data = await response.json() @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): async with ctx.typing(): log.info("Finished appending titles to titles_no_underscore list") - s_desc = "\n".join(self.formatted_wiki_url(index, title) for index, title in enumerate(titles, start=1)) + s_desc = "\n".join(self.formatted_wiki_url(index, titles) 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) -- cgit v1.2.3 From e5305fc75dc3a506370351e6c9dbac5e2aaa40c6 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 13:45:16 +0530 Subject: removed t.optional --- bot/exts/evergreen/wikipedia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 588db9d6..3a82db62 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -1,7 +1,7 @@ import asyncio import datetime import logging -from typing import List, Optional +from typing import List from discord import Color, Embed, Message from discord.ext import commands @@ -26,7 +26,7 @@ class WikipediaCog(commands.Cog): """Making formatted wikipedia link.""" return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' - async def search_wikipedia(self, search_term: str) -> Optional[List[str]]: + async def search_wikipedia(self, search_term: str) -> List[str]: """Search wikipedia and return the first 10 pages found.""" async with self.http_session.get(SEARCH_API.format(search_term=search_term)) as response: data = await response.json() -- cgit v1.2.3 From a7264895497c7b3e484ba2ce2131d6b092b5fdd3 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 13:48:30 +0530 Subject: removed s from title --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 3a82db62..b097e606 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -60,7 +60,7 @@ class WikipediaCog(commands.Cog): async with ctx.typing(): log.info("Finished appending titles to titles_no_underscore list") - s_desc = "\n".join(self.formatted_wiki_url(index, titles) for index, title in enumerate(titles, start=1)) + s_desc = "\n".join(self.formatted_wiki_url(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) -- cgit v1.2.3 From 2571f9e88f492ece4a09fc38913acaaffc4cc5e6 Mon Sep 17 00:00:00 2001 From: Anubhav <57266248+Anubhav1603@users.noreply.github.com> Date: Mon, 21 Sep 2020 15:54:21 +0530 Subject: Update bot/exts/evergreen/wikipedia.py Co-authored-by: Thomas Petersson <61778143+thomaspet@users.noreply.github.com> --- bot/exts/evergreen/wikipedia.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index b097e606..dc003aea 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -22,9 +22,9 @@ class WikipediaCog(commands.Cog): self.http_session = bot.http_session @staticmethod - def formatted_wiki_url(index: int, titles: str) -> str: - """Making formatted wikipedia link.""" - return f'`{index}` [{titles}]({WIKIPEDIA_URL.format(title=titles.replace(" ", "_"))})' + def formatted_wiki_url(index: int, title: str) -> str: + """Formating wikipedia link with index and title.""" + return f'`{index}` [{title}]({WIKIPEDIA_URL.format(title=title.replace(" ", "_"))})' async def search_wikipedia(self, search_term: str) -> List[str]: """Search wikipedia and return the first 10 pages found.""" -- cgit v1.2.3 From d356c6491fcfecbeebfabdf43a1af8ee3554c012 Mon Sep 17 00:00:00 2001 From: Anubhav1603 Date: Mon, 21 Sep 2020 19:29:04 +0530 Subject: corrected value error msg --- bot/exts/evergreen/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index dc003aea..c1fff873 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -96,7 +96,7 @@ class WikipediaCog(commands.Cog): break except ValueError: - await ctx.send(f"Sorry, but you cannot do that, I will only accept an integer, {error_msg}") + await ctx.send(f"Sorry, but you cannot do that, I will only accept an positive integer, {error_msg}") except IndexError: await ctx.send(f"Sorry, please give an integer between `1` to `{titles_len}`, {error_msg}") -- cgit v1.2.3