diff options
| author | 2021-02-10 09:26:53 +0530 | |
|---|---|---|
| committer | 2021-02-10 09:26:53 +0530 | |
| commit | b4ee10ad69581fef0cc01c0539c644bb0f885c61 (patch) | |
| tree | c372c89b8a9db48dac5d6b003960234ed51b7f20 | |
| parent | Revert "Fixes Issue Matching Regex" (diff) | |
Add stackoverflow cog with answers
NOTE: I am not linking to the question page itself anywhere
| -rw-r--r-- | bot/exts/evergreen/stackoverflow.py | 44 | 
1 files changed, 44 insertions, 0 deletions
| diff --git a/bot/exts/evergreen/stackoverflow.py b/bot/exts/evergreen/stackoverflow.py new file mode 100644 index 00000000..ab98c757 --- /dev/null +++ b/bot/exts/evergreen/stackoverflow.py @@ -0,0 +1,44 @@ +from html import unescape +from urllib.parse import quote_plus + +from discord import Embed +from discord.ext.commands import Bot, Cog, Context, command, cooldown + +BASE_URL = "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&site=stackoverflow&q={query}" +SEARCH_URL = "https://stackoverflow.com/search?q={query}" +SO_COLOR = 0xF98036 + + +class Stackoverflow(Cog): +    """A cog which returns the top 5 results of a query from stackoverflow.""" + +    def __init__(self, bot: Bot): +        self.bot = bot + +    @command(name="stackoverflow", aliases=["so"]) +    @cooldown(1, 15) +    async def stackoverflow(self, ctx: Context, *, search_query: str) -> None: +        """Sends the top 5 results from stackoverflow based on a search query.""" +        async with self.bot.http_session.get(BASE_URL.format(query=quote_plus(search_query))) as response: +            data = await response.json() + +        top5 = data["items"][:5] +        embed = Embed(title=f"Search results for {search_query!r} - Stackoverflow", +                      url=SEARCH_URL.format(query=quote_plus(search_query)), +                      description=f"Here are the top {len(top5)} results:", +                      color=SO_COLOR) +        for item in top5: +            embed.add_field( +                name=f"{unescape(item['title'])}", +                value=(f"{item['score']} upvotes ┃ " +                       f"{item['view_count']} views ┃ " +                       f"{item['answer_count']} answers " +                       ), +                inline=False) +        embed.set_footer() +        await ctx.send(embed=embed) + + +def setup(bot: Bot) -> None: +    """Adds the cog to the bot.""" +    bot.add_cog(Stackoverflow(bot)) | 
