Skip to main content

How to send and receive discord messages with your bot in 2023.

Hello everyone. Welcome back to the Blog of Chamodh today I will show you how to send and receive discord messages with your bot. Before getting started, I remind you that this is the second episode of the series How to make Discord bot in 2023 and if you haven't read the previous one, please read that first as I explain how to create your bot application and add it to a server there.


READ: https://blogofchamodh.blogspot.com/2023/02/how-to-make-your-own-discord-bot-2023.html

We don't gonna use virtual environments, IDEs, or any other utility because we can use Replit for our coding purposes. 

Let's Get Started.


STEP1: Set up Replit

It is really simple. Just go to https://replit.com/~ and sign up if you have never used its service before and create a new Repl. I don't hope to guide you in this as this is an intermediate tutorial.

Make sure to select Python as you create the new Repl because we are going to use Python for our back end.


STEP 2: Get your token.

To run our bot we have to use a token. You can find your token here: https://discord.com/developers/applications/ and select the application 

Then select the Bot Menu from the left sidebar and then you can see your token. If you cannot see it you will be able to see a Reset Token Button, after clicking it, you will be able to see your token.







Then copy the token. To hide my token I am going to create a new file called credintials.py and create a string variable called token so that I can import it later.


STEP 3: Install Dependencies.

To run our discord bot, we want some packages. We only need discord.py to get started and we may need some other packages to add extra functionalities to our bot.

To install discord.py run this command in the Repl's Shell.

~/GlisteningMemorableLanguage$ pip install discord.py


STEP 4: Let's Code

To handle the events we use the following code chunk. As I mentioned before I don't expect to explain the code line by line as this is not a beginner-level tutorial. If you find something difficult to understand, please let me know in the comment section.


To receive and send messages when a user sends a message to the server, we use the on_message function.

You can simply add the method to the MyClient as follows.



When we set up our on_meesage method, we have to check whether the message is sent by a user other than our bot. Unless it becomes an infinite loop of messages. To do this we can either check for the message's author or tell our bot to respond to messages which start with a specific character like $ or!


If we choose the first option, the on_message method will be the above code which checks if the message's author is not the bot. If so, it will send a reply



In the above example, to send a reply to the channel from
where the message came.


If we go with the second option we can use the startswith()
function
which returns true if a string starts with a
specific character. The following code will give you a better idea.
When you use the second method don't forget not to send a message
starting with the character you defined from the bot, as it will result in
 an infinite loop.





Comments

Trending Now

Let's talk about cryptography.

Have you ever heard of cryptography?  People write their diaries every day. For many of them, their diary is one of their best friends to whom they tell their every secret. What happens when someone else finds such a diary? That would freak out the dairy's owner for sure. Also, it might put their lives in danger. Wise men always hide their diaries so no one else finds it.  But legends hide their message so that only they can read what they wrote. So even if the diary is found by someone else, they cannot know the secrets. for example, I wrote this in my diary today.   P dyval h isvn wvza hivba jyfwavnyhwof avkhf Can you understand it? This is just a very primitive level of cryptography yet powerful enough to hide what I wrote from 90% of people. I will discuss this type of cryptography in the next blog post. The human used cryptography from the very beginning to share their messages in secret and conceal their inventions. As you may know, many people tried to find a recip...

How to make a your own Discord bot with Python in 2023. #01

I'm sure that you have heard of discord bots way before reading this article. But have you ever tried to make your own bot with Python? If not, this is the time. Let's get started. Over time tons of discord bot tutorials have been published, but if you follow them they don't help you to build a functional bot because many of the packages used in them are updated and no longer support older syntaxes. Here is the full guide to creating your own Discord Bot in 2023 with Python. STEP 1: Create a Discord account First of all, we have to set up a developer account to create the bot. Actually, you can use your ordinary discord account to access the discord developer portal, but it is highly recommended to create a separate account for development purposes. You can create a new discord account with this link:  https://discord.com/register STEP 2: Create a new Application After creating the discord account, you have to go to the discord developer portal and sign in with your newly-c...