From da0e59ebdf2d6c10a8f37655fbe86d19fe31d101 Mon Sep 17 00:00:00 2001 From: Waren Gonzaga Date: Fri, 5 May 2023 03:32:53 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20TWEAK:=20modular=20utilities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot.js | 72 +++---------------------------------------- src/utils/core.js | 38 +++++++++++++++++++++++ src/utils/database.js | 41 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 src/utils/core.js create mode 100644 src/utils/database.js diff --git a/src/bot.js b/src/bot.js index 7cc21fb..b3bd32e 100644 --- a/src/bot.js +++ b/src/bot.js @@ -4,11 +4,10 @@ const { Client, ChannelType, GatewayIntentBits, - Partials, - EmbedBuilder } = require('discord.js'); -const { GoogleSpreadsheet } = require('google-spreadsheet'); -const config = require(`${__dirname}/config.json`); -const moment = require('moment'); + Partials } = require('discord.js'); +const config = require('./config.json'); +const { sendEmbedMessage, formatTime } = require('./utils/core'); +const { sendData } = require('./utils/database'); require('dotenv').config(); @@ -34,29 +33,6 @@ const client = new Client({ ] }); -// load spreadsheet -const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID); - -/** - * send embed message - * @param {string} message - * @returns pre-defined embed style - */ -const sendEmbedMessage = (message) => { - return new EmbedBuilder() - .setDescription(message) - .setColor(`#f213a4`); -} - -/** - * get username from ownerid - * @param {number} id - * @returns - */ -const getUsernameFromId = async (id) => { - return (await client.users.fetch(id)).username; -} - // listen to post messages client.on('messageCreate', async (message) => { if (message.author.bot) return; @@ -253,46 +229,6 @@ client.on('threadCreate', async post => { }, config.datasheet_init); }); -/** - * sends data to the spreadsheet - * @param {object} data - data being added as row in the spreadsheet - * @param {string} datasheet - name of sheet where data being sent e.g. init, response, resolve - */ -const sendData = async (data, datasheet) => { - // authenticate - await doc.useServiceAccountAuth({ - client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, - private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), - }); - // load the "initial" sheet - await doc.loadInfo(); - const sheet = doc.sheetsByTitle[datasheet]; - - // check if the data will be send to init sheet - if (datasheet === config.datasheet_init) { - await sheet.addRow(data); - }; - - // check if the data will be send to response sheet - if (datasheet === config.datasheet_response) { - await sheet.addRow(data); - } - - // check if the data will be send to resolve sheet - if (datasheet === config.datasheet_resolve) { - await sheet.addRow(data); - }; -} - -/** - * format time according to UTC - * @param {number} date - epoch timestamp - * @returns time and date format - */ -const formatTime = (date) => { - return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); -} - // reading events file const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); diff --git a/src/utils/core.js b/src/utils/core.js new file mode 100644 index 0000000..fdae6c1 --- /dev/null +++ b/src/utils/core.js @@ -0,0 +1,38 @@ +const { EmbedBuilder } = require('discord.js'); +const moment = require('moment'); +const config = require('../config.json'); + +/** + * send embed message + * @param {string} message + * @returns pre-defined embed style + */ +const sendEmbedMessage = (message) => { + return new EmbedBuilder() + .setDescription(message) + .setColor(`#f213a4`); +} + +/** + * format time according to UTC + * @param {number} date - epoch timestamp + * @returns time and date format + */ +const formatTime = (date) => { + return moment.utc(date).utcOffset(config.utc_offset).format('M/DD/YYYY HH:mm:ss'); +} + +/** + * get username from ownerid/author.id + * @param {number} id user's id + * @returns + */ +const getUsernameFromId = async (id) => { + return (await client.users.fetch(id)).username; +} + +module.exports = { + sendEmbedMessage, + formatTime, + getUsernameFromId +} \ No newline at end of file diff --git a/src/utils/database.js b/src/utils/database.js new file mode 100644 index 0000000..a8326f0 --- /dev/null +++ b/src/utils/database.js @@ -0,0 +1,41 @@ +const config = require('../config.json'); +const { GoogleSpreadsheet } = require('google-spreadsheet'); +require('dotenv').config(); + +// load the spreadsheet +const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID); + +/** + * sends data to the spreadsheet + * @param {object} data - data being added as row in the spreadsheet + * @param {string} datasheet - name of sheet where data being sent e.g. init, response, resolve + */ +const sendData = async (data, datasheet) => { + // authenticate + await doc.useServiceAccountAuth({ + client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, + private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), + }); + // load the "initial" sheet + await doc.loadInfo(); + const sheet = doc.sheetsByTitle[datasheet]; + + // check if the data will be send to init sheet + if (datasheet === config.datasheet_init) { + await sheet.addRow(data); + }; + + // check if the data will be send to response sheet + if (datasheet === config.datasheet_response) { + await sheet.addRow(data); + } + + // check if the data will be send to resolve sheet + if (datasheet === config.datasheet_resolve) { + await sheet.addRow(data); + }; +} + +module.exports = { + sendData +} \ No newline at end of file