Repository: Cog-Creators/Red-DiscordBot

Issue:
imgur search Posts Three Images into Chat
When using the built in imgur search functions, it pulls three image search results from imgur/reddit and posts all three to Discord. When I looked at the code it looks like there's a slice that tells it to post all three. Is this intentional behavior? When the search succeeds on all three that's a huge wall of image posted to Discord chat as a single post. I can edit the slice to only use the first but I figured I'd ask here first in case this is a bug/error or if I'll need to edit this. Just seems like an insane default or I'm just doing something wrong on my end.

Slice in question (same code is used for each type of image search it looks like):

```
for r in results[:3]:
  msg += r["gifv"] if "gifv" in r else r["link"]
  msg += "\n"
```

Thanks!


Discussion:
I think we should allow the user to decide how many images they want to get, the more sane default would surely be one image but I think allowing up to max of 5 images to be returned will work great here.

So for example:
- `[p]imgur search dog`
  should return one image of a dog
- `[p]imgur search 1 dog`
  should return one image of a dog
- `[p]imgur search 3 dog`
  should return three images of a dog
- `[p]imgur search 5 dog`
  should return five images of a dog
- `[p]imgur search 0 dog`
  should fail
- `[p]imgur search 6 dog`
  should fail

This can be achieved using `typing.Optional`:
```py
async def imgur_search(self, ctx, amount: Optional[int] = 1, *, term: str):
```

Resolving patch:
diff --git a/redbot/cogs/image/image.py b/redbot/cogs/image/image.py
--- a/redbot/cogs/image/image.py
+++ b/redbot/cogs/image/image.py
@@ -1,4 +1,5 @@
 from random import shuffle
+from typing import Optional
 
 import aiohttp
 
@@ -42,11 +43,15 @@ async def _imgur(self, ctx):
         pass
 
     @_imgur.command(name="search")
-    async def imgur_search(self, ctx, *, term: str):
+    async def imgur_search(self, ctx, count: Optional[int] = 1, *, term: str):
         """Search Imgur for the specified term.
 
-        Returns up to 3 results.
+        Use `count` to choose how many images should be returned.
+        Command can return up to 5 images.
         """
+        if count < 1 or count > 5:
+            await ctx.send(_("Image count has to be between 1 and 5."))
+            return
         url = self.imgur_base_url + "gallery/search/time/all/0"
         params = {"q": term}
         imgur_client_id = (await ctx.bot.get_shared_api_tokens("imgur")).get("client_id")
@@ -68,7 +73,7 @@ async def imgur_search(self, ctx, *, term: str):
                 return
             shuffle(results)
             msg = _("Search results...\n")
-            for r in results[:3]:
+            for r in results[:count]:
                 msg += r["gifv"] if "gifv" in r else r["link"]
                 msg += "\n"
             await ctx.send(msg)
@@ -79,14 +84,23 @@ async def imgur_search(self, ctx, *, term: str):
 
     @_imgur.command(name="subreddit")
     async def imgur_subreddit(
-        self, ctx, subreddit: str, sort_type: str = "top", window: str = "day"
+        self,
+        ctx,
+        subreddit: str,
+        count: Optional[int] = 1,
+        sort_type: str = "top",
+        window: str = "day",
     ):
         """Get images from a subreddit.
 
         You can customize the search with the following options:
+        - `<count>`: number of images to return (up to 5)
         - `<sort_type>`: new, top
         - `<window>`: day, week, month, year, all
         """
+        if count < 1 or count > 5:
+            await ctx.send(_("Image count has to be between 1 and 5."))
+            return
         sort_type = sort_type.lower()
         window = window.lower()
 
@@ -121,7 +135,7 @@ async def imgur_subreddit(
         if data["success"]:
             items = data["data"]
             if items:
-                for item in items[:3]:
+                for item in items[:count]:
                     link = item["gifv"] if "gifv" in item else item["link"]
                     links.append("{}\n{}".format(item["title"], link))
 


Test patch: