Skip to content

Instantly share code, notes, and snippets.

@shahriyardx
Last active April 28, 2020 13:01
Show Gist options
  • Save shahriyardx/49b90d2474a5105809355de57b5aacb6 to your computer and use it in GitHub Desktop.
Save shahriyardx/49b90d2474a5105809355de57b5aacb6 to your computer and use it in GitHub Desktop.

Revisions

  1. shahriyardx revised this gist Apr 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wait_for.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ async def pixabay(self, ctx,cou:int,*,query):
    se = "Enabled"
    try:
    async with aiohttp.ClientSession() as s:
    async with s.get(f"https://pixabay.com/api/?key=16244254-43c141e7eae6cdcf242bb7d92&q={query}&image_type=photo&pretty=true&per_page={cou}{ss}") as r:
    async with s.get(f"https://pixabay.com/api/?key=16244254-43c141e7eae6cdcf242bb7&q={query}&image_type=photo&pretty=true&per_page={cou}{ss}") as r:
    data = await r.json()
    except:
    await ctx.send("Something went wrong!")
  2. shahriyardx created this gist Apr 28, 2020.
    110 changes: 110 additions & 0 deletions wait_for.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    @commands.command()
    @commands.cooldown(1,30, commands.BucketType.user)
    async def pixabay(self, ctx,cou:int,*,query):
    sg = self.client.get_guild(700374484955299900)

    nextbtn = discord.utils.get(sg.emojis, name="ignext")
    prevbtn = discord.utils.get(sg.emojis, name="igprev")
    skipf = discord.utils.get(sg.emojis, name="igskipf")
    skipb = discord.utils.get(sg.emojis, name="igskipb")
    beg = discord.utils.get(sg.emojis, name="igbeg")
    end = discord.utils.get(sg.emojis, name="igend")

    next = True
    query = query.replace(" ", "+")
    if cou < 3:
    cou = 3
    if cou > 200:
    cou = 200
    if ctx.channel.nsfw:
    ss = "&safesearch=false"
    se = "Disabled"
    else:
    ss = "&safesearch=true"
    se = "Enabled"
    try:
    async with aiohttp.ClientSession() as s:
    async with s.get(f"https://pixabay.com/api/?key=16244254-43c141e7eae6cdcf242bb7d92&q={query}&image_type=photo&pretty=true&per_page={cou}{ss}") as r:
    data = await r.json()
    except:
    await ctx.send("Something went wrong!")
    next = False

    count = 0
    fnd = 0
    while next:
    if len(data["hits"]) >0:
    try:
    r = data["hits"][count]
    fnd = len(data["hits"])
    #await ctx.send(r["largeImageURL"])
    embed = discord.Embed(color=ctx.author.color, timestamp=ctx.message.created_at)
    embed.set_image(url=r["largeImageURL"])
    embed.add_field(name="Tags", value=r["tags"])
    embed.set_footer(text=f"Page {count+1}/{fnd} - Safesearch : {se}")
    except:
    await ctx.send("No more images found!")
    break
    else:
    await ctx.send("Nothing match your query!")
    break
    if count <=0:
    try:
    await msg.edit(embed=embed)
    except:
    msg = await ctx.send(embed=embed)
    else:
    await msg.edit(embed=embed)

    if count >0:
    await msg.add_reaction(beg)
    await asyncio.sleep(0.1)
    if count >4:
    await msg.add_reaction(skipb)
    await asyncio.sleep(0.1)
    await msg.add_reaction(prevbtn)
    await asyncio.sleep(0.1)
    if count < fnd -1:
    await msg.add_reaction(nextbtn)
    await asyncio.sleep(0.1)

    if fnd > 5 and count < fnd -1:
    await msg.add_reaction(skipf)
    await asyncio.sleep(0.1)
    if count < fnd -1:
    await msg.add_reaction(end)
    await asyncio.sleep(0.1)

    def check(r,u):
    return (u.id == ctx.author.id) and (r.message.id == msg.id)
    try:
    r, u = await self.client.wait_for("reaction_add", timeout=60, check=check)

    if r.emoji == nextbtn:
    count += 1
    elif r.emoji == prevbtn:
    count -= 1
    elif r.emoji == end:
    count = fnd-1
    elif r.emoji == beg:
    count = 0
    elif r.emoji == skipf:
    if count +5 > fnd-1:
    count = fnd -1
    else:
    count += 5
    elif r.emoji == skipb:
    if count -5 <0:
    count = 0
    else:
    count -= 5
    try:
    await msg.clear_reactions()
    except:
    pass
    except:
    try:
    await msg.clear_reactions()
    except:
    pass
    break