Hello Everyone
In the previous posts, we have learned some basics of discord.js. We created a simple discord bot that responded whenever a user messages it. It was a basic bot that helps us learn how to interact with Discord using JavaScript. If you have noticed some of the discord bots response only when a user message with a specific command like ? /! Etc. This is called the prefix. This helps us avoid messages from other users. Like if anyone needs the bot they should send a message to bot or in the specific discord channel with the particular prefix in order to get bot attention. The bot will watch all the messages and if the message start with the provided prefix it will respond. So let's start our tutorial.
- I have the code from our previous post. If you haven't read the previous post I recommend reading that post first. This script will only keep our bot online.
The second file is config.json. This only contain the bot token and prefix.
Whenever someone message to the bot/channel this function will be called.
let me just run the bot and resend the message to the user just for checking purpose.
As you can see the bot is responding.
Now we need to check bot is check whether the message is sent by the user or a bot. For that, we will check the message with if statement.
if the author is a bot then ignore it.
and only reply when the user message the word help otherwise ignore all the messages. lets re-run the code.
As you can see the bot is ignoring all other messages even when the first letter of the word help is small.
- Now if the user message with ? (which is our prefix) then reply otherwise ignore.
Now to get the right message we need to remove the prefix from the message.
I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
Congratulations @pakgamer! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :
You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Support the HiveBuzz project. Vote for our proposal!
This is an interesting post. I really loved how you explained the whole process of how to add custom prefix to a discord bot.
Thanks @pakgamer for sharing this post.
Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.
Join our Community on Hive and Chat with us on Discord.
[Gitplait-Team]