About weekliver1

Description

Easy Discord Bot in Python
what is going on men welcome back in. ultra-modern video we'll learn how. to build a discord bot with python so. allow us to get right into it. now earlier than we get into the particular. academic i want to mention that this. video is backed through tab9 and as necessarily. do not skip that aspect because it's going. to be fascinating to you i do not. suggest anything i do not advertise. whatever that i do not believe is. fascinating and useful. and that i do not use myself in order you. can see down right here once I move the window. right here. once I flow my digicam you will discover i am. using tab 9 i am correctly using it. in most of my tutorials i believe in all. the hot tutorials i am using tab 9. even though not every video is subsidized. and tab 9 is an car completion . engine on the way to visit the website here. and you will find that. tab 9 code quicker with ai vehicle.
Completions. tab 9 supports all contemporary languages java. javascript. python c sharp c plus rost cross and a lot of. extra. and iit's also supported in all uh. important. editors you can find uvs code you will discover. intellij pass. land you will see that pycharm you can find. atom emacs sublime even. whim which makes my heart beat quicker uh. and tab 9 has a loose always plan so. you could go to pricing right here. and you can find the tab nine is. totally loose you may pay. 0 money a month without any limits. so you do not have. like only a free trial you've the unfastened. tab 9 edition you do not need to pay. whatever. however if you have an interest in more. automobile completions in higher automobile. completions. gpu powered cloud automobile completions you. can investigate the professional version to boot. and also it's very important to know. that tab 9 takes. privacy very heavily so they don't um.
just take your cloud information and do some. nonsense with that they don't sell it to. anybody they treat it with appreciate. and they only use it to coach the car. final touch mannequin and they don't do. anything stupid with that so you could. read. about their code privacy on tab9.com. codeprivacy. and for this video above all you may. find aa coupon code within the link in. description down under. and you may get 50 off to your first. 12 months of. tab 9 professional if you are drawn to that. or you may also stick with the. unfastened version yet i'm able to highly recommend. you to apply tab 9. on your programming to speed up your. coding and to make it. just aa better event overall all. correct so we're going to construct a discord. bot in python but before we can begin. with the coding we have to go to. discord.com and we have to installed the. software.
And the bot itself so you visit. discord.com and you create an account or. you log into your account. after which you navigate to slash. builders. this opens up the discord developer. portal for you and here you may go to. applications. and right here we will create a brand new application. or a brand new bot you may say. uh or we're going to create a bot within. of the application. now something you need to know is. that when you have a bot and you desire this. bot to hitch a server you should have. the manage server permissions. you cannot simply create a bot and join. random servers with it and do stuff. on them you must create you should. both be the admin of the server otherwise you. must ship the hyperlink to an admin of the. server or you have to have the manage. server permissions. so just keep that in intellect now what we are. going to do is we're going to click on.
On new program and we'll. name this tutorialbot. and we're going to create this here so. what we then must do is you can you. delivers. an outline you supplies an icon or. whatever like that uh. what is crucial is that we visit the. bot tab here at the left and we create. at uh or we click upload bot so we click on. on upload bot. this isn't reversible certain do it and now. we have academic bot. now one thing that you need uh or that. you are going to want in a while within the . programming method. is the token so i am not going to click on. to reveal the token here but if you wish. uh to know your token you just copy it. and you can paste it into your script or. into the environments we're going to. discuss that in a moment. yet that is what you're going to ought to. connect with the bot. now if for some purpose someone reveals.
Your token. you exhibit it or you show it to somebody. you could simply regenerate the token and. the historical token is not. not valid so you may in basic terms use the. new token. yet that is what you're going to have to. connect to the bot so then we go to. oh auth 2 and here we will set the. permissions or first off we have to. investigate. bot right here and down here we will check the. permissions so we've. for example we'd like the bot to have the ability. to send messages to cope messages. mom bot discord need to do in fact. so i do not know what we're going to do. we will say view channels okay. we don't desire it to cope an excessive amount of uh. i definitely don't want to present it the. administrator view audit log. perhaps now not manage server deal with roles no. event channels no kick participants definite band. participants certain. uh change nicknames deal with nicknames.
Uh match discord girlfriend bot going to send. manage messages. probably not embed links connect documents. we are not likely to utilize these kinds of. incidentally i am simply uh. checking them right here uh study message. history certain. point out all people certain add reactions use. scale down commands. and that is correctly it so. once we've that we will have. this link here and that is what we are. going to want. to let the the bot join the server so. what we do this is we reproduction that. we paste that into a new tab and then. what you see is. um i will select a server to let this. bot uh join the server and of course i. can simply select my own server here. so neural 9 community and ii could uh. simply. click keep right here now i'm uncertain. if we're unlikely to get a group of. messages immediately so. perhaps we should flip off the sound here. um. yet yeah there you cross you confirm what.
This spot is allowed to do after which 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 uncertain if it's. part of the discord. so if i visit discord cutting-edge within the . browser open discord i should be capable of. see the bot. maybe i'm not sure. um is it there allow me see. welcome lounge tutorial bot simply confirmed. up. so as you'll discover we've the bot now on. the server and now we will get into the. coding process. very well so we are now going to begin. with the coding but first of all we need. to put in the discord.py module and for. this we'll open up a command. line. and we're going to say pip set up. discord dot py so it's what we are. going to apply in order to application the bot. and also i'll use one other. library that you don't have to apply if.
You don't want to. uh and it's the python sprint dot n. library and this is crucial due to the fact i. have an environment file a dot. n dossier as you'll find here at the left. and ii use this dossier as a way to load. environment variables. into the script now this sounds maybe. extra difficult than it's it is only a. dossier that has the token. so you do not have to use that library. you can just take the token and placed it. right into a string and that is also excellent yet. without a doubt then people are going to. see the token when they open up the. script and since i am recording i do not. want this to happen. uh yet besides that you also can simply. create an ordinary file. and read the token from there i'm going. to apply the dot n. module due to the fact that is slightly extra. expert and for this i needed to do. pip install sprint capital u. i do not know why i had to do it maybe.
Because welcomer bot discord works for you devoid of the sprint. u uh but then just. python dash dot n like that. that is what we have to do and when we. have that we will begin with importing. the modules we will import. discord and we will import os for. operating method and we will. import. from dot and we'll import. load dot inf and that's it for the. starting so what we're going to do. first is we're going to create a consumer. we're going to say purchaser. equals discord dot client. after which we'll say load dot n. now what this does. is it loads the content of the dot and. file. what is inside of the dot n file i am. going to put in writing it down right here i am not. going to open it because then you're. going to see my token obviously. yet the 1st line is just hashtag dot n. and the second line is token.
Equals and then i have the token here. whatever the token is so you could. structure your file like that aas well if. you need to it is going to be loaded into. the script and then we will simply say. token equals os dot get n. which stands for get environment and. then just token. and by way of doing that you've an. environment variable yet we don't have. it. uh in the technique you simply have it in in. the. uh specific script right here. so once we have that i am unlikely to. to do any functionality but i'm just. going to put the bot online so we are. going to assert purchaser. dot run and that should placed. the bot on line now if we visit discord. server you're going to see that the bot. is not. on line we have three bots on line yet. academic bot is not right here. uh if we scroll down right here. to t a variety of members already. there you pass academic bot.
Uh you're going to see that the bot or. you saw that the bot is offline so if we. now run the script you are going to see. that the bot goes to be on-line and. it's going to appear right here. among the bots and ii'm going to show you. that it is the case we are just going. to run this. uh we've a problem without a doubt we want. to flow a token here. like that and once we have done that. uh you can find that nothing occurs. that's what should occur due to the fact. we are not doing whatever. but when we visit the server you will discover. educational bot is now online. and expecting pursuits and you know it. does not have any function but if we. have a few pursuits that we're looking forward to. and a few function we are able to uh then. do that and it's what we're going to. do in your next step very well so let's get. all started with a very simple function.
we'll create a function. called onconnect and iit's going to. set off anytime the bot connects to a. server. so we're going to say def on attach. and right here we will say ok when. this happens just. write anything like bot connected. to the server now this function would not. paintings similar to that we need to add two. things firstly we have to upload a. decorator. which is going to claim that it is a. client event is an. occasion uh that discord has to listen to. or that the bot has to hear and. additionally this is going to be an. asynchronous operate for these of you. who do not know what async is. inspect my video on async watch for on. this channel on asynchronous. python programming if you are fascinated. in that yet you don't really want to. understand what's occurring at the back of the. scenes you only want to know which you.
have got to upload the async keyword. and you should add it into the. starting so async. def on connect and above that we're. going to feature the decorator. purchaser.occasion now if i simply run this. script we will see. that uh the bot is going to claim bot. linked to the server. in the moment it connects to the server. now we may also do other things we will. additionally say okay. uh broadcast to the complete server in a. certain channel. that you're now here if you want to yet. without a doubt this would be. uh unnecessary right now we just favor. this to have. we just want this to be locked onto the. console and. now we will have an extra operate. which is going to be on message and with. that function you are able to do a lot of. different things. so we're going to say client.event back. and we're going to say async def. on message now every time a message and.
we will have a message here as a. parameter. each time a message is distributed in a channel. or. in a dm chat with the bot um. this bot is going to react to this. message and how it reacts to this. message depends upon us. so for example we can say okay first of. all if the message. author so if message.author equals. client dot person if that is so. then do not do anything since you are. the person who sends the message so just. go back and do not do anything. in any other case we will do all sorts of. conditions right here which will say for. instance okay if the message. dot content starts with. extremely good autocompletion here from tab 9 . if the message content material starts with. and then we can say i don't know uh. whats up as an example dollar howdy. if that is the case then we can do. something. we are able to say okay get the channel uh. which this message become sent into so we.
Can say okay. get the channel and then send a message. into that channel so that it will say watch for. message dot channel so we get the. channel from the message. and we'll ship to that channel. uh i don't know. hi there world we are. presently recording a video due to the fact i'm. going to run this right here. at the server in a moment after which. you're going to see. that the bot correctly responds to that. so now if i run this here. you could firstly see bot linked. to the server if we now visit the server. and i visit the dms. of the bot so i visit educational bot and i. say ok. first of all i can simply say howdy so it. would not react to each of the messages hi there. not anything happens okay. dollar hiya and then you will discover howdy. international we are currently. recording a video and now i will. do that in the overall here. and we're going to get a gaggle of.
exclusive uh reactions i suppose. so if i say dollar howdy you are going to. see hello international we're presently. recording a video so the bot can respond. to the on message event. into the on connection event that's. just one instance of what we can do with. this type of bot. okay so next let's upload a dm. functionality let's say we have a branch. right here that says ok if the message. content dot starts with and we are going. to assert. dollar dollar exclusive. like that if that is the case without a doubt . you can also say if message content material. equals personal perhaps we will do. that so let's examine if message content material. equals and now we'll say dollar. confidential if that is the case we are going. to claim look forward to. message dot not channel message dot. author. so the one who sent the message to us is. going to get a dm. now we're going to say hi there.
In confidential brother. like that dm so if we run this as we speak. and we visit the server we're going to. see that if i go to the bot instructions for. example and i say. dollar personal. i will get a message right here from the. academic bot and it says. howdy in private brother as you'll find. it really works. so i'll delete that so that. people don't. start spamming this command right here and now. we will upload. a further functionality for instance let's. say we do not simply desire to react. to messages but we wish to proactively. publish into a channel. so as an instance on attach we want to say. into aa specific channel i'm right here or. anything like that so we go to the. channel. we correct click the channel uh for. example bot commands. and we're going to copy the identification so we. must have an identification. uh that the server can seek advice from when. sending a message so we'll say.

On attach. and we'll say channel the. channel that we'll post. in is going to be purchaser.get. channel and we're going to paste the id. of the channel. and we will say watch for. channel.despatched. um i do not know what do you want to say. simply connect it to. bot instructions whatever. so if we now run this and the bot. connects to the server we will. see within the bot instructions simply related. to bot instructions. o.k. with a purpose to delete that aas well. and one other thing that i desired to show. you right here. is definite if we have new participants becoming a member of i. mean that's variety of exasperating in case you do. it yet. to be able to do it what you are able to do is. you may say okay. async def on member join. so that is the operate that triggers. while new participants join. on member join i'll pass the. member that joins. and naturally we'd like a consumer event.
purchaser.event and if the member joins we. can just say okay watch for. member dot. create cree 8. dm like that and we'll say. a weight member dot. dm channel. dot ship as you can see right here vehicle. of entirety from tab 9 returned. and we are simply going to flow an f string. here welcome. member or welcome. to the server member. now we are unlikely with the intention to see. that except i create a second account. and become a member of my. server but when i run the server today. or if i run the bot right this moment and. someone connects which happens fairly. frequently as you'll find right here. last one was four mins in the past you may. see that nearly. all every 5 minutes each 10 mins. somebody joins if i just keep that. bot going for walks and someone connects to the. server they'll get. this message immediately now i will run. this and you're going to work out.
Uh i imply we are not likely to work out. anything actually but if someone. joins the server they will get. this message lower back we obtained the bot. instructions here we will delete that. and if now someone joins we're going to. get or they're going to get a dm message. now without a doubt we can also. just post uh right into a channel commonly. and say ok this individual joined yet we. have the welcome messages here anyhow. yet this is also something you are able to do. you could watch for new contributors. joining the server now we are able to do plenty. of different things with discord.py we. can automate kicking we are able to automate. banning we will dm persons we can change. server settings. uh we can automobile mute we will automobile. delete messages and so on yet those are. highly stepped forward matters and especially. when we start off with discord bots. as we do in this video i believe it's the.
best thing to only talk about. the on message function so to only. send messages to the bot or to a channel. that the bot sees and when this message. is distributed and the bot recognizes a definite. pattern it's going to act on it. so what we're going to do now for this. video is we'll create an easy. uh inventory query you could say so we are. going to. just ask for some financial prices for. a few. uh remaining costs on specific shares and. we're going to get an answer from the. bot. and for this we're going to desire the. pandas data reader so we'll say. import pandas information reader. as information superhighway and for thiis of course we need to. actually. install it first so we need to open up. the command line and we have to say pip. set up pandas sprint data reader now this. is important because we're in fact. uploading pandas underscore information reader.
Yet we need to set up pandas sprint. information reader in my case i already have. this. so i'm not likely to down load anything. right here. but when you do not have it you ought to. set up it first. and now we're going to just add an. usual function which we'll. name. get get stock price. we'll pass a ticker image . and iit's aactually quite simple we are. simply going to assert information equals. web dot information frame in fact information reader. sorry internet. data reader and we will pass. the ticker image we will pass. the yahoo finance api. and iin order to get the price we are just. going to get. dataa close. i lock negative one that is how we get. the last importance so it truly is simply loading. the stock cost. off a certain ticker symbol and after we. have that we are able to go forward and print it. now how. are we going to do this we are just going.
to claim ok. if the message dot content material. now not equals yet starts with after which. we'll say. um inventory or. inventory cost like that. then if this is the case we will. say. get inventory cost or in fact we are going. to do it like that we'll say. if message.content. dot break up on the spaces. if the length of that is exactly. two then we'll do anything. because you don't need to have whatever. but. inventory price after which the ticker symbol. so we're going to say ok if. the size of the the individual words. is 2 then we're going to say ok get. stock cost and we will move. message. dot content dot cut up. as you will find exquisite audit of entirety. returned uh we're going to take. index 1 and we will get the stock. cost. and we will say cost equals that. and then we are simply going to claim.
A weight and we'll put up into. an identical channel so await. message.channel.send autocompletion here. again. and we're going to say the stock cost. so we'll say stock. cost one inventory cost. off after which just. uh actually perhaps let's simply pass ahead. and extract it so we will say. ticker equals message. dot content dot cut up. like that. after which we're just going to exchange. this with ticker. after which we are able to just say inventory price of. ticker. is cost. so of course we haven't any exception. handling right here we are not going to. have this bot jogging in production all. the time so it's not that important we. ought to in fact manage okay. what if i say stock price and something. else and then a ticker or not a ticker. what if the ticker can't be found and. so on yet we are simply going to disregard. that we're just going to.
Use this uh the correct manner uh let me. simply see again if thiis works okay if. message content material starts with stock cost. certain okay. then we'd like two precisely two phrases okay. if that is the case we say the ticker is. the second one word. uh the price is get inventory cost of that. ticker and then we will send into. that channel inventory price of ticker is. priced that ought to paintings. so let's examine if that works we are going. to run this. not anything happens why does nothing take place. uh why does nothing happen. there you pass it turned into line 37. on connect ok. in fact we don't desire that anymore. let's examine if it works now. there you pass bot linked to the server. and now we're going to visit bot. instructions and we're going to say. stock price aapl. and you will see that inventory cost of aapl is. whatever it's right now. then we'll say inventory price facebook.
And you will discover inventory price of fb is. whatever it is. and we will say inventory cost tsla. for tesla. and as you'll discover it works it may. additionally paintings in the dms. let's have a look at if it does inventory. cost uh gooldman sachs gs. and it additionally works right here so it truly is one. element that you'll be able to do without a doubt you could. do. all kinds of different things you can. get random rates you can. um i don't know you can you can simply. play wide variety guessing game with the intention to. you can interact with that bot by means of. the on message operate this is quite. cool. you can also do some loops here you may. also you know upload. or ask for further input and so on so. this is truly relatively cool and it truly is. what you would need to play around with. while commencing to study discord. bots due to the fact you do not need to care. about aall these fancy things that you.

Listing

Sorry, no listings were found.