How to code a basic discord bot.

Notchmods
4 min readJul 10, 2021

--

This article will be about coding a basic discord bot with python and repl.it.

Firstly, you need to open repl.itand register your account. Once you’ve registered yourself then click on “New repl” button at the left of the screen. For the language select python then name your repl whatever you want. Once you have named your bot then click “Create repl” Coding the bot: Once your project has loaded then start by importing everything that you need for this basic project.

Now that you have imported everything that you need for this project then you need to fetch the client.

Now that you have fetched your client then you needed to activate a signal that shows that it has been activated. async def on_ready() function basically executes any function that’s under it once the bot has been activated. print(“We’ve logged in as {0.user}”.format(client)) #{0} is the bot name That basically says that your bot has been activated but alternatively you can use.

you can use both either way. Next thing you’ve to do is to get the bot to respond to what you have said.

States that this function is an event. Anything under there is basically an event.

Add the message detection functictionand checks if whether the trigger phrase is said by the user, this is used to prevent the bot from replying to itself.

The code above here is the trigger phrase. Any word that starts with “Hi” will be replied with a “Hello”. Extra features: Here are a few extra features that you could add: You can group several trigger phrases into an array and that’ll make things simpler instead of a continuous if statement.

If any of the words in the greetings has been mentioned by the user then the bot will reply with a “Hello”. Same as how you can group several trigger phrases into an array. You can also group reply into an array and make the bot reply with either one of those words or phrases in the array. We will make a random colour generator for this example:

You will need to make an array containing all the random color you want. Add some emojis on it if you like. Then once you’ve done that,you’ll do the same as the responding code. You will need to add the @client.event , after you have added that you will need to add the function and below the function, you will need to check whether the message is sent by anyone other than itself so that it would not reply to itself. Then do the same with the if statement add the trigger phrase, but you could also add a wide variety of trigger phrases. Another features that you can add is to show the bot activity. Below the async def on_ready(): add await client.change_presence(activity=discord.Game(‘GTA VI’))

client.run(os.getenv(“Token”)). This final code runs the bot, but to run it you need to get thetokens which will lead to the next sectionCreating thebot:With the extra featuresout of the way now we are going to create the discord bot. Go to Discord Developers Portalthen click on the “New

Application” button, type the name of your applications. Then click on the “Bot” button 3rdbutton on the left side of the screen. Click on “Add bot”, click the “OAuth2” button 2ndon the left side of the screen. Scroll down and until you have seen the “OAuth2 URL Generator”. Find the “bot” checkbox(4thon the middle column) and click it . Scroll down to the Bot permissions and click on any permissions you want the bot to have then copy the link and paste it. Done you’ve a bot in your server congrats you’ve created a basic discord bot.

Code structure:

--

--

Notchmods
Notchmods

Written by Notchmods

Notchmods is a game studio that published several notable titles and create several softwares. Notchmods has a YouTube channel with over 300 subscribers.

No responses yet