0
votes

I'm trying make the bot send an embeded link from imgur, specifically https://imgur.com/t/dank_memes

I've got as far as sending random.choice links, but that'd run out quickly and be too tedious

I got as far as this:

 @commands.command()
 async def meme(self, ctx):
  images=["links go here"]

  embed=discord.Embed(colour=discord.Colour.orange())

  embed.set_image(url=random.choice(images))

  await ctx.send(embed=embed)

I'm not sure how I'll do this since I found nothing else, also if I have to use an API you might as well add me on discord and explain how: Dr Jakaboii#2019

3

3 Answers

1
votes

If you're looking to extract random images from imgur, you could use the url https://imgur.com/random to generate a random image that you can then retrieve.

If you're looking to retrieve random images from the dank_memes url specifically, then you can webscrape using BeautifulSoup and requests and specifically retrieve elements from the pages HTML that have the <img> tag with a source url that begins with https://i.imgur.com and then randomize them.

A basic guide for webscraping imgur images using BS4

Click here for BS4 documentation

Happy coding!

1
votes

You can make this a random.randint and for each possible number, run this code with a different link. Long yet easy.

0
votes

Here is my command I used for my own bot (inside a cog) by the way but you can change @commands.command to @client.command (if you use the the normal file). You will to get api key from here and you can find more information about the api here so you can learn more.

Hope this helps you out!

    @commands.command(name='imgur', pass_context=True)
    async def imgur(self, ctx, *text: str):
        """Allows the user to search for an image from imgur"""
        rand = r.randint(0, 29)
        if text == ():
            await ctx.send('**Please enter a search term**')
        elif text[0] != ():
            items = imgur.gallery_search(" ".join(text[0:len(text)]), advanced=None, sort='viral', window='all',page=0)
            await ctx.send(items[rand].link)

Remember to put on top:

import discord
from discord.ext import commands
import random as r
from imgurpython import ImgurClient

imgur = ImgurClient("Client ID","Client Secret")