Discord Bot Example

Feb 21, 2020 07:56 · 698 words · 4 minute read ease (" ready ") function

However, you will need your own IDE, and Node.js already installed. Start off with creating a folder , and copy its file path. Then, open up PowerShell, and type “cd (folder path)”. Type in “npm init -y”. That will start a new Node.js project. Then type in, “npm i discord.js”. That will install the Discord.js API. Then, type in, “npm i puppeteer”. That will install a headless version of chromium.

02:18 - Do the same as the last two times, but now, type in, “npm i request”. Also, type in , “npm i nodemon”. I forgot to here. Now, one final time, type in, “npm i mathjs”. Now create two files, through typing “echo > Main.js”, and “echo > Authentication.json” Also, in “package.json”, add a script called, “startBot”:“nodemon Main.js”. In Authentication.json, add in the token you will get in the next part. Create an application in the discord developer portal. (Link in the description.) Then, make a bot in the bots tab, and you can get your API authentication key at the top. Here, I am showing how to get the invite link to the bot. Make sure it has Administrator as one of the permissions, otherwise you couldn’t add some necessary commands. Just select what server you want to invite it too, Complete a captcha, And you’re done.

06:20 - Now, let’s show off what commands this bot can use. The help command displays a screen of all commands. The repeat command repeats what you say. The ping command gives you the bot’s ping. The search command searches up a phrase, and returns the first search result. It takes a fair while. The Lorum Ipsum command returns some filler text, with a specified amount. The random command gives a random number within a range. The add command adds two numbers. The subtract command finds the difference between two numbers. The multiply command multiples two numbers. The divide command divides two numbers. The simplify command simplifies an equation. The evaluate command solves any other “normal function”, like sin().

08:11 - The differentiate command finds the derivative of an equation with respect to a variable you provide. Now, let’s look at the code. At the top here is just some information containing my contact info. Up here where you see constants being defined, are the libraries you set up in the last video are imported. The guildHeader variable is just for ease of use. This helpEmbed is the list of commands the help command shows. This async search function has a browser, searches up a phrase, then returns the first result through XPath. Look through the //comments for more info in the description. You can see it throw an error if there are no results, which is why I have a try/catch around the call. The getRandomInt function just generates a random number from a range. The Client.on(“ready”) function is what is first ran when the bot starts.

09:20 - You can see it also set its “playing” message, and it’s status. The Client.on(“message} function runs whenever a person types in a message on a server with the bot. The help command just replies with the embed. The repeat command parses the text to remove the command , and then replies with the parsed phrase. The ping command is simple. It just finds your computer’s ping, then replies it.

09:53 - The search command parses the phrase, and then passes it to the function You can see the XPath right here, as well as the try/catch. The Lorum Ipsum command goes to lorumipsum.net/api/1, and just grabs the HTML file, stripping it down. It also takes in a variable to decide how many paragraphs to grab. The paragraph tags are being stripped here. The rest of these commands are just basic arithmetic , other than a couple toward the end. Evaluate just passes the equation to the “evaluate” function of the Math.js Library. It’s the same with the “simplify” and “differentiate commands. This uses the random number generator and just gives a number between bounds. Here’s another showing of the code. Now, in the end, you log in with the token you stored in that file. That’s it! Kiddo. .