How to create meme bot/command using discord.py
Published: 2022-08-18 07:32:00

## How it works
Meme bot gets memes from [r/dankmemes](https://www.reddit.com/r/dankmemes) subreddit. Everytime you run the command, it will find one of the hot posts on this subreddit. If you want to add this feature to your discord bot, then follow these insturactions:

## Installing packages
`pip install discord.py`
`pip install requests`
`pip install aiohttp`

### If you are using cogs:
```py
import aiohttp
import requests
import discord
from discord.ext import commands

class Meme(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def meme_command(self, ctx):
        aembed = discord.Embed(title="", description="")

    async with aiohttp.ClientSession() as cs:
        async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
            res = await r.json()
            embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
            await ctx.send(embed=embed)`

def setup(client):
    client.add_cog(Meme(client))
```

### If you aren't using cogs:
```py
@client.command(pass_context=True)
async def meme(ctx):
    embed = discord.Embed(title="", description="")

    async with aiohttp.ClientSession() as cs:
        async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
            res = await r.json()
            embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
            await ctx.send(embed=embed)
```

It will look like this
```py
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True

TOKEN = "your token"
PREFIX = "your prefix" #example: ".", "!", "?"

client = commands.Bot(command_prefix=PREFIX, intents=intents,help_command=None)

@client.command(pass_context=True)
async def meme(ctx):
    embed = discord.Embed(title="", description="")

    async with aiohttp.ClientSession() as cs:
        async with cs.get('https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
            res = await r.json()
            embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
            await ctx.send(embed=embed)

client.run(TOKEN)
```

## Note:
**If you are using Heroku to host your bot. I recommend using this lines instead:**

### With cogs:
```py
from requests import get
import json
import discord
from discord.ext import commands

class Meme(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def meme_command(self, ctx):
        content = get("https://meme-api.herokuapp.com/gimme").text
        data = json.loads(content,)
        meme = discord.Embed(title=f"{data['title']}", color = discord.Color.random()).set_image(url=f"{data['url']}")
        await ctx.reply(embed=meme)


def setup(client):
    client.add_cog(Meme(client))
```

### Without cogs:
```py
    @client.command(pass_context=True)
    async def meme_command(self, ctx):
        content = get("https://meme-api.herokuapp.com/gimme").text
        data = json.loads(content,)
        meme = discord.Embed(title=f"{data['title']}", color = discord.Color.random()).set_image(url=f"{data['url']}")
        await ctx.reply(embed=meme)
```