How To make a Bot in Discord.py That can do google search!!
A Bot in Discord.py That can do google search!!
Knowledge multiplies when shared!
So without any further ado let's get started!
First part in this tutorial is to install the necessary packages. In this case we will use
Google API :
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:
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!
So here I have made the command using
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:
Then we will send a message to the user that his/her information is being searched! So to that we will do:
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:
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
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:
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:
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!