Skip to content

Commit

Permalink
✨ TWEAK: modular utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
warengonzaga authored May 5, 2023
1 parent 2fef85b commit da0e59e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 68 deletions.
72 changes: 4 additions & 68 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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'));
Expand Down
38 changes: 38 additions & 0 deletions src/utils/core.js
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions src/utils/database.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit da0e59e

Please sign in to comment.