simple Discord Bot in Python<br/>what is going on guys welcome back in. trendy video we're going to learn the way . to build a discord bot with python so. allow us to get right into it. now before we get into the actual. tutorial i would like to say that this. video is backed via tab9 and as always. don't skip that side because it's going. to be fascinating to you i don't. suggest anything i do not promote. whatever that i don't believe is. interesting and useful. and i don't use myself so as you. can see down here after I flow the window. here. when I circulate my digital camera you will discover i am. utilizing tab 9 i'm actually using it. in maximum of my tutorials i believe in all. the new tutorials i'm utilizing tab 9. despite the fact that not every video is backed. and tab 9 is an automobile of entirety. engine that will visit the web site here. and you will discover that. tab 9 code quicker with ai car.<br/>Completions. tab 9 supports all contemporary languages java. javascript. python c sharp c plus rost pass and many. more. and iit's also supported in all uh. important. editors you will see that uvs code you will see that. intellij go. land you can find pycharm you can see. atom emacs chic even. whim which makes my coronary heart beat faster uh. and tab 9 has a unfastened continually plan so. you can visit pricing here. and you will see that the tab nine is. completely free you could pay. zero funds a month with none limits. so you don't have. like just a unfastened trial you've the loose. tab 9 edition you don't want to pay. anything. however if you have an interest in more. automobile completions in bigger car. completions. gpu powered cloud vehicle completions you. can investigate the pro edition as well. and also it is very important to grasp. that tab 9 takes. privateness very seriously so they do not um.<br/>Simply take your cloud information and do a little. nonsense with that they do not promote it to. anybody they deal with it with admire. and they simply use it to train the car. final touch model and they don't do. whatever stupid with that so you could. examine. about their code privateness on codeprivacy. and for this video primarily you can. find aa coupon code in the link in. description down lower than. and you may get 50 off for your first. 12 months of. tab 9 professional if you are attracted to that. otherwise you could also stick with the. unfastened version but i can incredibly suggest. you to apply tab 9. in your programming to speed up your. coding and to make it. just aa better experience overall all. correct so we'll build a discord. bot in python but earlier than we are able to begin. with the coding we have to visit. and we have to mounted the. application.<br/>And the bot itself so that you go to. and you create an account or. you log into your account. and then you navigate to curb. developers. this opens up the discord developer. portal for you and right here you can visit. functions. and right here we will create a brand new software. or a new bot you can say. uh or we will create a bot within. of the application. now something it's essential know is. that when you have a bot and you want this. bot to join a server you should have. the manage server permissions. you can't simply create a bot and join. random servers with it and do stuff. on them you should create you should. either be the admin of the server otherwise you. have got to send the link to an admin of the. server or you need to have the manage. server permissions. so simply maintain that in mind now what we're. going to do is we will click.<br/>On new program and we'll. call this tutorialbot. and we're going to create this right here so. what we then have got to do is you could you. offers. a description you can provide an icon or. anything like that uh. what is crucial is that we visit the. bot tab right here at the left and we create. at uh or we click on upload bot so we click on. on upload bot. this isn't reversible yes do it and now. we've tutorial bot. now some thing that you desire uh or that. you're going to need in a while within the . programming strategy. is the token so i am not going to click on. to reveal the token here yet if you want . uh to know your token you only copy it. and you may paste it into your script or. into the environments we'll. talk about that during a moment. yet that is what you're going to have to. hook up with the bot. now if for some motive somebody finds.<br/>Your token. you exhibit it otherwise you show it to someone. you could simply regenerate the token and. the historical token is not. not legitimate so you can merely use the. new token. but it's what you're going to must. connect to the bot so then we go to. oh auth 2 and here we are able to set the. permissions or firstly we have to. investigate. bot here and down here we are able to investigate the. permissions so we have. as an example we'd like the bot to be able. to send messages to manage messages. whatsoever you must do in fact. so i don't know what we're going to do. we will say view channels okay. we don't want it to manage an excessive amount of uh. i definitely don't desire to offer it the. administrator view audit log. perhaps no longer deal with server manage roles no. match channels no kick participants certain band. members definite. uh change nicknames deal with nicknames.<br/>Uh tournament emojis no we'll ship. manage messages. probably not embed links attach files. we are not going to make use of these kinds of. incidentally i am simply uh. checking them here uh study message. history certain. point out every person certain upload reactions use. cut down instructions. and that's in fact it so. as soon as we have that we're going to have. this hyperlink right here and it's what we are. going to need. to permit the the bot join the server so. what we do here's we reproduction that. we paste that into a new tab and then. what you spot is. um i will select a server to allow this. bot uh join the server and naturally i. can merely select my very own server here. so neural 9 neighborhood and ii could uh. simply. click on preserve here now i am unsure. if we are not likely to get a gaggle of. messages immediately so. maybe we must always flip off the sound right here. um. yet yeah there you go you verify what.<br/>This spot is permitted to do and then you. authorize it. as an admin or as someone who has the. deal with server permission. i am human there you pass and. now it's aauthorized i am unsure if it's. portion of the discord. so if i go to discord trendy within the . browser open discord i should be capable of. see the bot. perhaps i am unsure. um is it there allow me see. welcome living room educational bot just confirmed. up. in order you will find we have the bot now on. the server and now we are able to get into the. coding strategy. o.k. so we are now going to start. with the coding but first off we need. to put in the module and for. this we're going to open up a command. line. and we will say pip install. discord dot py so it's what we are. going to apply with the intention to application the bot. and likewise i'm going to use a further. library that you don't have to use if.<br/>you don't need to. uh and that's the python dash dot n. library and this is important due to the fact i. have an environment file a dot. n dossier as you'll find right here on the left. and ii use this file in order to load. environment variables. into the script now this sounds perhaps. extra tricky than it's it's only a. dossier that has the token. so you don't have to use that library. you could simply take the token and positioned it. right into a string and that's additionally satisfactory yet. of course then everyone is going to. see the token when they open up the. script and since i'm recording i do not. want this to happen. uh yet besides that you can also just. create a normal dossier. and skim the token from there i'm going. to apply the dot n. module because that is somewhat extra. professional and for this i had to do. pip install sprint capital u. i do not know why i had to do it maybe.<br/>Because it's user particular. maybe it really works for you with out the sprint. u uh but then just. python sprint dot n like that. that's what we have to do and when we. have that we can start with importing. the modules we will import. discord and we'll import os for. working manner and we're going to. import. from dot and we'll import. load dot inf and that is it for the. starting so what we're going to do. first is we will create a client. we will say purchaser. equals discord dot client. after which we will say load dot n. now what this does. is it a lot the content of the dot and. file. what's inside the dot n file i am. going to put in writing it down here i am not. going to open it because then you're. going to determine my token obviously. but the 1st line is just hashtag dot n. and the second one line is token.<br/><a href="">mom bot discord</a> after which i've the token right here. regardless of the token is so you could. constitution your dossier like that aas well if. you want to it's going to be loaded into. the script and then we are able to just say. token equals os dot get n. which stands for get environment and. then just token. and by using doing that you have an. surroundings variable but we don't have. it. uh within the manner you only have it in in. the. uh specific script here. so once we've that i am unlikely to. to do any function but i'm simply. going to put the bot on line so we're. going to assert purchaser. dot run and that ought to positioned. the bot online now if we visit discord. server you're going to see that the bot. is not. on-line we've three bots online yet. tutorial bot isn't right here. uh if we scroll down here. to t quite a lot of participants already. there you move academic bot.<br/>Uh you're going to see that the bot or. you noticed that the bot is offline so if we. now run the script you are going to see. that the bot is going to be online and. it is going to appear right here. among the bots and ii'm going to show you. that it's the case we're just going. to run this. uh we've an issue without a doubt we'd like. to move a token here. like that and once we have done that. uh you'll discover that nothing occurs. that is what should occur because. we're not doing whatever. but if we go to the server you will discover. tutorial bot is now on-line. and expecting events and you know it. does not have any function but when we. have some hobbies that we are awaiting. and a few functionality we will uh then. do that and this is what we'll. do in the next move alright so let's get. began with a very simple function.<br/><iframe src="" width="560" height="315" frameborder="0" allowfullscreen></iframe><br/>We're going to create a function. called onconnect and iit's going to. trigger anytime the bot connects to a. server. so we're going to say def on connect. and here we will say ok whilst. this happens simply. write whatever like bot connected. to the server now this operate doesn't. paintings the image of that we have to upload two. things first off we have to upload a. decorator. which is going to say that this is a. client occasion is an. occasion uh that discord has to hear. or that the bot has to hear and. also this is going to be an. asynchronous operate for those of you. who do not know what async is. look into my video on async anticipate on. this channel on asynchronous. python programming if you are interested. in that but you do not really want to. realize what is taking place in the back of the. scenes you only want to know that you.<br/>ought to add the async key-word. and you ought to add it into the. beginning so async. def on connect and above that we are. going to feature the decorator. purchaser.event now if i simply run this. script we will see. that uh the bot goes to claim bot. connected to the server. in the instant it connects to the server. now we can also do other things we will. also say okay. uh broadcast to the total server in a. sure channel. that you are now here in an effort to but. of course this would be. uh pointless right now we simply want. this to have. we simply want this to be locked onto the. console and. now we're going to have one other function. that is going to be on message and with. that operate you can do various. different things. so we'll say purchaser.occasion back. and we will say async def. on message now anytime a message and.<br/>we'll have a message right here as a. parameter. anytime a message is sent in a channel. or. in a dm chat with the bot um. this bot is going to react to this. message and the way it reacts to this. message is dependent upon us. so as an instance we can say okay first of. all if the message. writer so if message.writer equals. client dot consumer if that is the case. then don't do whatever since you are. the one that sends the message so simply. go back and don't do whatever. or else we will do all types of. stipulations here in order to say for. instance okay if the message. dot content begins with. first rate autocompletion right here from tab nine. if the message content material starts with. after which we will say i don't know uh. whats up for example dollar hello. if this is so then we will do. anything. we will say ok get the channel uh. which this message turned into sent into so we.<br/>Can say ok. get the channel after which ship a message. into that channel so that it will say wait for. message dot channel so we get the. channel from the message. and we will ship to that channel. uh i do not know. hey global we are. currently recording a video due to the fact i am. going to run this right here. at the server in a second and then. you're going to see. that the bot correctly responds to that. so now if i run this right here. you could first of all see bot connected. to the server if we now go to the server. and i visit the dms. of the bot so i visit academic bot and i. say okay. firstly i will just say good day so it. does not react to each of the messages good day. nothing occurs ok. dollar hey and then you will find hiya. world we're presently. recording a video and now i will. do that in the final right here. and we're going to get a gaggle of.<br/>one of a kind uh reactions i guess. so if i say dollar hello you're going to. see hello international we're presently. recording a video so the bot can respond. to the on message occasion. into the on connection event it's. just one instance of what we are able to do with. this sort of bot. okay so subsequent let's upload a dm. functionality as an example we've a branch. here that asserts ok if the message. content material dot starts offevolved with and we are going. to say. dollar greenback personal. like that if this is so without a doubt . you can also say if message content material. equals personal perhaps we'll do. that so let's see if message content. equals and now we will say dollar. confidential if this is so we're going. to assert look ahead to. message dot no longer channel message dot. writer. so the one who despatched the message to us is. going to get a dm. now we'll say hello.<br/>In confidential brother. like that dm so if we run this today. and we visit the server we will. see that if i go to the bot commands for. instance and i say. dollar exclusive. i'm going to get a message here from the. academic bot and it says. good day in personal brother as you will see that. it really works. so i'll delete that so that. people don't. start spamming this command right here and now. we're going to add. an additional function as an instance let's. say we don't just desire to react. to messages but we want to proactively. put up into a channel. so let's say on connect we want to say. into aa specific channel i am here or. something like that so we go to the. channel. we right click the channel uh for. example bot instructions. and we'll copy the id so we. must have an identification. uh that the server can confer with when. sending a message so we're going to say.<br/>On connect. and we'll say channel the. channel that we're going to publish. in is going to be purchaser.get. channel and we're going to paste the id. of the channel. and we will say wait for. channel.despatched. um i do not know what do you want to say. just attach it to. bot commands . so if we now run this and the bot. connects to the server we'll. see within the bot commands just related. to bot commands. o.k. with a purpose to delete that aas well. and one thing more that i wanted to show. you here. is sure if we have new participants joining i. imply that's variety of annoying in case you do. it but. to be able to do it what you can do is. you may say okay. async def on member become a member of. so that is the function that triggers. whilst new participants become a member of. on member join i'll pass the. member that joins. and naturally we need a consumer occasion.<br/>purchaser.event and if the member joins we. can just say okay wait for. member dot. create cree 8. dm like that and we'll say. a weight member dot. dm channel. dot send as you will see right here car. of completion from tab 9 again. and we're just going to move an f string. right here welcome. member or welcome. to the server member. now we're unlikely so that it will see. that except i create a moment account. and become a member of my. server but when i run the server right this moment. or if i run the bot as we speak and. someone connects which happens particularly. routinely as you can find here. last one turned into 4 mins in the past you can. see that just about. all every 5 minutes every 10 mins. somebody joins if i just retain that. bot jogging and someone connects to the. server they'll get. this message immediately now i'm able to run. this and you're going to see.<br/>Uh i mean we're not likely to determine. whatever actually but if someone. joins the server they're going to get. this message lower back we bought the bot. commands here we will delete that. and if now somebody joins we will. get or they're going to get a dm message. now of course we can also. just post uh into a channel in general. and say ok this individual joined but we. have the welcome messages right here anyways. but this is also anything you can do. you may await new members. joining the server now we will do a lot. of different things with we. can automate kicking we can automate. banning we can dm persons we are able to difference. server settings. uh we are able to car mute we are able to auto. delete messages and so on yet those are. noticeably stepped forward things and especially. when we begin with discord bots. as we do during this video i think it's the.<br/>best thing to just discuss. the on message operate so to just. ship messages to the bot or to a channel. that the bot sees and when this message. is distributed and the bot recognizes a certain. pattern it's going to act on it. so what we will do now for this. video is we'll create an easy. uh inventory question you could say so we are. going to. just ask for some financial prices for. some . uh closing costs on specific shares and. we will get an answer from the. bot. and for this we're going to desire the. pandas data reader so we're going to say. import pandas data reader. as internet and for thiis of course we have to. correctly. set up it first so we have to open up. the command line and we need to say pip. set up pandas dash information reader now this. is important because we're actually. uploading pandas underscore data reader.<br/>but we need to set up pandas sprint. data reader in my case i have already got. this. so i am unlikely to download anything. right here. but when you don't have it you need to. set up it first. and now we'll just add an. usual operate which we're going to. call. get get stock cost. we'll pass a ticker symbol. and iit's aactually quite simple we're. simply going to assert data equals. cyber web dot data body in fact data reader. sorry information superhighway. information reader and we will circulate. the ticker symbol we'll pass. the yahoo finance api. and iin order to get the cost we are simply. going to get. dataa close. i lock unfavorable one it is how we get. the last significance so it is just loading. the inventory price. off a undeniable ticker image and after we. have that we are able to cross forward and print it. now how. are we going to try this we are simply going.<br/>to say ok. if the message dot content. not equals but starts with after which. we're going to say. um stock or. inventory price like that. then if this is so we'll. say. get inventory price or in fact we're going. to do it like that we're going to say. if message.content. dot split on the spaces. if the size of that is strictly. then we will do something. due to the fact you do not need to have whatever. yet. inventory price after which the ticker image . so we'll say okay if. the length of the the person phrases. is 2 then we'll say okay get. stock cost and we'll circulate. message. dot content material dot break up. as you will discover exceptional audit crowning glory. returned uh we'll take. index 1 and we're going to get the stock. price. and we will say price equals that. and then we are just going to claim.<br/><a href="">welcome discord bot</a> and we'll put up into. a similar channel so watch for. autocompletion right here. again. and we will say the inventory price. so we'll say stock. price one stock price. off and then just. uh in fact perhaps let's just pass ahead. and extract it so we're going to say. ticker equals message. dot content material dot cut up. like that. after which we are simply going to exchange. this with ticker. and then we can just say inventory cost of. ticker. is price. so of course we haven't any exception. dealing with right here we're not going to. have this bot going for walks in production all. the time so it is not that significant we. ought to correctly manage ok. what if i say inventory price and something. else and then a ticker or no longer a ticker. what if the ticker can't be found and. so on but we are simply going to ignore. that we are simply going to.<br/>Use this uh the proper manner uh let me. just see back if thiis works okay if. message content material starts offevolved with inventory price. certain ok. then we'd like two precisely two words okay. if that is the case we are saying the ticker is. the second one notice. uh the cost is get inventory cost of that. ticker and then we'll send into. that channel inventory cost of ticker is. priced that ought to paintings. so let's see if that works we are going. to run this. nothing occurs why does nothing occur. uh why does nothing occur. there you pass it was line 37. on connect okay. in fact we don't desire that anymore. let's see if it works now. there you go bot connected to the server. and now we're going to go to bot. commands and we will say. inventory price aapl. and you'll discover stock cost of aapl is. it is today. then we'll say inventory price facebook.<br/>And you will find stock price of facebook is. whatever it is. and we're going to say inventory cost tsla. for tesla. and as you will see it works it should. also work in the dms. let's examine if it does stock. price uh gooldman sachs gs. and it additionally works here so this is one. thing that you'll do of course you could. do. every kind of various things you could. get random fees you could. um i don't know you can you may just. play range guessing video game with the intention to. you may work together with that bot through. the on message operate this is quite. cool. you can also do a little loops here you could. also you know add. or ask for added enter and so on so. this is actually relatively cool and it is. what you would need to mess around with. while starting to find out about discord. bots because you don't need to care. about aall these fancy matters which you.<br/>

This user hasn't created any releases yet. Find more releases from other users: