Create a Discord Bot With Node.js

broken image

a brand new directory one can create. this with the dossier explorer if you don't. desire to use. the home windows terminal i'll sort. mikter and ii'm going to call this. discord.js. academic bots ok so that's discord.js. hyphen educational hyphen bot. and you can see that it turned into just created. successfully and ii'm going to go forward. and just cd. into discord.js educational bot so it's. going to in actual fact difference the listing. so we're presently inside. documents folder and we have simply created. a new folder known as discord.js. tutorialbot but i desire to in actual fact. change the present directory. to that new folder i just created so i'm. going to variety cd which stands for change. directory. so input you'll discover that now our. current directory we are inside. discord.js educational bot that's within. the files folder. now i will open up my text editor.

broken image

This is going to be the main entry point. to our. bots okay it's in which we're going to. hold all of our code and what i want to. do subsequent. is i need to set up a dot env file. so we will create a brand new file and. observe how i'm developing this file. external of the source folder. okay so dot env is outside of the source. folder. and ii'm going to create an environment. variable by means of simply typing the name of the. surroundings variable i desire to use so. i am just going to name this discord.js. bot token you can call it whatever you. desire yet you must truly give it a. call after which the equivalent sign and you. want to assign. a cost now to truly get our bot. token we have to do. a pair things we first have got to. correctly pass over to the developer portal. so you could go over to discord.com curb. developers. scale down purposes and you're going to.

Be brought to this dashboard over right here. and you're going to click new. software and i am just going to call. this. uh djs academic bot or. i'm correctly just call this discord.js. tutorialbot and i'll create it. and we will be dropped at the. display screen over right here so we have the customer. identity with the client secret we're not. going to worry about the client secret. at all we're going to click on. bots and ii'm going to click on upload bot. and. click on certain do it and there we cross we. have created a bot for. our program there are a number of matters. you are able to do along with your discord program. and one of the things that you'll be able to do is. create a bot with it so now that we have. created our bot we're going to pass ahead. and click on. reproduction right over right here in which it says token. click on to disclose token. we're going to click copy okay and.

i'll reproduction and paste that right. over right here. now it is okay that i show you guys my. token because that's only for. educational purposes it's going to be reset. after this video so don't worry but you. prefer to ensure you preserve your bot. token. saved personal you never want to percentage. your bot token with anybody because if. somebody has your bot token they are able to do a. lot of awful things. along with deleting every single channel on. your server looking on what. permissions has obviously you may kick. every single consumer from the server. be sure that you don't share  this token. with anybody and ensure. you're taking precaution whenever you are. securing your bot token. it's important retain exclusive now if. someone does compromise your bot. you can actually simply click. regenerate and this will replace the. token. it will in actual fact reset every little thing so.

The former token will not work. ok so if i click on token if i click on. regenerate you will discover that a new one. was generated i will simply click replica . and this one that'll be pasted won't. paintings so let's. near this out and let's cross ahead and. in fact import. dot n so we're gonna do require dot n. and i'm gonna call the config operate. so this is going to clearly load up. all of our environment variables that. are within our env dossier. now some thing that i'll point out is. that if you even have surroundings. variables. set on your operating system these. environment variables will not override. them. so for example if on my working manner i. have an environment variable called. discord js underscore bot underscore. token. and i've it set to some type of value. this bot token. or i am sorry this environment variable.

is absolutely not interpreted with this magnitude. it will interpret the value set on. the working manner so it's very. important to do not forget that due to the fact. routinely some  persons. do run into the issue quite often so. that is why i am naming it something that. i don't have already on my working. method ok so the next factor that i. desire to show you guys. is how thiis basically works so now i am. going to clearly. reference our surroundings variable permit. me console.log it. in an effort to reference our surroundings variable. we'll reference the worldwide. procedure object. and this item has a property called. dot conclusion or. conclusion env which is an item and we're. going to. reference our environment variable so. discord.js. bot token now i'm going to go forward and. run the bot now. so we will run the script so we are going. to variety node.

source bot.js oh whoops. what's going on right here node.sourcebot.js. there we pass and you can see that the. script labored efficiently and you can. see that our token. turned into logged to the console ok so that's. just aa pretty. easy example of ways this complete issue . works now the subsequent drawback is that we. in fact don't have our bot on our. server. ok so in the present day i have my experiment server. you can actually just create a server by means of. doing add server and click on create a. server and you just name it whatsoever you. prefer. it's very easy and when you've got your. server uh what we want to do. is we want to upload our bot to the server. so what we'll do is we are going. to head over. to oauth2 well first let's get the. purchaser identification so we're going to click. preferred information. and we're going to reproduction the client identity. and then what i will do is i am.

Going to visit this url. so i will depart the url either in the. video or they'll might be i do not know. if it will likely be in the outline. yet i'll put it in the video clearly. you must visit discord.com. api slash oauth2 authorize. and we're going to circulate in a question. string so question mark. client underscore id that's the query. parameter. and this is going to have the price of. our. bots purchaser id which is what we received from. right over right here. okay you go to popular information. client id and you replica  it. ok just paste it over there we additionally . need  to make certain we have a scope. because if i just enter. this at present it is going to take us to. right here however it says no scopes were offered. so we have to add an extra question. parameter with ampersand and then the. name of scope and then set thiis equal to. bot. so now due to the fact we are authorizing our.

application as a bot. now we'll cross ahead and select. the server that we want to add the bot. to. now you can only add bots to servers. wherein you've the manage server. permission. so that doesn't always suggest that. you should be the owner because there. are distinct permissions when it comes. to the discord server but if you're the. owner you then. obviously have the chief permission. so what i'll do is i'm just. going to choose the. server that i possess so arizona server i'm. simply going to click authorize. and we'll do that and that. ought to be it so say approved. ok let's close this window let's close. this in addition. now if i go over to discord you will see. that it says discord.js educational bot. uh has joined ok it says in these days at 6 27. am. and our bot is over right here now our buzz in.

The server but it is not. on line so let's in fact get it on-line. so to try this we need to really use. code so that's going to be the subsequent facet. of this academic so in fact logging the. bot in. so something that i am going to mention right. off the bat is if you men desire a. reference to the discord.js library. one could go to discord.js.org. and you can click documentation. they also do have a aid besides that. you can suffer if you favor. a text stylish aid or textual content based. tutorial . it's aactually very informative the devs. put numerous attempt into this. in addition to documenting their overall. library. so i'd particularly advise you go to this. web site those are additional assets. apart from this video that you'll be able to. reference. ok but we will move over. documentation and we're going to be. referencing the documentation plenty.

So what we'll do first is we are. going to. import a category and directly from. the discord.js library so i am utilizing the. require operate. and i am in actual fact importing discord.js. and in among these curly braces this. is truly called object destructuring. and it additionally  works with uploading uh. certain. exports from the module so the module. itself truly just an item that has. a gaggle of alternative. uh exports so these those exports you. can. instantly import them from it so if i. desire to import client. i can import client if i need import. message i'm able to do this as well. yet the only factor that we will. import is client ok the client itself. is a category that enables us to have interaction. with the discord api. and it actually makes use of we really want to. create an example of that classification. so we're going to do const and ii'm going.

To name this. purchaser with a lowercase c it would be. difficult but whether it is confusing you can. call it. bots i am simply going to call a shopper and. we will. use the new keyword to create a new. instance of the client class. so we have now this client object which. is an example of. the client classification ok and it truly is where. the item oriented. terminology is very important once you. have a category. you may create many alternative circumstances. of it but we only desire one. okay and now that we've created an. example of this class we will start. referencing all the properties as well. as the methods. so among the methods that we are going. to be using is. the login method this goes to take. in a parameter that is the bot token. that we need to move in. and this goes to essentially log or. bought in it is going to create the.

Connection to the discord. gateway the discrete api so to truly. log or purchased in we'll. move a token now for this reason why. i have our environment variable. is due to the fact i don't desire to just paste my. token in like this. let's reproduction that and let's paste it in. there we pass i don't want to only paste. it like that aand you by no means want to do. this with any type of credential due to the fact. credentials are very sensitive so you. want to make sure you retain it private. you need to retain it inside an. environment variable yet we will. reference our surroundings variable like. we did earlier. so discord js bots. token just like that okay. you want to make sure it suits and now. let's in fact run our bot so you can. see immediately it is currently offline. but i'll move forward and just variety. node dot source.

Bot.js and you will see that our botches went. online that's distinctly cool. okay now i'm actually sort of tired of. simply typing. node.source.js so what i'm in fact. going to do. is i'll set up  scripts i am. going to establish a start script. and this begin script is really going. to be important if we wish to. install our bot so for instance heroku for. instance. the start script is essentially simply going. to be an alias for. node.cut down sourcebot.js. now rather than typing that in all of the. time i will just variety npm. run begin and you'll discover it is just an. alias. for node dot minimize source bot.js. ok it's going to run the appliance. i'm additionally  going to set up a dev script. that is in my opinion what i love to do. and it clearly makes use of nodemon and what. this goes to do. is every single time i make a metamorphosis to. any of my dossier within the  listing it's.

Going to restart the process now you. need  to make sure you have. nodemon mounted on the way to use. this. so if you don't have nodemon established. you could forget about thiis part but i'd. highly suggest you install node. modding just sort npm. i hyphen g node mod. and that should set up it globally and. you could simply sort nodemon. after which resource.js. and you will find that every single time. if i keep it's going to say restarting. due to changes. okay it really works like a wonder. so this is going to truly prevent. you from wanting to restart your. application manually over and over again. rather it is going to do it automatically. everytime you  make a transformation on your. software. ok so for the rest of academic we are. simply going to be using nodemon. yet like i said if you don't have typical. in case you have concerns with nodemon ignore.

It don't be concerned approximately it simply. use the node command or the start script. that we created. okay so conclusion camera dev and there we cross. so one more thing that i want to mention. is that if you actually. have an wrong token it is going to in fact. throw this error it's going to say invalid. tokens provided so please make sure that. you have. your bot token uh hooked up actually. okay if that error happens it signifies that. you circulate in an wrong token. or the token uh you most likely reset the. token and it stated that. ok so always double check all right so. our bot is presently logged in yet what. if i personally desired to. log a message to the console each time. the bot first. logs in ok so what we're gonna do is. we're going to take heed to an event. ok routine are important to. realize so let me get allow me. provide you with guys a brief assessment of the way.

routine work so every thing. that occurs on the discord server or on. the discord purchaser itself may be proposal. of as an event. so for example to illustrate in javascript. in. the browser we've simple events such. as the clicking event so if i click. for example this website over right here. i'm able to sign up a click on event on this. sidebar and if there's a sign up. click event then we are able to execute some . kind of operate. correct we can pop pop up an alert for. example well as an instance to illustrate. if i type hey and if i variety input and. if i ship that. message okay that message is going to be. despatched to the channel. ok the discord api goes to emit an. occasion. again to the customer and the client which. goes to be. our library over here it will. manage that event for us okay and it's. going to in actual fact emit what's called a. message occasion.

While the bot first logs in it is going to. emit a ready occasion. if as an instance as an example if i react to. this message over right here with this emoji. that is going to set off a message. reaction advert occasion ok so i am hoping you. guys get the assumption of those hobbies. due to the fact basically anything that we do. if i delete the message as well that's. going to set off an event. and there are so many distinctive routine. that we will use. with the discord api that is exceptionally. cool. if you truly want to get an entire list. of routine one can pass over to. the discord.js documentation. and iif you click on purchaser and iif you. scroll down over right here on the facet. you will find those are all of the routine. ok the one that we will be. utilizing now could be the prepared occasion. that's brought on each time the client. will become all set so each time the bot has.

efficiently logged in. so to truly sign in an event we are. going to must. call the on technique. instantly on the customer item ok so. one thing that i will also point out is. the client. classification itself extends event emitter it's. a subclass of occasion emitter. and what i imply subclass is that. essentially. it inherits all of the techniques and. properties of. the splendid category the guardian classification so if. you actually go over to documentation. and iif you pass over to client it says. extends base purchaser. and base client extends occasion emitter so. in case you click on that it'll take you to. the node.js documentation. and iif you're accustomed to occasion. emitters you'll know the way they work. anytime some thing occurs we will. manually emit an event. after which any uh after which any file that. uh. listens to those movements will be able to. accept those events.

okay so it's aa good way to uh do things. asynchronously besides. ok so what we'll do is we're. going to reference customers. ok remember it's a subclass of event. emitter in an effort to reference the on. technique. and we will flow and ready that is. the call of the event. so it is usually going to be purchaser.on the. call of. the development so all set and then it's going. to be a callback function as a second. parameter. the callback function is how we handle. the events. ok and for the ready activities this. callback operate takes 0 parameters. so all we're going to do is i'm simply. going to say console.log. the bot has logged in now if i keep and. if i'm going over to my terminal it is going. to claim. the bot is logged in now if i want a. more descriptive. log i may also for example if i wanted to. log. the bot's username and the bots.

Discriminator. so i can actually just use template. string so that's. backtick so as opposed to utilizing costs i. can use backticks. and we'll use dollar sign curly. braces so this is termed string. interpolation and ii'm going to reference. client. and the client item itself has a. estate referred to as person. ok and the user object itself is a. purchaser user variety and the client user. sort has a group of different residences. that we will reference so you can find. over here. that we will reference as an instance for. instance if i want to get the username. i'm able to get that aas well so. purchaser.user.username. and if i keep that it may say it will say. in the consolediscord.js tutorialbot. if for instance if i wanted both the. username and the discriminator. i can do purchaser the user.tag and it will. log that to the console supply it a moment. or two or 3 let's examine.

There we pass okay so i am just gonna say. uh. has a log in and iif you pass over to the. documentation i'll maintain. referencing this because it's very. important. okay it's very well documented so if we. click on purchaser and if i click. properties you'll find. that the customer itself has a gaggle of. homes and one among the properties is. consumer. and the kind is a shopper user if i click. on that. it's going to inform me all the properties of. purchaser consumer so i know that client.person. thiis user item is a client person sort. and the client consumer kind has some of these. properties such as tested username. tag uh method. presence all some of these homes. and so they have methods too so i. might tremendously suggest you men to play. around with them and see what you are able to do. with it there is a lot of alternative. data that you'll be able to get from.

each object so i would totally. inspire you guys to mess around with. it. okay yet we will go away it like this for. now so you will see that that if i restart the. bot. every single time it logs in. efficaciously it will set off this. all set event. yet we'll pass on and next. thing that we want to. do is we want to truly established a few. undemanding. responses from the bot so to try this we. have to. hear to another occasion known as the. message event remember like i mentioned. every little thing that happens. inside our discord purchaser so if i delete. a message that's going to trigger. a message delete occasion if i send a. message it will set off a message. event. and to expose you men that we're going to. reference the client item once more. and we'll call the unusual method. and this time. and iif you have this intellisense setup.

it is going to show you all of the various uh. activities so we're going to use the message. occasion. okay now this time the message event. takes in. a parameter for the callback operate. okay and that parameter is a message. object it's a message variety and ii'm going. to visit the documentation once more. because i desire to encourage you men to. use documentation. if i go over to events so remember. that is lessons click on client. click on movements or scroll over down over. here. and iif you go over right here and iif you click. on message that is the call of the event. and it says admitted whenever a message. is created which sincerely capacity. every time a message is distributed to a channel. and it says over here parameter type. description so the parameter over here. these are essentially the parameters for. the callback operate. ok so if you see uh let's say for.

Instance for the message reaction add. if you notice more than one parameter that. means that you would need  parameters. for the callback function. okay yet let's focus on message for now. so you will discover that. it is a message parameter the sort is. a message so i click message you can. see. that i have a bunch of different. homes and feature a gaggle of different. methods. okay and it's the fantastic thing about item. orientated programming due to the fact we are. truly encapsulating a bunch of. distinct methods and houses. and we're clearly putting them all. together because they all relate. in this kind of way and with these kind of. homes we can reference the endeavor. we can. reference the application attachments so. for example as an example if the message. has any documents that were despatched with it we. can get those files by using referencing.

Attachments. if i want to get the author that despatched. the message i will reference. message.writer. ok if i desire to get the channel that. the message was sent in i will do. message.channel and and so on so let's just do. a easy factor. so as an instance if i want to get the particular. message that the user sent we will. in fact reference message.content material okay. so if i am going over right here. it says the content of the message so. we're just going to go forward and do. console.log. message.content i simply desire to show you. guys how thiis works. so our bot is logged in and let's cross. over right here now let's send a message let's. say good day. and notice what it says hiya so we. clearly despatched a message okay. this event turned into emitted and whilst this. occasion turned into emitted if we want to manage. it we. want the callback operate the callback. function takes one parameter that is a.

Message. kind and the message object itself has a. bunch of homes and we are. referencing content. ok now as an instance if i want to get the. consumer that sent the message. so let's log something more descriptive. so let's. do something like the user or let's do. uh. let's try this message so lower back i'm utilizing. a template strings and string. interpolation so that's  back ticks. and then we're using. the dollar signal and then a pair of curly. braces. and iinside the curly brace you must. flow within the  variable magnitude. that you want to interpolate or examine. rather. so message dots and i desire to get the. writer. that despatched the message so like i pronounced we. can reference the valuables author. on the message item and iif you seem. over here style of the author property is. a person kind so if i open that up. i can actually see all the properties of.

The user variety. in addition to methods so let's just do this. message.writer. and let's simply get the tag so that's the. username the pound symbol as well as  the. discriminator and i'll simply do that. that user despatched. well let's just try this let's create a. extra advanced this sort of. like our possess customized logger after which. we are going to simply log the message. so measure the content material just like that so. let's do. hiya and you'll discover oh wait our bot is. now not uh. logged in but just supply it a couple of. seconds. okay so say hello very well so you'll discover. i have my discriminator and the message. that i had despatched so let me do exactly one. more let's just say good day global. and there we have whats up global ok so. that's fairly basic with a bit of luck. that makes sense. yet let's do something more progressed. ok let's as an instance. let's just say if i desired to ship the.

Message. returned to the user if they are saying hiya so. what we'll do. is we'll use an if situation. and we're clearly going to envision to. see if. message.content material remember message.content material. is a string. it is the particular string the actual magnitude. that the user had sent the particular. message. and we are gonna in actual fact determine to see. if they despatched. the message hello so we're gonna compare. it to the string hi there. a hardcoded string and if it's genuine. ok if this is a situation is right. we will cross ahead. and do message now if i desire to send the. message lower back. to the user and tag them i'm able to just do. message.respond. and i can simply say howdy there. okay and let's shop and let's have a look at what. happens after I variety hey. so hi there so see how the bot responds it. tags me it says hiya there. as an example if i desire to ship a message.

that does not tag the user. so if i want to send the message and never. variety the user what you can do is you could. reference. the channel property good firstly. let's talk about what the channel. estate is. if you cross over returned to the documentation. and iif you click on. message type ok and if we go over to. channel. channel is the channel that the message. became despatched in and this may either be a. textual content channel. that's any channel that is contained in the. precise discord server that's text elegant. or a dm channel that's the dm among. the the bot itself and the user so in. case you didn't recognize you'll dm. the bot. so if i click on textual content channel you are. gonna see we have a gaggle of different. homes. and we've a bunch of other techniques. okay we are not gonna cross over each. single property technique. but when i desire to ship a message returned to.

The channel the strategy that i would need. to call. is dot send and you can find over right here it. says sends a message. to this channel and what is pleasant about. this documentation like i suggested previous. is it is well documented it has a group. of other. examples so you could reference it it. suggests you how to ship uh just easy. textual content messages. you can send files you can send embed. messages tons of of things. okay yet we're simply going to send a. easy message so let's call dot send. and let's do exactly howdy now one thing's. going to occur. and ii'm going to show you men what is. going to occur and ii'm going to show. you men how to solve it and it is a. ordinary train. that everyone in general does in the. discord bot development neighborhood so. right now we're checking to work out if. message.content material is the same as hiya.

 

 

And iif it is we are going to send returned hiya so. we're basically sending an identical message. returned. watch this howdy. and if you actually see it is going to. say hello time and again it is. truly. uh sending a message a dissimilar. distinctive times the explanation why there is. this put off. is because there's a cost decrease and that i. suppose that's dealt with by means of the library. itself to prevent you from being price. restricted. so that's why it's only sending 5 . messages at a time but you will discover right. over right here that it is going on. but why precisely is happening so permit me. correctly simply restart the bot so it. stops spamming the channel so the explanation. why this is occurring. is due to the fact when we send this message. howdy it's going to sincerely. uh examine this to genuine correct because. mesh.content material equals equals equals whats up. that's real. and then it is going to ship this message.

lower back to the channel. that is additionally  hiya so while this message. changed into sent by using the bot okay. this message was despatched through the bot it mentioned. whats up. and while the bot despatched that message it. also caused the message event. so in view that that still brought on the message. event our callback operate become also. known as to boot. ok accordingly why we also have the logs. over here due to the fact by means of default this does. not forget about bot messages. ok i simply desire to allow you to men know. that so because it does not ignore bot. messages. it performed this common sense and it mentioned okay. appear message.content is also equal to. howdy it is genuine so it will ship. this message so in actual fact infinitely. we will ship that message again and again. and yet again. without a doubt  it didn't happen earlier than. since the message that we mentioned changed into. good day world . correct we replied to the user or good day.

There rather and. that is unique than the actual. message.content material itself. so to fix this what we wish to do is at. the. very desirable point over here we're going to. cross ahead and use one other if condition. and we essentially just desire to envision to. see if the writer. of the message so message the author and. if we go back to the documentation. keep in mind what writer. is author is a user sort ok. and if you truly inspect the. residences there's a property known as bot. now this can be a boolean. and it truly tells you whether or. now not the user is a bot so if. message.writer.bot is. genuine then meaning the. writer of the message is a bond now. definitely we don't have to match it to. true we will do just if message the. writer of bot. and then if it really is true we will simply. return. and that actually will discontinue whatever. from executing after so this would notably.

a lot ignore. bot messages of course so that it will. manage bob messages you may. manage it manually you only intend to make. sure that nothing else occurs. after so you only want to come back so now. let's cross forward and go back up right here. or cross inside this uh if situation over. right here and let's cross ahead and ship. hello returned and let's examine what occurs. so. see how this time the bot sends the. message perfectly quality. however it does not send it again and again. back because we are checking to work out. if the message changed into sent with the aid of bot and if it. is we're returning so it's not going to. execute whatever after this. whatever after this line over right here so. with any luck that part is sensible. so that's a very easy uh reaction from. the bot okay so the next thing that. i desire to do is i desire to show you men. the way to start with developing a few.

Easy commands. for our discord text channels so if you. guys aren't acquainted with what textual content. instructions are. essentially with maximum chat purposes. they have their own commands. and they clearly increase using. the chat. if you really seem on discord if you. variety forward diminish there are actually. some . builtin instructions that discord offers. for you but these commands aren't enough. due to the fact routinely we'd we may want. sure instructions to do other things such. as fetching information from an api or getting. data from the database et cetera. so we're going to cross forward and create. our own instructions so some thing that i. might want to point out is that commands. in actual fact start with a prefix. so in our case we'll use the. dollar sign for our prefix after which. after prefix is going to be our command. name so as an instance uh dollar signal and.

Then kick. that would be the kick command and. instructions characteristically don't have spaces. and also you should not have areas  together with your. commands it's going to make it plenty. tougher . to actually set it up anyways yet after. the gap. every thing else after that's implied to. be a command argument. so as an instance if i would like to set up a. easy kick command i want to kick. somebody from. the server i'd do dollar sign kick. and then i would move in the user id so. the user identification may be one two 3. and that might kick a person if they were. present in the guild. or within the  server ok so with a bit of luck that. part makes sense so let's pass forward and. just start with this. so within my bot.js dossier up top over. right here. i'm just going to claim a variable as. fixed. known as prefix and ii'm going to assign it. the cost of a dollar signal that's.

Essentially just gonna be a string okay. this can be whatever you want i'd. propose you now not. use the at symbol due to the fact that could try out. to feature clients. uh you should not also use the forward. diminish yet i'm simply gonna use greenback sign. for now. so what i'm additionally  gonna do next is inside. the message occasion because remember whilst. we ship the command. it is also a message which is going to. pass over the message occasion. what i'm going to do is i am first going. to envision to work out if the message itself. if the message.content material ok if it begins. with prefix so this starts offevolved with method. is a technique at the string prototype. due to the fact we all know. content is a string that allows you to name dot. starts with to examine to determine if it starts. with. a substring which therefore we are. checking to see if the message begins. with prefix. so if it does then meaning they are.

attempting to challenge a command. and that command could either be a legitimate. or invalid command which will handle. ourselves later. so the subsequent aspect we'll do is. we wish to get the particular. command call so we know for a indisputable fact that. every little thing after the prefix. is predicted to be the command call so. as an example if i do dollar signal. uh to illustrate google seek. ok our command call is going to be. google search. so what we will do is we are going. to get that command name after the. prefix so to try this. well first let's claim a variable. called command name. and ii'm going to go ahead and reference. message.content material over again. and ii'm going to call the substring. method due to the fact we wish to extract a. substring. from a whole string so everytime you . desire to get a string. like a a undeniable part of a text from an.

Overall string you would want to use the. substring method. so the substring method can absorb one. or two parameters so you will discover over. here it says return to substring at the. targeted area. now we wish to return the substring at. the position. after the command prefix. ok in our case with strings the. command prefix itself. is at location number 0 so we wish to. go back every little thing that is. after the prefix that is going to start. from situation one. all the way as much as the top over here ok. so now to do that. what i'll do is i'll. first circulate in prefix dot length. okay so now watch this so if i console. log command name. i want to show you men what the particular. output is so let's pass over here let's. simply anticipate our bot to log in. ought to in just a pair mins like. okay there we go so let's do.

Kick and you are going to work out it gives us. kick so all over again if i do. kick that is going to offer us kick but if. i myself do kick one  3. that is correctly going to present us the. entire string after the prefix. so if we want just the command name we. cannot just try this because we'll have. not just the command call. however the arguments after okay keep in mind. what i pronounced previous for you to. never have areas  in your commands ok. so what we are correctly going to do. is we're going to think that each. unmarried uh. argument is going to be break up up by means of. areas  going to let's simply call. dot trim so this will trim each of the white. space earlier than and after. okay well in fact you recognize what let me. do this right over here allow me do this. before substring so which will trim all. the white area earlier than. and after ok and then what i'm going.

To do is. permit me actually layout this slightly. nicer so it appears. so we do not have an extended line of code. so after the dot substring i'm going to. split it up so i'm going to cut up it by means of. utilizing an easy. area over here now this is going to. return an array so it does not make sense. to name this a command call anymore so. instead what i'm going to do is i'm. going to do anything called array. destructuring. okay and what i'm doing first. is the 1st factor within the  array is. going to be established. goes to be destructured into this. command name variable. after which another factor after that. array. goes to be saved within this rx. variable that's going to be an array. so with this spreader operator what it's. basically doing it's clearly just pack. unpacking all of those components within the . array into this rx.

Variable so permit me show you how that. seems like so if i do command call. i would do args and let's do this returned. so let's do. kick one two three you are gonna see we. have the command name and we have one. two 3. if i do kick one area two space 3. it will supply me an array of all. the arguments ok so. i know it's somewhat weird but watch. what occurs if i simply did. args okay if i just did args one two. three. it would in actual fact deliver me only one yet. i want every little thing so i'm going to use. the spreader operator ok that's what. this is called the spreader operator. so with any luck that part is sensible now. one thing more that i must also. address too is for instance for instance if. i did kick. one  three space area space space. and then four five six. watch what occurs it's aactually going. to give us a bunch of other areas .

So to really solve that  discord dice rolling bot  will. actually use what is referred to as a daily. expression. which allows us to clearly match for a. pattern in the string. so as opposed to simply having a hardcoded. unmarried white area we will do a daily. expression. so i am gonna clearly use a simple uh. steady expression like this so it truly is. in actual fact gonna event all. uh white areas  ok so let's copy this. lower back and let's see what occurs. oh we gotta watch for a bot to log in. first. there we cross and there we go you'll discover. that we in simple terms have  arguments so we. had however we had plenty of other. areas . it in simple terms treats them as two arguments. ok so optimistically. this complete idea of instructions makes. sense okay the anatomy of a command. that's so we've the prefix we've. the command call and we've the. arguments. ok so now that we have parsed our uh.

Commands actually. we will move ahead and really verify to. see what the command the user accomplished. so if i want to check to determine if the user. for example in the event that they did an easy command. which includes. kick so we're going to do if command. name is equal to kick. ok we're going to do exactly whatever. like message.channel. we are simply going to faux  like we kick. the user for from time to time we're correctly. going to kick the user. so kick the user let's just make sure it. works you always prefer to make sure. you are testing your code a dead ringer for kick. one two 3. kick the user so we know that thiis works. ok let's do a different command so let's. just say to illustrate. i may also do an elsif condition after. and for instance if i want to do ban and. let's just fake like we. banned the user and banned the user. and let's try this returned so if i do kick.

123. band one two 3 and those are excellent. so if i simply kick itself. it might still execute that so now let's. in fact implement our command. so what i'm gonna do is i'm just gonna. do the kick command for now so when it. involves. kicking clients there is a couple matters we. have got to do first first we have to. in fact. make sure that the user that we're. attempting to kick is really. within the  guild due to the fact if they aren't in. the guild then we can't kick them. however when it comes to banning you may. correctly ban people that are not within the . guild. so what we'll do is we are first. going to get the member item first. okay so i'll declare a variable. referred to as member and that i desire to get the. member object from the guild with a view to. try this we want to really get the. guild item so. remember that if we actually check with.

The documentation okay right this moment we. presently basically have the message object. itself so in terms of. utilizing this library itself you basically. have got to get all your data from. either the client object or. the item that you've got that is within. of the scope of the function. in our case we have the message object. and we will use the message item to get. all. sorts of data that we need with any luck. that is smart. so for instance if i wanted to get the. channel i will do message.channel. if i desire to get the guild the guild. that the message is shipped in i will do. message.guild if i desire to get the. member item. of the user that sent the message i'm able to. do message.member and and so on okay. but we'll get the guild object. so we will reference member. or message.guild and you will discover if i. click the documentation i have a.

Bunch of residences and methods on guild. we are going to check out this in just a sec. but first we'll do. message dot guild and what i will. do after. is i'll reference the individuals. estate now with a view to investigate it. you can find on the documentation. the contributors estate itself is a guild. member. supervisor okay and the guild wide variety. supervisor sincerely manages all of the. members that belong to. guild and there are different homes and. techniques so you can do matters which include. bang the user you could fetch the user. you may prune the user or yeah you may. prune the user you can resolve the user. you could unban the user etc now notice. how there is no kick technique. so we really want to get the member. from the cache. okay so we are gonna move forward and. reference message.guild.individuals keep in mind. that is the guild range supervisor.

And the guild range supervisor has a. estate known as coins. ok now cash is actually a group. okay now if you truly want to know. what a group is you could. click the documentation over right here and. it will let you know what it is. but collections are in actual fact just like. maps okay so this was a. another category that was built on. good of maps it extends maps. ok and it has additional software. methods it's aactually. very very easy to use it's nothing crazy. okay. the bottom map itself does not have a. technique inclusive of. filter out or uh reduce or kind. yet this collection itself does okay so. when you consider maps you can assume. approximately key importance pairs. and in terms of discord every little thing. has an identification. users have an identification i have an id so for. instance so as to get my identification. i will correct click on reproduction that is my identity.

If i want to get the id of a message i. can correct click on the message and i can. click on copy id. that is the message of the i that is the. id of the message if i want to get the. identification of the channel i will right click it. and i can paste it over there ok so it. makes sense for. the library devs to shop every little thing. inside collections. because every little thing is mapped via an identity. and when you reference matters in a map. it's aactually much quicker. than looping by means of an array for. example ok. we are not likely to move super extensive and. time complexity yet i simply desired to. point out that. so if i desire to get the particular member. item from. the cache that is a set diminish. map ok. if collections appear difficult to you. just consider the cache as a map. and you can see over here that this map. itself. okay it maps the id of the guild variety.

To the actual guild number object itself. and if i actually click on guild quantity. it'll provide me. the documentation for it and it'll give. me all the properties and all the. techniques okay. so let's take this one step at a time. first let's call dot get remember this. is. a similar exact method that is part of the. map. and we are gonna move in the particular consumer. identity. ok so for this kick technique or for this. kick command i'm actually expecting. uh args that is an array okay. to be nonempty so i'm gonna do one. determine i will move ahead and verify to. see. if args dot length is equal to 0. then i am just going to return a message. and ii'm going to say message. dot respond please. uh provide an identity in order that way. once I send this message i don't have. to it will not execute anything else after. ok but if they really did provide an.

id then this will be fake. so i am just going to head forward and. reference args subscript 0. similar to that because that is going to. be the identification because we are only going to. kick one user. ok so now watch this. if i'm going ahead and console log member. let's go ahead and move into the console. first let's try this let's do kick one. two three you're gonna see that it says. undefined if i do. kick it says anson please supply identity if. i do kick area it is gonna do a similar. aspect. ok now if i desire to kick a user let's. cross forward and kick uh. this bot over here let's attempt to i am. going to paste the identity and notice how we. aren't getting the error message again. and if i look within the  console we've all. of. the info of the guild variety see it's a. guild number object. it has the guild estate it has a gaggle. of alternative properties to boot.

okay we are unlikely to reference every. unmarried one of them but we just need to. ensure we have this guild variety. item. ok and iif you look at the. documentation you have all the. houses. and all the methods and iif you click. them it's going to let you know. what every estate represents and iif you. look into the methods it will inform you what. each method does. okay so what we will do is we are. going to ascertain to determine if. member is truthy because if this member. isn't present in the cash. then meaning the member is likely now not. in fact in the guild itself. okay so we will verify to determine if. contributors truthy. so if number so in that case then. we will move forward and kick the. user so i can actually do exactly member. dot kick ok and i delivers a purpose. if i desire to but i'm just i am not gonna.

do this for now. and if the member is not discovered then. we are going to simply say message. and we will send this message to the. channel and not reply to the user. so we will simply say uh that. member was no longer found. okay so watch this. let's first do kick one  3. that number became not found okay now watch. thiis if i attempt to kick this bot. you're going to see that an blunders is. going to occur and it's saying lacking. permissions due to the fact our bot does not. have permissions. so we need to go ahead and give our bot. permissions so. characteristically what you ought to do is you. ought to create a separate role to your. bot. so as an instance i'll create you. recognize i am simply going to create a special. role discord bots. ok and you must basically. uh let me give it a colour just to uh so. i'm able to stand out a bit. ok you furthermore mght prefer to ensure that the.

Bot itself has permissions. above other users so for example. uh for instance this discord bot is above. every person. okay however it won't be able to in fact do. anything to a tested. bot moderator admin so if you're sort of. new to discord and if you do not certainly. comprehend the total. function hierarchy truly as an example. if my bot only has this discord bot function. and it's trying to kick somebody with the. tested rule it won't be able to do. that. because uh the function is greater okay so. i will show you an example. yet you desire to ensure that for this. role you must give its. kick individuals okay and let's go ahead and. try this so. first let's deliver this man the discord. bots roll. so it is going to have the ability to kick brainy bot. because it would not have roles. yet first let me give it the validated. function genuine speedy uh hold on. allow me do this let's simply make it so.

They may do that ok. there we move so permit me cross ahead and make. it so that it may kick. this ball over right here so now if i do kick. you're gonna see that not anything occurs. even though we do have the roll. it does have the permission ok it has. the particular tick members permission but. it nonetheless isn't able to kick it because. it's. underneath the verified position. because that is simply well-nigh a excessive. level rationalization of the full function. hierarchy. so if you really wanted to be able to. kick the proven position or any person with. the confirmed role. then you would need to placed it on top of. it. so i store and ii'm going to get the identification. returned. if i kick detect how the bot is long past. and there is no errors this time those. were from the former ones yet notice. how the ball is long past due to the fact we simply. kicked it. okay now if i also uh take away .

The confirmed rule from this man over. here so let's do kick. and let's do reproduction identity. ok detect how it nonetheless works so any. function. that's underneath the discord bots role. will have the ability to be kicked. or banned okay if we supply the ban. permissions so hopefully that side makes. sense okay and obviously if i attempt to. kick you know them again it's going to. say the member was no longer found. so it's important that you. understand the complete permission. as hierarchy as the complete roll hierarchy. okay. but that is practically the simple gist. of the kick command okay it is. that simple very well so allow me cross forward. and just get my user back on the server. okay so let's just say to illustrate if. the bot would not have permissions so. let's just provide it. the validated position again good let's simply. deliver it let's deliver it one other let's.

Deliver it a. the admin role ok so as an example if the. bot didn't have permissions. we should technically send a few style of. message to the console or the uh the. channel. letting the user comprehend that they do not. have permissions or the bot design. permissions. so detect how within the  console earlier. we had this uh unhandled promise. rejection caution so something that i. should mention and ii think i mentioned. this earlier. is that everytime you  call methods like. dot kick. ok they are going to correctly return a promise. ok. so we should always technically manage the. promise and we shall also manage our. blunders in case it happens since you. never know what would occur. so once we solve the promise this will. actually go back. the particular guild variety that turned into kicked. so if i go here. it will say kicks this member from the. guild and returns.

Promise guild member so let me go ahead. and do that. so let's hand the promise by. calling.then this is going to offer us. the member. that became kicked so i am simply going to. keep in mind. and what i am going to do is i will in fact just. send a message to the channel. ok and i am simply going to claim oh let's. do that remember. become kicked ok so. the reason why i am utilizing string. interpolation is because i desire to. actually mention the member that became. kicked. so to do this you possibly can simply uh. move within the  member parameter. immediately within in among the curly. braces and it'll actually uh on the. consumer. that is quite cool now if they don't. have permissions we are just going to. send the message. we are simply going to say message.channel. let's ship. i do not have permissions. ok now keep in mind that uh the bot. permissions are completely different .

Than actual person permissions so. you must always make sure you're. checking to determine who's executing sure. sure instructions so in our case this kit. command. is technically a moderation command so. you need to make sure that you're. checking to work out. if moderators are using this command. ok. so as an example let's cross ahead and. do this let's attempt to kick yeti back so. it is going to say. i don't have permission so it. technically does however it can't kick it or. i haven't got a permissions. to kick that consumer well let's do exactly. this i can't kick that consumer. ok and iit could be due to either. permissions or the person's better. okay and uh let's simply. eliminate the uh rule again and let's. kick them returned. and now cs as the identity changed into kicked and if i. click on it it'll exhibit the particular. user card. okay so that's extraordinarily distinctly.

 

 

basic now if we wanted to. correctly examine to see if. uh the person that turned into executing the. command became both a moderator or admin. or an owner. lets  do this we can actually try this. right up top over. here so for instance for example i can do. something like if. message dot member so this time i'm. referencing message.member rather than. message the writer. because there's a change between the. precise user variety and the guild range. sort when we're speaking about. permissions. roles nicknames we wish to reference the. member. if you are attempting to get the user's. discriminator or the user's username. you need to get that from the particular. consumer variety itself but i'm going to cross. forward and just examine to determine. if the member has permission so that's. a further method and that i can actually simply. pass in sure permissions so for.

Instance i want to make sure that. the user has uh kik. members okay so if the user has kik. individuals. then we will allow them to use it if they do not. then we'll just return a message. we are going to just say you do not have. permissions to apply this command so if. you do not have the permission. uh we'll do exactly message.respond you do. not have. permissions to use that command. and that ought to be an attractive. straight forward so let's do this back. and this time i'm gonna even have to. do that uh from my alt account. so allow me move forward and simply invite my. person again so permit me just cross another. account ok so. i'm in the channel and i am just gonna. kind kick. and see the way it says you don't have. permission to apply that command. ok but when i provide it as an instance if i. give it the uh discord bots function. which does have the permission if you.

click on returned. observe how it's announcing a further. message it's saying please provide an id. which basically mentioned that thiis part turned into. uh fake. so it didn't go back after which it is going. over here. if i attempt to do let's do one other thing. let's do. kick one two three it just says that changed into. not discovered okay so. our other account does have permission. to apply the command if i eliminate it. returned. you do not have permission to apply that. command ok so with any luck that's. pretty elementary now let's do one. extra command let's do a band command. so if i desire to do a band command it's. very like  the kick command. okay but in fact the nice factor approximately. the band command is that we don't. actually need to. determine to work out if the user's in the guild. because we will ban users that are not in. the guild okay so we will do the.

Same element we'll determine to determine. if the command name is the same as banned. this time and ii'm going to copy and. paste. this code over here yet this time. instead of examine and spot. if the permission is kick contributors we're. going to work out if the man has band. participants. permission okay so if they've this. permission. then we are going to allow them to use. that command if they don't then we can. simply ship that message returned. and i'll additionally  replica  uh this besides. and you will see we've loads of. reuusaable code which you can begin to determine. that. it's aactually you know becoming a little. bit messy yet of course like i said the. complete factor of the educational is to. make anything that's very simple i. don't desire to you recognize. create some  kind of loopy opinionated. constitution but when you guys have an interest. in that i do have plenty of other.

Tutorials on my channel. that conceal so many various things with. discord bots so feel free to check it. out. but for now we are going to just stick to this. okay so the subsequent factor that we are gonna. do. is we're going to reference the guild. object returned so we will do. message dot guild and we're correctly. going to reference contributors. and we will call the band technique. and the band technique takes in what is. referred to as a person resolvable. so consumer resolvable can both be the. exact user item. it's the member item it can be. the. identity of the man you're attempting to kick. or ban. or i'm sorry it can be the identity of the. person who you're attempting to ban. and declare ban okay so in case you click. consumer resolvable it'll let you know what it. is. so it could either be the user object it. may be the identification which is known as a. snowflake.

It is the message object so that. will simply very nearly remedy to the. author of the message. ok or it may be the guild wide variety. item itself so in our case. we are just going to move in the identity. directly so we don't have to actually. fetch the member from the cache. we are able to simply move an args subscript zero. and now simply directly ban the user. so for example uh let's go ahead and check out. this. so something that i additionally  desire to do. is i need to truly handle the errors. without a doubt . so i'm just going to head ahead and just. trap the mistake i'm not going to unravel. the promise. with a dot then i am going to just log the error. for now. simply to show you guys what the errors. seem like. ok so let's pass forward and let's attempt to. uh let's attempt to do band 123 and let's. see what happens so you'll discover that it. correctly says. unknown person ok so unknown user.

sincerely signifies that the user is not. found. so allow me in fact get the identity of a. extraordinary uh. bot that i have it isn't within the  guild. but if i try this it's gonna say. uh lacking permissions ok so unknown. person so thiis this user isn't discovered. yet thiis user become ok and this is. truly certainly one of my other bots. so today it says we're missing. permissions our bot is missing. permissions. so we're gonna have to present our bot. permissions now so let's pass forward and do. that so let's return to the function and. let's simply. enable band members and let's try to. do it back and iif you appear over here uh. we don't have the mistake anymore it. actually banned the user if i myself. desire to see all of the users which are. banned i can visit server settings. and you can find that i have uh. cinderease which is my different bot that is. the username band.

 

So if you truly circulate in an invalid. consumer then it in fact will not paintings so the. consumer actually needs to exist. but the person doesn't must be in the. equal guild okay so let's actually handle. our errors like i reported we always desire to. handle errors to provide any. uh messages back to the user in case. something would not paintings but this time you. comprehend what i'm going to do. in place of using uh then trap i desire to. swap it up just a little. i'll use async away so in case you. guys aren't aware of async look ahead to. i tremendously suggest you gain knowledge of and. become familiar with that due to the fact. with discord.js these kind of techniques. that you're going to call are going to. go back supplies. and there are going to be some times. where you're going to really need the. information that is lower back. ok and you might want to use it for.

in a while so what we're going to do is. we will upload the async keyword all. the way. in the front of our callback function. and then correct over here the band technique. will go back a promise. ok it's aactually going to come back the. member that was banned. or it's going to return the uh the user. or member that become banned. okay and what i'll do is instead. of utilizing denim.capture. i'm in fact going to use try out capture this. time i do know  it seems fantastically messy like. our code appears exceptionally messy. but the complete factor is i desire to show. you guys the various uh techniques. that you'll be able to cross approximately it due to the fact with. discord.js it's a extra improved library. so i want to attempt i want to do what i will. to reveal you men extraordinary tactics. when it comes to. you know writing your code using the. scorches library. okay so with the intention to  actually use async.

look forward to efficaciously. we need to first actually look forward to this. technique call. so i'm going to do const and since this. returns uh the actual person or member. option itself. i am simply going to name this uh const. consumer. okay and we will look ahead to the. promise. so this is going to attend till this. promise has been fulfilled. and iit's going to go back the user object. now if i console log the user. it is going to supply us the particular user. information and if i console log the error it. will let us know the error. so let's do that back so let's move. forward and do band. 123 okay and let's variety that and let's. it will say unknown person okay so. that's fantastically cool. um and let's additionally  do this uh let me do. one thing more. let's do uh ban and permit me get the identity of. my. other bots so this bot is at the server. like i suggested. and if i ban the user you're going to.

See that it returns the identification now notice. the way it in fact does not return the user. item. and if you truly seem over right here or. let me correctly visit the documentation. to show you men this. so it makes extra feel in case you move over. right here it returns a promise. and the promise resolves both the. guild number. the user or the snowflake the snowflake. is the identity the explanation why it's returning. to snowflake. is because thiis user is truly nowhere. to be discovered. in the actual client's cache okay. so because our bot is usually because. our since the person that we are banning. is actually not in. any mutual guild that our bot is in it's. simply going to come back the identification. okay but if the bot was actually in the. same. uh guild that it become in as our different bot. then they would truly return the. precise item itself ok. so what we're going to do is we are going.

to move forward and simply truly we are simply. going to display message we will do exactly. message.channel the ship. person became banned effectively. okay and if an blunders happens we should always. necessarily send the error to channel. uh we will say whatever like an blunders. occurred either i don't have. permissions. or the user became not found. okay so now let's pass forward and let's uh. ban good let's unban any other consumer. so let's unban my different my other bot. account and once I say bot account what. i mean is like the actual. like a precise discord program like. self botting is actually. opposed to terms of service so i just favor. to elucidate i'm not selfbotting. the account that i am using is a precise. different discord. bot account that i created in the course of the. portal ok it's simply in an extra. guild and ii don't have yet another account. to use. okay i'm able to just ban this one yet i will do.

That later but the entire reason why i am. showcasing it is because. i desire to show you men how to prohibit clients. that aren't in an analogous guild. so if i do ban the user become banned. effectively ideal. and let me ban this guy. there we pass use was banned effectively. distinctly undemanding so. that is practically it when it comes to. kicking and banning. okay so the something i'll show. you guys how to do next is to establish. a simple roll response system okay so. this is going to be a really hassle-free one. we're not going to be using a database. for this. so what we want to do is each time we. react to a definite. emoji after we react to a message with a. sure emoji it is going to give us a. particular position. i'm certain you men have visible whatever. like this before in which in case you. move join the discrete server and then you.

Click to illustrate the roles channel. there are a bunch of messages after which. it tells you which of them emoji. will give you which role so i've a. couple of. guidelines established on my server ready yet you. simply desire to move over to roles and just. click this plus signal and simply create. whatever position you desire. so i can just call this position node.js for. instance. you also want to make sure that your bot. has the manage guidelines permission if it. does not it won't be able to upload rules. and one thing that i need to mention is. that your bot will in simple terms. be capable to add roles to any position that's. below the particular rule the bot has. so what what it capacity is this bot over. right here it has the discord field rule. so meaning it is just going to add. clients. to any position below it so if it tries. to feature the user to the bot. moderator admin position it's unlikely to.

work so let's move forward and installed our. message so we're gonna have a javascript. and ii guess we can. use whatever emoji we want i'll. use apple we will do python. so for python we will do do we've a. banana emoji for c sharp we will do. grapes after which what become the last one. java we will do peach. so essentially we're going to send this. message whoops made a mistake over here. and ideally what we want to do is we. want to just add this sort of reactions. and iit's going to deliver us. the role so if i add the apple emoji. it will deliver us the javascript. role if i add the banana emoji it's. going to provide us the python role. if i add the grape emoji it is going to. give a c sharp and if i add the peach. mode it will give us the java rule. so let's pass ahead and iimplement this so. there's actually a couple things we'd like.

To do one we'd like to listen to a new. event. okay so we're now not contained in the. message occasion anymore we are correctly. going to listen to a new occasion and it's. known as. message response advert whoops. there we pass okay so this is another. occasion and the callback function takes in.  parameters it's going to take up. message response item so we are gonna. call it reaction. and it's gonna absorb a consumer item. ok so the reaction item itself has. residences which include the message it was. reacted to the emoji that it was reacted. with. and the user object represents the user. that reacted. to the message so if we wish to get the. identity of the user that reacted to the. message then we are able to do that to boot ok. now the very subsequent factor that we wish to. do is we want to investigate to work out. which message is receiving those. reactions so what i will do is.

want to ascertain to determine if. reaction.message.id. so keep in mind reaction has a property. called message and message. has a property known as id everything has. an identification or now not every little thing yet. most  things have an identification so we will. first check to see if the identification. of the message fits this because. that's the proposal of the message. i right clicked it and i clicked on copy. identity. and paste it in the image of that now the. next component we'll do. is we will verify to see if any of. those emojis have been. further we're using unicode emojis okay. so with unicode emojis they actually. haven't got ids we'll examine the. name. instead so we're going to get the call. through absolutely we will destructure. this from. the reaction dot emoji estate. so remember it is the message response. item. and it has the emoji property and the.

Emoji estate has the call. okay so now i can actually verify to see. which mode they react with so we're. going to use a switch case. after which the first case i'll. have is i need to envision to determine. in the event that they react with the apple emoji now. how do we actually get the call of it is. it in fact named apple well no longer relatively. i can't do something like case apple. or i am unable to do something like if name is. equal to apple. rather since these are unicode emojis. we want to truly get the unicode. itself so to do that. on the discord client you'll be able to. do. backward minimize colon and the call of the. emoji. so apple and you'll reproduction it. and it is easy to just paste it. directly in there. we're going to do a similar element with. banana. grey oh grapes. after which peach okay so one can simply. straight copy that. so simply ruin right over here.

 

We are simply gonna set up our switch case. real fast. and then we can replica  that aas well so. now in the event that they react with the apple emoji. we will pass ahead and give them. the javascript role so now we really. have to deliver the role to the user. so we will cross ahead and duplicate the. identity. of the javascript rule after which what. we'll do. is every time they react with the apple. emoji we'll simply supply the function. to. the member okay so when I say member we. desire to actually get the member item. itself. so what i will do is ideal external. over here. all in the end i am correctly going to go. ahead and. get the member from the guild to be able to do. that from right here now. we need to reference the guild estate. or the guild item someway. well we will do that by way of without a doubt doing this. reaction dot message okay remember the.

response has a estate known as message. and like we did before the message has a. estate called guild. and guild has a estate known as contributors. okay it is a guild number manager. and it is a selection of each of the. guild numbers. or i am sorry now not a set this is. the executive to get the actual sequence. we'd reference cash. which is the collection so now we can. reference this cache remember the cache. is just a group also commonplace. as a map ok the gathering is a map so. we are able to call the dot get method. and then we will flow in the id of the. consumer to get the. exact uh member allow me call this member. i did not suggest to name it guild sorry. so we will circulate a person.identification okay. remember there's a huge difference. between. clients and contributors to feature a role. to a particular person at the server you.

have to get the member object. okay to replace permissions into the. member item and so on so now. if that is actual we're going to reference. member dot roles. after which we will simply circulate in the id of. the position. after which we're going to do a similar. thing for uh banana so banana became python. so let's get the python roll. quantity dot guidelines to feature python. and just make sure there we go c. sharp became i feel uh grape. let's remember it's member dot rules. grape and let's do a different. uh i think python was peach right or. java become peach. sorry java. ok so keep in mind the guidelines and upload. very well so let's just see how this. works okay first. let's actually so i am actually going to. show you one thing genuine quick. ok so watch this so our bot. is going to restart and that i want to exhibit. you men anything so permit me open up the. logs. over right here the console i'm going to move.

forward and react with the apple emoji. and you're aactually going to notice that. not anything is happening in the logs i. correctly did not be given the roll. nothing occurred. and you're might be wondering well is. our code broken. good not always ok so. let me show you one other thing permit me. in fact send a message. and allow me react with the apple emoji. again and you are going to work out correct over. here that it does say hiya this time. so the question is why did it now not paintings. for this message. yet why did it work for this message. okay so in discord.js. the library itself correctly caches your. messages what that means  is. anytime a message is distributed to any. channel it's stored in a group. okay now the message reaction at occasion. is simply emitted. for cached messages simply so if the. message is in memory if it is stored in.

The collection it will emit this. occasion. if it isn't it will not emit this occasion. by default so so as to work round. this. we'd like to really opt into anything. known as partials so partials are a. discourages suggestion that allow you to. handle uncached information so information. that is not stored in the cache but it. will nonetheless allow you to use. the development itself okay so. fortunately we really simply want to write. one line of code so contained in the. constructor. so new purchaser each of the manner online 4. we're going to move in an. object inside the parentheses and we are. going to specify partials. and we'll permit the user or. no longer person. we're going to permit the message. partial after which the response partial. because we are using message response add. so we'd like both of these so now this. ought to paintings without any doubt. so if i return to the console and i'm.

Going to react with a different emoji. this time. so simply deliver a sec ok so let's supply it. the banana emoji. and you're gonna see that's hey and. did it deliver us the function it did it gave. us the python rule. permit me react with the grapes emoji it. gave us the csharp function. and let's react with the peach emoji. it's going to supply us the java rule. so notice how now it is working before it. wasn't the reason why is due to the fact we. opted into partials okay whilst our bot. restarted this message. turned into nowhere to be found cache now in case you. in fact fetch the message from the. discord api it will likely be cached yet as soon as. your bot is going offline and it comes lower back. on-line. this message is nowhere to be found in. the collection of messages which are. cached. so so as to get it to work if it is. now not cached you need to make sure you choose. into partials.

So let's react with orange and spot. how nothing's gonna take place. ok it will nonetheless set off the event yet. nothing's gonna happen. ok and likewise whether i send a new. message and i'll react with uh as an example. green apple. it is still gonna work ok so hopefully. that makes sense and optimistically that. grants an expertise on why you. have got to use. the partials now in an effort to eliminate. the function. you could do just a similar issue  so we're. going to repeat that code and we're going. to paste it. and this time we will use the. message reaction. get rid of event okay and iinstead of doing. roles.add we're going to do roles.eliminate. now let's store and like i reported due to the fact. we're opted into the partials. this would work so let's test it again so. i've all the roles well now not all of. them i need to uh. deliver myself the java javascript rule.

So let's deliver us that role so i have all. the rules so now. let's pass forward and unreact you'll find i. acquired it bumped off the javascript rule. it bumped off the java rule. ok remove the c sharp roll and. then it got rid of the python rule if i. react returned it will give me the. javascript position so that's pretty much. how it works. so hopefully this become an easy. system that suggests you guys how to set up. reaction roles. and just a few suggestion make sure you  do. now not uh. allow users to add themselves to the. admin function okay or any moderated function. that might be below the discord. bots function okay so i will show you. guys a way to. use cyber web hooks now so in case you guys are not. primary. discord correctly has cyber web hook. integration so one can use. for instance internet hooks with github so. whenever someone.

Triggers a few kind of event in your. calendar repository along with a celebrity a. fork. a pull request it's going to ship that message. to a specific channel. okay so we're gonna move ahead and primary. set up a web hook. so you really want to go into a. channel that you really want the web hook to. ship messages to. so if i am going over to general if i click on. edit channel. and if i click on integrations you are. going to determine that i actually do have one. internet hook. ok so i have an internet hook correct over. here permit me uh return there. and i can create a group of information superhighway hooks. that do different things so. as an example i'll create this cyber web hook. and i'll just call this uh. announcements okay and you may select. the channel that you want the webhook to. be in. so we will just leave it as widespread and. now you really desire to get the. data of the webhook so we're.

Going to copy this internet hook url. so if we really paste this url into. our browser you will see that we've. basically json object lower back so let's. cross ahead. and primary let's get the id so the identity is. correct over right here this. uh the webhook id is after this web. hooks course. okay so we'll reproduction that part. after which the token. is every thing after that okay. so i'm going to shop these items within. an atmosphere variable. so permit me pass forward and open up this. and we will name this webhook id. webhook token so first let's get the identification. so there we move now let's get the token. there we cross clone of. clone of that ok so now the subsequent. element we're going to do is we wish to. instantiate. the webhook client class so discord.js. comes with the webhook. client category that makes it very easy to. use webhooks. so we're going to move forward and import.

That. class from the discord.js module and now. we'll pass forward and declare a. new variable we will call the. webhook. purchaser and we'll use a brand new. keyword to instantiate webhook client. and it takes in 3 parameters the. last one is alternative the last one is the. ideas. but we will take in the identity so. that's the webhook identification. so we'll reference that by way of. our surroundings variables that's webhook. id. and then webhook token. ok there we pass ideal so now. we can actually write a simple command. to send webhook messages. so now we will cross down all the. way to. inside our message occasion after our band. command we'll. write an elsif announcement that's going to. verify to work out if the command name is. equivalent to announcements. ok so this is largely going to be a. image  bulletins command with internet.

Hooks. definitely you can use regular messages. but we will use web hooks since you can. definitely use web books for a group of. different things except for discord you. can use it with slack messages i believe. i think slack has internet hooks you can also. use with github you can use google like. which services have cyber web hooks. integration and you can just play around. with that so now we want to in actual fact. take. in as an argument the actual message. that the user wants to send so for. instance our. prefix dollar signal so announce so. correctly adjustments to announce. okay and i want to say whatever like. hi there global and that i desire to at all and sundry. the issue is at the moment we're splitting. all of our arguments with a daily. expression that eats up all of the white. space. so what we'll do is we're going. to do the opposite we will become a member of.

every little thing with a space instead. ok so we will join all of our. arguments so. const message. and that is going to only we're going to. be taking args which is our array of. arguments. and we'll join it with an area. the reason why is due to the fact like i stated rx. is an array. that is divided up via white space. so with a view to get the particular common. message we need to opposite the operation. and join it back and i'll console log. too so that you men can see what it looks. like now let's in fact send the message. with the net purchaser so webhook. client.send. and we're going to flow in message and. now. deliver it some time and now inform the realm. so see the way it says hello world  at. everyone which is fairly cool it additionally . can point out. rules that is uh very useful. okay i'm able to do announce hiya. excellent if i inspect the console you could.