Introducing Neox Bot v1.2.0 - A discord bot for Neoxian city

in #utopian-io5 years ago

Git Hub: https://github.com/bala41288/neox-bot/releases/tag/v1.2.0

Introduction

Neoxian city is a virtual city in Discord owned by @neoxian in other words called as Mr. Dragon. It is one of the favorite places for many people where so many exciting things happen. These exciting things include giveaways, discordtips, knowledge sharing, token sharing, love sharing, etc. There was a user story from @neoxian to have his own bot for the city with some features to make giveaways easier.

Neox-Bot.png

The version of the bot that I'm going to be talking about in this article is v1.2.0 with a giveaway feature. The previous versions of the bot had functionalities to display the date and time of the Steem and Wls posts shared in the discord channels. This will make sure the post is not past the payout date. In the current version of the bot, a giveaway feature is introduced where people who have access to the #giveaway channel in Neoxian city can do a giveaway.

History behind the idea

There is already a giveaway discord bot available. Anyone who owns a discord server can invite the existing giveaway bot to their discord server. The bot is written in Java and the source code is also open to everyone.

Git Hub: https://github.com/jagrosh/GiveawayBot

The challenge in this existing bot is that the bot goes offline quite often and we will not be able to do giveaways using this bot. Another alternative to using this giveaway bot would be to take the code and do a self-hosting to use it. In our case, we wanted to implement all the functionalities of the Neoxian server in just one bot so that it can be managed easily. The future versions of this bot will have other improvised functionalities as well.

Technical details

I wanted to write the bot using NodeJs with the help of DiscordJs. I have used the following technology stack to build this discord bot.

  • Node.js
  • Express
  • Discordjs
  • Steem
  • Moment Js
  • Mongo DB

Till the previous version of the bot, there was no need for a database. In order to implement a giveaway feature, I introduced a mongo database to store and retrieve the giveaway information. This will also store the history of giveaways happening in Neoxian City. In the future, it will be possible to identify who won the maximum number of giveaways and who initiated lots of giveaways. After all, stats always attracts us.

Usage

$neox - Provides the current version of the bot

image.png

$neox gcreate - Interactive giveaway setup

image.png

The source code is open with an ISC license. Anyone can use the code to build their own bots or applications.

Code Snippet

The main file is the app.js file and it has most of the code. For anyone who is already into coding and is familiar with Javascript, the source code is self-explanatory. I would like to provide some more details about the Giveaway feature logic.

I first create a dummy giveaway record in the mongodb collection when the giveaway is initiated by a user.

if (msg.content.indexOf('$neox gcreate') == 0) {
                            console.log('no data');
                            var giveaway = new Giveaway({
                                initiatorID: msg.author.id,
                                initiatorUsername: msg.author.username,
                                createdTimestamp: msg.createdTimestamp,
                                startTime: "",
                                endTime: "",
                                channel: "",
                                duration: "",
                                winnerCount: "",
                                winners: "",
                                prize: "",
                                gid: "",
                                status: "started"
                            });
                            giveaway.save((err, doc) => {
                                if (!err) {
                                    msg.channel.send(`:tada: Alright! Let's set up your giveaway! First, what channel do you want the giveaway in?`);
                                    return;
                                }
                                else {
                                    console.log('Error in Saving Giveaway: ' + JSON.stringify(err, undefined, 2));
                                    msg.channel.send(`There was an error while initiating the Giveaway. Please contact Administrator.`);
                                }
                            })
                        }

After the record is created, in each interaction with the bot, I keep updating the existing record with the information by checking what is empty so far. I also used a field to control the status of the giveaway so that if a giveaway is canceled or uncompleted, it can be marked accordingly.

The replies and the messages from the bot can be sent in an attractive layout with the help of embeds feature available in the discord. I used a piece of code to send the information in the form of Embed. This will look neat and attractive.

bot.channels.get(ch).send({
                                                    "embed": {
                                                        "title": ":tada: Neoxian City Giveaway :tada:",
                                                        "description": value.prize,
                                                        "url": "",
                                                        "color": 2146335,
                                                        "footer": {
                                                            "icon_url": "https://steemitimages.com/p/4qEixipsxSf1jXvCicS49aiaKDfFxASf1eKR39suyU4qmikNaw2FMepusxFTD1TUaJ",
                                                            "text": footer
                                                        },
                                                        "fields": [
                                                            {
                                                                "name": "React with :tada: to enter the Giveaway",
                                                                "value": timeRemaining
                                                            },
                                                            {
                                                                "name": `${value.winnerCount} Winners`,
                                                                "value": gift.repeat(value.winnerCount)
                                                            }
                                                        ]
                                                    }
                                                });

The above piece of code when viewed in the discord server will look like the below.

image.png

The winner selection was a bit tricky initially for me to implement. When I first started exploring that, I had to track who clicked on the reaction and then choose the random winner from that. There was an inbuilt method available in the DiscordJs called awaitReactions.

msg.awaitReactions(filter, {
   time: data.duration * 60000
})

I provided the duration the bot should wait and collect the reactions to move to the next step. Based on the timeout, the bot will check the reactions and exclude the reaction from the bot itself and pick the random winners from the users who reacted to the giveaway.

Road map

I'm planning to add some more features in the future to the Neox-bot. As mentioned earlier, the idea is to have one powerful task that can be smart enough to manage most of the activities in the city. Some already in my list are:

  • Any linked dropped by the users in the promotion channel should be converted to SteemPeak link.
  • A ranking system to display the top giveaway initiators and top winners.
  • A feature to pick a random number between two given numbers. Requested by @aamirijaz.
  • A feature that will rank the users based on the chats.
Sort:  
  • Good article with images, code samples and explanations of coding choices.
  • Please include the PR link or the list of commits in your next article.
  • Also add a license to the project repository.
  • The code would benefit from eslint module.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thanks for the review @helo. This was my first development contribution to Utopian-io. I will remember the points mentioned above for my next article.

Thank you for your review, @helo! Keep up the good work!

I must say, I love Neox bot more for it many awesome features and future features that will be added to it. Thank you very much Dragon Bala, for making our wonderful city of neoxian independent.

Hi @bala41288!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Thank you Bala for providing my City with its very own bot. It gets more useful everyday.

Hey, @bala41288!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Great work Mr Bala, you really did a great work.

Posted using Partiko Android

Well done bala and I really like this new bot.
Congratulations for a job well done...

Congratulations @bala41288! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published a post every day of the week

You can view your badges on your Steem Board and compare to others on the Steem 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!

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Wow it's really good bot @bala41288 I must use this bot thanks for information

Posted using Partiko Android

This is awesome bala, a well detailed job you did.Since its existence no more bot breakdown.Thank you for putting in so much work and sweat.We the citizens are eternally grateful.

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.035
BTC 66739.07
ETH 3228.97
USDT 1.00
SBD 4.23