46
loading...
This website collects cookies to deliver better user experience
.gif happy
then the bot will send a random happy gif.(the blue button on top-right corner)
.Add Bot
on the right hand side, and after this step you will have a screen like this 👇🏻.bot
👇🏻.src
in which we have a main file called bot.js
in which we are going to code our bot.package-lock.json
, package.json
and node_modules
respectively, they are basically of node packages and their information..env
file but we will discuss about it later in this blog.npm i discord.js
)npm i dotenv
)npm i node-fetch
)import discord from 'discord.js;'
.env
files stores all your secrets, like your discord bot's token or your API Key, these things will not be uploaded on GitHub using the git ignore file.addEventListner
which helps us to react to certain events, like if a user clicks on something or double-tap on something a particular function should run.addEventListner
is more or less replaced by .on
function..on
functions are called in regards to the client
so we have to write client.on('event', callBackFunction)
.<Name of the Bot> is up and ready!
and name of the bot is fetched by this inbuilt property known as .user.tag
, which is to be called in regards to the client
..login
.client.login(process.env.TOKEN)
process.env.TOKEN
, this is the way we call variables from our .env
file..env
file.tenor api key
inside your bot.js
file, you just have to write process.env.TENOR
.try-catch
block around the client.login()
function, so if any error occurs, we can catch it and show it on the console. msg
will contain the message which user has sent. .gif
.
try-catch
block.msg.content
helps us to fetch the content inside the msg. In leman's term, it is like .innerText
in JavaScript. .gif
the code inside the if
statement will be executed..gif batman
then this will be considered as a string and a problem arises here, which is how do we separate the bot command and the user's query..split()
, which will help us to separate the whole string into two different values stored in an array, for example: if I write .gif batman
then .split()
will make an array : ['.gif', 'batman']
.first index of query
which will be .gif
to the string .gif.https://g.tenor.com/v1/search?q=USERQUERY&key=API-KEY
https://g.tenor.com/v1/search?q=${query[1]}&key=${process.env.TENOR}
async
in front of the callback function as you can see in the above image on line number 10.async
will make your function, asynchronous and then we will use await
to wait for the response from the API.src
folder, then write node bot.js
.