This project was created in order to:
- Remind to read my favorite manga
- Check price of cryptocurrency
- Check my cryptocurrency wallet (in-development feature)
- Check the lowest and highest price (in-development feature)
- ???
I haven't had many ideas to make it more useful, please feel free to contact me if you have something to improve the bot.
- Discord.js V13
- NodeJS
- PostgreSQL
- Prisma (ORM Framework)
- Puppeteer
- CoinMarketCap APIs (free version)
- Cron Job
- Heroku server
There is a saying: "Don't reinvent the wheel". This means there are a lot of tutorial out there to set up a Discord bot, I don't need to do it again. When I was doing this project, I followed the tutorials below here to complete the task:
In general, they have several steps:
- Create an Application in Discord
- Create a Discord Bot in the Application
- Grant permissions to the Bot and attach it to a Discord Server
- Get the Bot's token to our NodeJS server
- Sign in using that token in our server (checkout
discord.js
file)
In this project, I use Prisma as the ORM Framework (it can connect to both SQL and NoSQL databases with minimal effort). You can easily see the prisma
directory, it stores the migration files and database models that our application needs.
There are some models to cover my need:
- User (contains Discord Id)
- Manga (contains interesting manga)
- MangaChapter
- Reminder
- Coin (contains interesting coins)
- PriceUpdate
- PriceUpdateConversion
Before migrating the database, you need to create a database and prepare the .env
file. Subsequently, we can migrate our database and seed data.
Note: You should edit file seed_manga.js
and seed_coins.js
in your interest.
yarn install # install node_modules first
npx prisma migrate dev # migrate all changes in database
npm run db:seed # seed data
- prisma: Database models, migrations, seeders
- src:
- jobs: Cron and non-cron jobs for fetching data
- parsers: Parse data from website to gather manga data
- platforms: Serve as connector to Discord and maybe other communicating platforms
- services: Handle logic and interact with database
To check the manga available in different websites, web crawling is used periodically with cron jobs. There are many libraries for crawling, but Axios and Puppeteer are my preferable technology. Axios is famously known for request invoking functionality. On the other hand, Puppeteer serves as a headless browser with Chromium API to overcome CDN.
You can see the code here.
Example:
I use CoinMarketCap APIs to get the data every 5 minutes and store it into database, you can check this file src/jobs/cryptocurrency/fetch_coin_price.js
. It also sends the price update message to me in Discord if time is appropriate.
Example:
Working on this project is so enjoyable. I will optimize and add more features as long as I am still interested in this idea. Thank you guys for reading.