├── .env.example ├── channels.txt ├── .gitignore ├── package.json ├── README.md └── index.js /.env.example: -------------------------------------------------------------------------------- 1 | DISCORD_TOKEN=your-discord-token -------------------------------------------------------------------------------- /channels.txt: -------------------------------------------------------------------------------- 1 | 1195650263969710171 2 | 1195650273645965373 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | .env 3 | node_modules 4 | logs.txt -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "discord-gmgn-chat-bot", 3 | "version": "1.0.0", 4 | "description": "Automated Discord chatbot that sends 'GM' or 'GN' messages to your specified server targets every day.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "colors": "^1.4.0", 14 | "cron": "^3.1.6", 15 | "discord-simple-api": "^1.0.0", 16 | "dotenv": "^16.4.7" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Discord-GMGN-Chat-Bot 2 | 3 | Discord-GMGN-Chat-Bot is a Node.js bot that uses the `discord-simple-api` package to send "Good Morning" and "Good Night" messages to specified Discord channels at 08:00 UTC and 20:00 UTC respectively. 4 | 5 | ## Features 6 | 7 | - Sends "GM" (Good Morning) at 08:00 UTC to specified channels. 8 | - Sends "GN" (Good Night) at 20:00 UTC to specified channels. 9 | - Reads target channel IDs from a text file. 10 | - Utilizes `.env` for token security. 11 | - Colorful console output for better readability and debugging. 12 | 13 | ## Prerequisites 14 | 15 | Before you begin, ensure you have met the following requirements: 16 | 17 | - Node.js installed on your system. 18 | - A Discord bot token. [Learn how to create a bot and get a token](https://discord.com/developers/applications). 19 | - The channel IDs where the bot will send messages. 20 | 21 | ## Installation 22 | 23 | 1. Clone the repository: 24 | ```sh 25 | git clone https://github.com/dante4rt/Discord-GMGN-Chat-Bot.git 26 | ``` 27 | 2. Navigate to the repository directory: 28 | ```sh 29 | cd Discord-GMGN-Chat-Bot 30 | ``` 31 | 3. Install the necessary packages: 32 | ```sh 33 | npm install 34 | ``` 35 | 4. Create a `.env` file in the root directory and add your Discord bot token: 36 | ``` 37 | DISCORD_TOKEN=your-discord-bot-token 38 | ``` 39 | 5. Add channel IDs to `channels.txt`, one ID per line. 40 | 41 | ## Usage 42 | 43 | To start the bot, run the following command: 44 | 45 | ```sh 46 | node index.js 47 | ``` 48 | 49 | The bot will automatically send "GM" and "GN" messages at the specified times. 50 | 51 | ## Contributing 52 | 53 | Contributions to the Discord-GMGN-Chat-Bot are welcome. If you have a suggestion that would improve this, please fork the repository and create a pull request. 54 | 55 | ## Contact 56 | 57 | If you have any questions or want to get in touch, please open an issue on the GitHub repository. 58 | 59 | Enjoy your automated Discord greetings! -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | require('colors'); 3 | const { CronJob } = require('cron'); 4 | const Discord = require('discord-simple-api'); 5 | const fs = require('fs'); 6 | 7 | if (!process.env.DISCORD_TOKEN) { 8 | console.error('The DISCORD_TOKEN is not set in .env file.'.red); 9 | process.exit(1); 10 | } 11 | 12 | const bot = new Discord(process.env.DISCORD_TOKEN); 13 | 14 | let channelIDs; 15 | try { 16 | if (!fs.existsSync('channels.txt')) { 17 | throw new Error('channels.txt file does not exist.'); 18 | } 19 | channelIDs = fs 20 | .readFileSync('channels.txt', 'utf-8') 21 | .split('\n') 22 | .filter(Boolean); 23 | } catch (error) { 24 | console.error(error.message.red); 25 | process.exit(1); 26 | } 27 | 28 | const sendCronMessage = (message, time, color) => { 29 | return new CronJob( 30 | time, 31 | () => { 32 | const sendMessageSequentially = (index = 0) => { 33 | if (index >= channelIDs.length) return; 34 | 35 | const channelId = channelIDs[index]; 36 | if (!channelId.match(/^\d+$/)) { 37 | console.error( 38 | `Invalid channel ID "${channelId}" found in channels.txt`.red 39 | ); 40 | return sendMessageSequentially(index + 1); 41 | } 42 | 43 | bot 44 | .sendMessageToChannel(channelId, message) 45 | .then((res) => { 46 | const logMessage = `Channel ID : ${channelId} | Message : ${ 47 | res.content 48 | } | Date : ${new Date().toUTCString()}`; 49 | console.log(logMessage[color]); 50 | fs.appendFile('logs.txt', logMessage + '\n', (err) => { 51 | if (err) console.error('Failed to write to logs.txt'.red, err); 52 | }); 53 | }) 54 | .catch((err) => { 55 | const errorLog = `Failed to send message to channel ${channelId} | Date : ${new Date().toUTCString()} | Error : ${ 56 | err.response.data.message 57 | }`; 58 | console.error(errorLog.red); 59 | fs.appendFile('logs.txt', errorLog + '\n', (err) => { 60 | if (err) console.error('Failed to write to logs.txt'.red, err); 61 | }); 62 | }) 63 | .finally(() => { 64 | setTimeout(() => sendMessageSequentially(index + 1), 1000); 65 | }); 66 | }; 67 | 68 | sendMessageSequentially(); 69 | }, 70 | null, 71 | true, 72 | 'UTC' 73 | ); 74 | }; 75 | 76 | const gmJob = sendCronMessage('GM', '0 8 * * *', 'green'); 77 | const gnJob = sendCronMessage('GN', '0 20 * * *', 'blue'); 78 | 79 | gmJob.start(); 80 | gnJob.start(); 81 | 82 | console.log('Cron jobs started.'.yellow); 83 | --------------------------------------------------------------------------------