how to make a discord meme bot using 15 lines of python code.
- discord.py
Required Modules
- discord
- requests
- json
👉 Step 1, Import the required modules and create a basic discord bot using python.
import discord
from discord.ext import commands
from requests import get
import jsonclient = commands.Bot(command_prefix="!")@client.command()
async def hi(ctx):
await ctx.reply("hi")client.run("Token")
👉 Step 2, use this website meme-api.herokuapp.com/gimme, Which provides json data of reddit posts.
Scrap the json data from the site using requests module.
get(“https://meme-api.herokuapp.com/gimme").text
and use json module to load the json data
data = json.loads(content,)
👉 Step 3, Make a discord embed message using discord.Embed
- use the keyword ‘title’ to get title value from the site & keyword ‘url’ to get meme image URL from the site.
meme = discord.Embed(title=f”{data[‘title’]}”, Color = discord.Color.random()).set_image(url=f”{data[‘url’]}”)
👉 Final code should be like this.
import discord
from discord.ext import commands
from requests import get
import jsonclient = commands.Bot(command_prefix="!")@client.command()
async def hi(ctx):
await ctx.reply("hi")@client.command()
async def meme(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)client.run("Token")