create account

How to add Custom prefix to your Discord Bot. Discordjs Tutorial by pakgamer

View this thread on: hive.blogpeakd.comecency.com
· @pakgamer ·
$9.60
How to add Custom prefix to your Discord Bot. Discordjs Tutorial
## <center> Hello Everyone</center>

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.

![image.png](https://images.hive.blog/DQmdEofMkxibg1wj9xPh2Ae1aixhWcZP8hL5PpDUbssUBpX/image.png)


* 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.

![image.png](https://images.hive.blog/DQmYZ7Ag62cSjUbaaytgQ8x8qcTHh9e2QcueQwonrmoSFnm/image.png)

The second file is config.json. This only contain the bot token and prefix.

![image.png](https://images.hive.blog/DQmWZczpnkYUj77KA9bGjFkyutFziKgG2ymBk8TkcRrMqoz/image.png)



![image.png](https://images.hive.blog/DQmVwsW9jAmr63Sa8qVhhzquntdsBWZZyctGmwA5JonNLN1/image.png)

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.

![image.png](https://images.hive.blog/DQmZxUD7ffPkb91nk839obzq1SfKYdqmMZ85E9Cv3v3JD2U/image.png)

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. 

![image.png](https://images.hive.blog/DQmPXShHv91t6Seb67F7kUz4cbhxmFDR3Yc8FmikE565VMz/image.png)
if the author is a bot then ignore it.


![image.png](https://images.hive.blog/DQmbSiRwvDnpUEEfhNhKBJ95Fawya1sqX6pLASLZiSwoCtN/image.png)

and only reply when the user message the word help otherwise ignore all the messages. lets re-run the code.


![image.png](https://images.hive.blog/DQmYD5xfG59HUBS3QWXrJD8Vh5jgKy4B581D6kSixgiNkAg/image.png)


 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. 


![image.png](https://images.hive.blog/DQmVfXxxVFoPUqYoaQZf6jdDbuCvobvQk9A5FTnkroVyRKR/image.png)


![image.png](https://images.hive.blog/DQmRpJBhY21UBnyoJbs3UwixfWobFKi8bqD5GYjmBq14T6h/image.png)

Now to get the right message we need to remove the prefix from the message.

![image.png](https://images.hive.blog/DQmPdGkbAMPfy3ixEcJRFCE5z3NpowwAcQybp5BMViAg72v/image.png)


<center>
**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**
![ntitled-1.png](https://images.hive.blog/DQmd3WcHH8i6U2JkmnrvEvd4FwjDQxJtG3Tnhzxdjswktdx/ntitled-1.png)</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 18 others
properties (23)
authorpakgamer
permlinkhow-to-add-custom-prefix-to-your-discord-bot-discordjs-tutorial
categoryhive-148441
json_metadata{"tags":["programming","discordjs"],"image":["https://images.hive.blog/DQmdEofMkxibg1wj9xPh2Ae1aixhWcZP8hL5PpDUbssUBpX/image.png","https://images.hive.blog/DQmYZ7Ag62cSjUbaaytgQ8x8qcTHh9e2QcueQwonrmoSFnm/image.png","https://images.hive.blog/DQmWZczpnkYUj77KA9bGjFkyutFziKgG2ymBk8TkcRrMqoz/image.png","https://images.hive.blog/DQmVwsW9jAmr63Sa8qVhhzquntdsBWZZyctGmwA5JonNLN1/image.png","https://images.hive.blog/DQmZxUD7ffPkb91nk839obzq1SfKYdqmMZ85E9Cv3v3JD2U/image.png","https://images.hive.blog/DQmPXShHv91t6Seb67F7kUz4cbhxmFDR3Yc8FmikE565VMz/image.png","https://images.hive.blog/DQmbSiRwvDnpUEEfhNhKBJ95Fawya1sqX6pLASLZiSwoCtN/image.png","https://images.hive.blog/DQmYD5xfG59HUBS3QWXrJD8Vh5jgKy4B581D6kSixgiNkAg/image.png","https://images.hive.blog/DQmVfXxxVFoPUqYoaQZf6jdDbuCvobvQk9A5FTnkroVyRKR/image.png","https://images.hive.blog/DQmRpJBhY21UBnyoJbs3UwixfWobFKi8bqD5GYjmBq14T6h/image.png","https://images.hive.blog/DQmPdGkbAMPfy3ixEcJRFCE5z3NpowwAcQybp5BMViAg72v/image.png","https://images.hive.blog/DQmd3WcHH8i6U2JkmnrvEvd4FwjDQxJtG3Tnhzxdjswktdx/ntitled-1.png"],"app":"hiveblog/0.1","format":"markdown"}
created2020-06-18 17:59:51
last_update2020-06-18 17:59:51
depth0
children2
last_payout2020-06-25 17:59:51
cashout_time1969-12-31 23:59:59
total_payout_value4.864 HBD
curator_payout_value4.738 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,089
author_reputation19,304,605,112,640
root_title"How to add Custom prefix to your Discord Bot. Discordjs Tutorial"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,042,808
net_rshares22,097,984,540,834
author_curate_reward""
vote details (82)
@gitplait-mod2 ·
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.

<sub> **Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.** </sub>

Join our [Community on Hive](https://hive.blog/trending/hive-103590) and Chat with us on [Discord](https://discord.gg/CWCj3rw).

[[Gitplait-Team]](https://gitplait.tech/)
properties (22)
authorgitplait-mod2
permlinkre-pakgamer-qc63rc
categoryhive-148441
json_metadata{"tags":["hive-148441"],"app":"peakd/2020.05.5"}
created2020-06-19 10:00:30
last_update2020-06-19 10:00:30
depth1
children0
last_payout2020-06-26 10:00:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length490
author_reputation28,898,670,427
root_title"How to add Custom prefix to your Discord Bot. Discordjs Tutorial"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,055,497
net_rshares0
@hivebuzz ·
Congratulations @pakgamer! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@pakgamer/upvoted.png?202006181841"></td><td>You received more than 2750 upvotes. Your next target is to reach 3000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your board](https://hivebuzz.me/@pakgamer) And compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>


To support your work, I also upvoted your post!


###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22109%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/109)!
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-pakgamer-20200618t190935000z
categoryhive-148441
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-06-18 19:09:36
last_update2020-06-18 19:09:36
depth1
children0
last_payout2020-06-25 19:09:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length860
author_reputation370,308,710,293,163
root_title"How to add Custom prefix to your Discord Bot. Discordjs Tutorial"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,043,854
net_rshares0