Guide to Building a Basic Bot for Discord - Episode 3 (Adding a Greeting and Announcement Function)

in #utopian-io7 years ago

image.png

Good day Everyone. Hopefully your coding skills are progressing and improving at a steady pace. Each day I continue to experiment with new lines of code to see how they work and how they can improve users overall experience with my Discord Server. It can be frustrating at times, but the key is to not give up - also ask questions from anyone who is willing to help.

I continue to add new lines of code to my Discord Bot and would like to share the latest update that I have learned and incorporated into the existing Bot that we have been working on.

image.png

For Episode 3 of my ongoing series, I will share with you how to add an Announce and Greet function that will notify everyone of new members joining your discord server as well as Greet them with a warm welcome and directions to help them get started.

At this point, we have already accomplished and coded the following:

  1. How to setup and deploy your Bot on Discord
  2. How to code a simple response to pre-specified questions and comments
  3. How to program your bot to draw random numbers and keep your users entertained

This addition is not as complicated as the previous lessons and only requires you to add a few lines of code to your already existing index.js file from our Episode 2 guide.


Your existing index.js code should resemble the following screenshot

image.png

You will notice that I have left a few spaces where I will add the few lines of code required to complete this task. Again - using Visual Studio Code (or the editor of your choice), you can add the following code:


bot.on("guildMemberAdd", member => {
    let guild = member.guild;
    guild.defaultChannel.sendMessage(`Welcome ${member.user} to our Discord Server.  Please check out the FAQ channel for documentation and support to help you get started`).catch(console.error);
  });

What the above code is telling the Bot to do is scan for new members joining your discord server. If a new user is found then announce to the channel and welcome that person with a message.

Your updated code should look like the following screenshot

image.png

Above, I have highlighted the added code.

Simple Right? Now let's test it out. Save your work by pressing Ctrl-s and we can test your updated bot in Discord.

Using the Terminal Window in Visual Studio Code, type in the command node --harmony . in order to execute your bot.

In your Discord Server, anytime someone new enters the channel, the bot will announce the arrival to the channel and Welcome the user with a message of your choosing. You should see something similar to the below screenshot

image.png

Hopefully this added code will help automate the task of welcoming new users and letting people know that someone new has joined. It's a great way to help make people feel more welcome and let people know they have someone new they can either help or talk to.


I also wanted to take the time to share an additional resource which has proven to be a useful reference for coding. https://discord.js.org/#/ is a site where you can find a lot of resources to help aid you in your learning. If you go to the Documentation section, you can find a lot of additional guides as well as various code and syntax you can practice with geared specifically for the discord.js library we are using to code for Discord. There are also several examples you can leverage for your bot that you might find interesting.

image.png

I hope you enjoyed this post and found it helpful. If you are looking for a more challenging bot to code, you can also refer to another recent guide I posted - Guide to Creating a Discord Auto Upvote Bot for MS Windows using Javascript which is based on guides and help I recieved from @mahdiyari who has many other helpful guides of his own that are of great help.

If there are any updates, feedback, input, or suggestions - please let me know in the comments below. If you found this helpful, please consider visiting my Steemit profile and upvoting, resteeming, and voting for my witness cloh76.witness. I appreciate the support!



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @cloh76 I am @utopian-io. I have just super-voted you at 15% Power!

Achievements

  • Votes on this contribution are going well. Nice!
  • You are having more votes than average for this category. Nice!
    Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you for the contribution. It has been approved.

[utopian-moderator]

Thanks @deveerei!

You're welcome!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65733.39
ETH 3506.40
USDT 1.00
SBD 2.51