DEV Community

Nicat
Nicat

Posted on • Updated on

How to create meme bot/command using discord.py

How it works

Meme bot gets memes from 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:

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))
Enter fullscreen mode Exit fullscreen mode

If you aren't using cogs:

@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)
Enter fullscreen mode Exit fullscreen mode

It will look like this

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)
Enter fullscreen mode Exit fullscreen mode

Note:

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

With cogs:

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))
Enter fullscreen mode Exit fullscreen mode

Without cogs:

    @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)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)