Skip to content

Discord.py Basic Example

import discord
from discord.ext import commands
import os
import cse
from dotenv import load_dotenv # pip install python-dotenv

# your .env file
# CSE_API_KEY = your_api_key

load_dotenv()
api_key = os.getenv('CSE_API_KEY')

engine = cse.Engine(api_key)

bot = commands.Bot(command_prefix='!')
engine = cse.Engine(api_key)

@bot.command()
async def google(ctx,*,query):
    result = await engine.search(query, safesearch=True)
    em=discord.Embed(title=query, description=result[0].title, url=result[0].url, color=discord.Color.blue())
    em.add_field(name="Description:", value=result[0].description)
    await ctx.send(embed=em)

bot.run("you_super_secret_token")

Discord.py Cog Example

# cogs.google

import cse
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv # pip install python-dotenv

load_dotenv()
api_key = os.getenv('CSE_API_KEY')

engine = cse.Engine(api_key)

class GoogleCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def google(self, ctx,*,query):
        result = await engine.search(query, safesearch=True)
        em=discord.Embed(title=query, description=result[0].title, url=result[0].url, color=discord.Color.blue())
        em.add_field(name="Description:", value=result[0].description)
        await ctx.send(embed=em)

def setup(bot):
    bot.add_cog(GoogleCog(bot))
import discord
from discord.ext import commands

# your .env file
# CSE_API_KEY = your_api_key

bot.load_extension("cogs.google")
bot.run("you_super_secret_token")