Skip to content
← Back to Community
How To make a Bot in Discord.py That can do google search!!
Profile icon
ROCKINGRAYQUAZA

A Bot in Discord.py That can do google search!!

Knowledge multiplies when shared!

So without any further ado let's get started!

Installing Packages:

First part in this tutorial is to install the necessary packages. In this case we will use Google API :

  • beautifulsoup4
  • google
    image

image

Here google is the main module and beautifulsoup4 is the dependency so both are necessary!!

Importing the Module:

So now that we have installed everything required, then let's import them! So go to your discord.py bot repl and write:

try: from googlesearch import search except ImportError: print("No module found!")

Here we have given a try/except statement in-case if the module was not successfully installed or was not found!

Making the function:

Now we have everything on the go! So let make the function!

@bot.command() async def find(ctx,*, query):

So here I have made the command using bot.command()
Then started the function with async def find(ctx,*,query):
Here I have given the function name Find but you can give it anything!

Now we can take the writer's name to style out our function, so that can be done with:

author = ctx.author.mention

Then we will send a message to the user that his/her information is being searched! So to that we will do:

await ctx.channel.send(f"Here are the links related to your question {author} !")

And after that in-case the result takes a lot of time to be retrieved then we can make a code that will make a typing effect, like when a human user types there! So:

async with ctx.typing():

This will prevent the user from thinking that the bot is not working!

So now the main part comes! How will we search the net?
As you know that we have import search from googlesearch module so to make a real google search we have to put so parameters this can be opinion based situation!
So the code is:

for j in search(query, tld="co.in", num=10, stop=10, pause=2):

yeah you got that right it is made in for loop so now let me explain you each parameters importance!

query : query string that we want to search for.
tld : tld stands for top level domain which means we want to search our result on google.com or google.in or some other domain.
lang : lang stands for language.
num : Number of results we want.
start : First result to retrieve.
stop : Last result to retrieve. Use None to keep searching forever.
pause : Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP. Keeping significant lapse will make your program slow but its safe and better option.
Return : Generator (iterator) that yields found URLs. If the stop parameter is None the iterator will loop forever.

So above I have provided you the code if you want multiple result like 5-10 per query but if you want one result then you can simply just change the num, stop values to your desired numbers!
Now to print the result we will await the result and send it to the channel (Note: put this inside for loop!), like:

await ctx.send(f"\n:point_right: {j}")

Thus the program is done!

This was my first tutorial!
Hope you like this tutorial
Leave a comment in case of any doubt, every comment is accepted!
If you think that this post helped you, It will be my pleasure!

Voters
Profile icon
Sagar10-9402
Profile icon
Luxing12
Profile icon
zte2976exe
Profile icon
DOOMSLAYER78
Profile icon
IshanGayal
Profile icon
SheepyKSP
Profile icon
justnoe
Profile icon
DoodleAnna
Profile icon
gtat
Profile icon
maxwon
Comments
hotnewtop
Profile icon
minecraft201109

pls show the end result cause idk what lines to put in which area like where to put the:

for j in search(query, tld="co.in", num=10, stop=10, pause=2):
Profile icon
ROCKINGRAYQUAZA

Uh I recently deleted my bot from discord, I have the code tho

@bot.command() async def find(ctx,*, query): author = ctx.author.mention await ctx.channel.send(f"Here are the links related to your question {author} !") async with ctx.typing(): for j in search(query, tld="co.in", num=1, stop=1, pause=2): await ctx.send(f"\n:point_right: {j}") await ctx.send("Have any more questions:question:\nFeel free to ask again :smiley: !")

This is most suitable.. @minecraft201109

Profile icon
ROCKINGRAYQUAZA
Profile icon
KrustGaming

Hello please help me, i am getting this error : Type error : search() got an unexpected keyword argument 'tld'. Please help me please

Profile icon
ROCKINGRAYQUAZA

Can you share a screenshot if possible..
Else.
here is a template to help :)

@bot.command() async def find(ctx,*, query): author = ctx.author.mention await ctx.channel.send(f"Here are the links related to your question {author} !") async with ctx.typing(): for j in search(query, tld="co.in", num=1, stop=1, pause=2): await ctx.send(f"\n:point_right: {j}") await ctx.send("Have any more questions:question:\nFeel free to ask again :smiley: !")

@KrustGaming

Profile icon
ElmoCultist

I don't quite understand how you're supposed to use the search command.

Profile icon
ROCKINGRAYQUAZA

Hi! @ElmoCultist

@bot.command() async def find(ctx,*, query): author = ctx.author.mention await ctx.channel.send(f"Here are the links related to your question {author} !") async with ctx.typing(): for j in search(query, tld="co.in", num=1, stop=1, pause=2): await ctx.send(f"\n:point_right: {j}") await ctx.send("Have any more questions:question:\nFeel free to ask again :smiley: !")

By using this code above you have to do:
find
with this you will get 1 result for your query if you want more just increase the stop value to whatever you want

Profile icon
ElmoCultist

@ROCKINGRAYQUAZA
Thank you so much for the help.

Profile icon
TECHboi99

for me its showing that search is not defines , what should i set search as? @ROCKINGRAYQUAZA

Profile icon
ROCKINGRAYQUAZA

Hi!! @TECHboi99
have you imported search correctly?

from googlesearch import search

Also goto "Packages" on your repl..("cubical icon")
And install beautifulsoup4 if it is not installed

image
Hope this helps!

Profile icon
TECHboi99

Thanks a lot for your quick response, I did not install beutiful soap, ill do that, however even without, it shows that googlesearch could not be found, is it because of this? Edit : even with beautifulsoup4 its showing package not found for googlesearch.
@ROCKINGRAYQUAZA

Profile icon
TECHboi99

hey even with beautiful soap 4 installed, its showing me a package not found for googlesearch. raceback (most recent call last):
File "main.py", line 1, in
from googlesearch import search
ModuleNotFoundError: No module named 'googlesearch'[email protected]

Profile icon
ROCKINGRAYQUAZA
Profile icon
jeroenvanhermon

Thank you so much!!! I spend the entire day figuring out how to make google search for discord!

Profile icon
ROCKINGRAYQUAZA

I don't do much coding anymore but thank you @jeroenvanhermon

Profile icon
maxwon

@bot.command(): name 'bot' is not defined???

Profile icon
ROCKINGRAYQUAZA

firstly have u given the colon after @bot.command() in ur code also or u just wrote it here?
Otherwise, check here they have explained here nicely: https://stackoverflow.com/questions/63493179/nameerror-name-bot-is-not-defined
@maxwon

Profile icon
EpicGamer007

Wait, can u send pypi link to googlesearch?

Profile icon
ROCKINGRAYQUAZA

You should probably try that out =) @EpicGamer007

Profile icon
CodeLongAndPros

@EpicGamer007 go to duckduckgo.com and search !pypi googlesearch

Profile icon
EpicGamer007

@CodeLongAndPros , duckduckgo can do that? cool. i personally use google or bing but my dad uses duckduckgo.

Profile icon
CodeLongAndPros

@EpicGamer007 DuckDuckGo has something called Bangs, where doing !bang will search. So !python will search python docs, !w search's Wikipedia, !so searches StackOverflow, and !pypi searchs PyPi. More info is at https://duckduckgo.com/bang

They actually have about 15 bangs just for Java

Profile icon
EpicGamer007

@CodeLongAndPros , oh nicee