The Discord platform does nearly everything you might need to communicate with friends , family and coworkers , admit voice calls , instantaneous electronic messaging , chatrooms , and media communion . So it ’s no surprisal that the service has explode in popularity . But most new user do n’t realise that Discord bots can make the platform even more hefty — and that they ’re relatively well-heeled to set up even if you ’re not ( already ) a computer programmer .
If you ’ve used Discord at all you ’ve in all likelihood encountered a Discord bot . mayhap one of them welcomed you when you joined a new server , or booted a foolish drug user who violated the rule . Sometimes they ’re set to auto - reply to sure Word with jokes and memes . And certain brawny variants of Discord bots keep drug user in a given schmoose up - to - date on everything from covid-19 vaccinum accessibility to whether the local game storage has got the newest cabinet in stock .
bot can vary loosely in mapping and complexness , and while you could go get the codification base for one , you might require to make your own . Learning how to create one from scratch is not only belong to see you’re able to custom-make it to your own pauperization , but can assist you understand the inner workings of a bot and , importantly , how to troubleshoot it when things go cockeyed .

Photo: STRF/STAR MAX/IPx (AP)
So today we ’re break to be using Python , which is known for being a jolly good first terminology for anyone looking to learn to code , thanks to its relatively easy - to - read codification and built - in , operable formatting ( indenting is not only helpful for reading it ; it ’s necessary ! ) . Here ’s what you will involve to get started :
A proper data processor .
A Python install .

Screenshot: Wes Davis/Gizmodo
An Integrated Development Environment ( IDE)—this is where you ’ll be typing out your code .
A Discord news report and server .
Coffee ( optional , extremely bucked up ) .

Screenshot: Wes Davis/Gizmodo
Creating the Bot Application
I ’m engage under the assumption that if you ’re here assay to figure this out , then you already have an report and are hunt a Discord server . If not , go sign of the zodiac upandmake a waiter . Once you ’re done with that , you ’ll require to head to theDeveloper Portal . On the left , click the Applications chit , then the New software push on the top rightfield .
Here , you ’re not in reality naming the bot , per se — this is the lotion , which stop the bot . That said , there ’s no reason the covering and the bot ca n’t share the same name . Once you ’ve created the software , tap it , then tap the Bot link in the sidebar — this is where you could expend Discord ’s Build - A - Bot process to birth the lil automaton .
Once you ’ve create the bot , you should get a message aver , “ A wild bot has appear ! ” Congratulations ! It ’s a bot ! I ’m gallant of you . Name your bot , and below the Username field , you ’ll see a token department , with your relic shroud behind a link .

Screenshot: Wes Davis/Gizmodo
A Note on Bot Tokens
It is all important that you keep this nominal secret and keep it secure . The token is a unique identifier peculiarly for your bot , and should anyone else issue forth by it , they can take control of your bot . It ’s also how you connect the code you publish to the bot . It ’s such a sensitive art object of info that it ’s not even that skillful an mind that you keep it directly in your codification , but we ’ll get to that after .
For now , open a plain text editor in chief and glue the token there . economise it to whatever folder you project to keep any bot - related materials in , preferably with a name like token.txt to alleviate any confusion down the route .
Giving Your Bot Permissions
Next , you ’ll need to give your bot permissions . With your bot still open , come home OAuth2 in the sidebar , and under the Scopes division on this projection screen , tick the box labeled bot and then scroll down to Bot Permissions and solicit it up with some abilities . The boxes you check are up to you , and the I I selected in the below screenshot are not in any way a prescription :
While you ’re at it , tap bot in the sidebar again and scroll down to Privileged Gateway intention . If you want your bot to respond in any way to the presence of users or employ the member tilt , you ’ll need to enable these :
Assigning the Bot to Your Server
After you ’ve given the appropriate permission , you ’ll require to copy the connection beneath the Scopes Zen , open a new web web browser tab , and glue it in the destination / search saloon , then strike Enter . This will bring you to this screen , where you ’ll choose the waiter ( or what is sometimes referred to in the official Python Discord bot API as a “ lodge ” ) into which to drop your bot . Click Continue and you ’ll be brought to a cover to confirm the bot ’s permission for that server :
Side promissory note : Your bot ’s permissions are waiter - specific , so it follows that if you desire to view the permissions your bot has been given , you have to do it from that specific server . If you just want to brush up them , do the followers : decently click your host in the top left wing of the Discord windowpane , click Server configurations , then Integrations . Here , you ’ll see your bot under the heading Bots and Apps . press it and you ’ll see all the permit you ’ve allow your bot . If you desire to change them , you ’ll demand to go a unlike way , clicking part instead of Integrations and then dawn on your bot , and go to the Permissions tab on the next cover .
Breathing Life into Your Bot
To continue , you ’ll want to make indisputable you have Python instal . There are many guides on doing this — I likethis one . Once you ’re set up with Python , you ’ll need to get the Discord module . This contains all of the Discord - specific commands you ’ll need . If you ’re ferment in Windows , open Command Prompt using thesteps outlined hereand type the following , hit Enter when done :
py -3 -m pip install -U discord.py
If you ’re onmacOSorLinux , heart-to-heart Terminal and type :

Screenshot: Wes Davis/Gizmodo
python3 -m pip instal -U discord.py
With that footmark completed , go ahead and open up whatever IDE you ’ve chosen to use . I likeSublime , personally , but you could use whatever you want . Heck , you may even use a plain text editor if you ’re feel spicy . With your editor program candid , make a new single file and typecast this on the first telephone circuit :
moment discord

Screenshot: Wes Davis/Gizmodo
This brings the Discord library into your code . Next , you involve a path to associate your bot with this codification . This means calling the token for the bot – but remember when I suppose it ’s a good idea not to store that token right away in your code , and to keep it in a .txt single file rather ? This next production line is how you get it from that .txt file :
TOKEN = open(“token.txt”,”r”).readline ( )
In this line , you ’re creating your first variable : a string of fiber you may type to reference a specific activity . Here , TOKEN is the variable quantity , the = sign say the figurer that it ’s about to construe that variable star as something , and the school text follow that is that something . Here , exposed ( ) severalise the computer it ’s going to open a single file , and inside the parentheses is a duad of arguments , the first of which identifies the file you want it to open ( if you did n’t call your data file token.txt then supercede token here with whatever you did name it ) , and the r tell apart it the style you want it open in . We only want to read the relic , so we use r to open it in Read modal value . The .readline ( ) bit evidence it which line to read . Since there ’s only one to read , you will the parentheses empty .

Our whole bot, in one small screenshot!Screenshot: Wes Davis
Discord Intents
Due to somewhat late changes in how Discord handles bot event subscriptions , you ’ll need to take some extra steps to enable your bot to respond to sure event , like a penis connexion , for illustration . Discord calls these special permissionsIntents , with some higher - horizontal surface ones like substance abuser - monitoring called Privileged Intents ( the toggles you may have flipped earlier in the process ) . In addition to enabling them during your bot ’s setup , you will also need to enable intents in your codification . First , if you just require to tend with the default event subscription , delineate Intents as the default :
intents = discord.Intents.default ( )
Then , you ’ll flip to True either of the extra intents you ’d like . Additionally , you may not want all of the default option , and may only want your bot to react to a limited number of event types . In this case , alternatively of the above , you might do the following :

intents.members = truthful
intents.messages = True
intents.guilds = lawful
![]()
The final dance step we ’ll take for this surgical incision is delineate your client and ensuring :
Client = discord . Client(intents = design )
Programming Actions
OK , now we ’re quick to start telling our bot what to do . First , it ’s helpful to sum in a print ( ) financial statement so that you have check that your computer software has successfully logged in . This musical composition of codification will print , or video display , a affirmation in your runtime environment ( terminal or otherwise ) confirm that your bot is connect and run away :
@client.event
async def on_ready ( ):

print(‘Logged in as { 0.user}’.format(client ) )
The reason we use async here is because Python by default is synchronic , meaning it runs a command and waits for it to discharge before move on . You do n’t of necessity want this when communicating with a server , because if the host gets stuck or something , then so does your program . If subsequent code is n’t dependent at all on that assembly line of code , why not let your computer programme just keep running its other code ? So you ’ll expend async ( short for asynchronous ) to enable your host to continue running your other code . When Discord answer , your code will pick up where it pull up stakes off and execute whatever statements came after the asynchronous handler you used ( which in this case is on_ready ( ) ) .
Next , allow ’s go over how to get your bot to respond to control . As a sampling , let ’s have the bot say hello to you . First , you need to call your content manager :

async def on_message(message ):
Then , to prevent your bot from responding to itself , have the codification return without activeness if the bot say anything :
if message.author = = client.user :

return
Finally , the mastery that prompts the bot ’s response :
if message.content.startswith(‘.hello ’ ):

expect message.channel.send(‘Hello ! ’ )
So the whole block , together , will look like this :
The loop-the-loop prevention codification here , of course of instruction , is n’t rigorously necessary ; the bot would n’t get stuck react to itself here anyway . However , as your code grows more complicated , you may find yourself accidentally programming a bot that could potentially trigger itself and end up in a oscillation of ego - triggering and responding — say if you make a random movie quote bot , and one of your gun trigger Son is really in a quote from whatever library of quote you grabbed .

This next bit of code earmark your bot to post out a welcome message . This is where the intents we enabled at the top of our codification is a necessity , as it requires the bot to monitor the penis list , which Discord consider a inside intent :
async def on_member_join(member ):
print(‘got a thing ’ )

channel = client.get_channel([Channel ID break Here ] )
await channel.send(‘Welcome to this channel ! ’ )
Some key things here :

That print financial statement is there for debugging purposes . While testing the welcome message , I had a dummy account joining and leave the Discord host , and was n’t able-bodied to actuate a welcome substance for a long meter . I put in the print program line to verify that async def on_member_join(member ): was in reality triggering the curriculum to run subsequent code . If it was , the mark statement would , in Terminal , say “ draw a affair . ” It was n’t , so I had to debug . The primal short letter of code ended up being alllll the way back at the top :
I needed this codification so that the bot ( Client ) would gain the permissions I gave it . You do that by put an disputation in the above parentheses that sets the bot ’s intents equal to those as delineate at the top of our code . I had of course leave behind this entire line of code out , so my bot was n’t actually using the spirit I ’d enabled for it .
To get the Channel ID : While you ’re in the distribution channel you ’d like the ID for , you ’ll want to look in your address bar , where the URL should be , followed by two long set of identification number , separated by a /. The second set is your channel ID , while the first is your server , or guild , ID . practice the Channel ID in the parameter above .
![]()
in conclusion , the await component of that set of code is necessary when you utilize async . If your bot is go to respond , it require some variety of recall social occasion so that it knows what to do when the server does ultimately respond ; await serves that function , in this case narrate the bot to send the substance , on the previously - delineate channel , “ Welcome to this groove ! ”
Let ’s take a look at the terminal code , with all the appropriate ( and very necessary ! ) formatting :
Keeping the Lights On
So , you ’re believably go to need your bot always run . There are a few slipway to accomplish this : For one , you could just run it from your primary data processor at rest home , but that requires you to keep the machine always on and always associate , and plausibly never really sleeping either ( you could technically let it slumber if your information processing system is configure for wake on LAN , but this feature can be troublesome and irregular ) .
A undecomposed choice could be to hunt the code on a Raspberry Pi or other dedicated low - major power waiter machine . Basically , you would escape your * .py program on the host , and get into all goes well , it will just operate for an undetermined amount of time . This is ok when you ’re get out . As your bot becomes more complex and — if you allow it — starts being used in other Discord waiter , you ’re probably live on to desire it lead as close to 24/7 as potential . For that , you ’ll want to see into third - company hosting services . There are plenty of free and paid options out there , andhere is a decent , lengthy guideto help you with that .
And that ’s it ! With this introductory set of commands , you now have a functional bot . Of course , there is so very much more you could automate with a bot , and I would advance you to prod around Stack Overflow and the discord.py API . bot can be as sinewy as you want , with automations that make your spirit as executive far easier , or they can just be fun trivial quote simple machine or diversions for a small Discord host apportion with friends . Whatever the case , it ’s a fun way to not only manage your Discord server , but also discover primal deterrent example about coding .

Alt - techDiscordJavaScript
Daily Newsletter
Get the dear tech , science , and cultivation news in your inbox day by day .
intelligence from the future , delivered to your nowadays .
You May Also Like




![]()