Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
refactor: make helpers.ts for helper methods (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
sealsrock12 authored Sep 11, 2023
1 parent ba72d0c commit 4a1fe20
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
19 changes: 19 additions & 0 deletions src/core/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file
* This file exports helper methods that are used multiple times in modules/other code.
*/

/**
* Formats the current date to YYYY-MM-DD HH:MM:SS
* @param date The current date
*/
export function formatDate(date: Date): string {
const year = date.getFullYear();
const month = (1 + date.getMonth()).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');

return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
1 change: 1 addition & 0 deletions src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './logger.js';
export * from './modules.js';
export * from './main.js';
export * from './mongo.js';
export * from './helpers.js';
17 changes: 1 addition & 16 deletions src/modules/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ interface Application {

const APPLICATION_COLLECTION_NAME = 'applications';

/**
* Formats the current date to YYYY-MM-DD HH:MM:SS
* @param date The current date
*/
function formatDate(date: Date): string {
const year = date.getFullYear();
const month = (1 + date.getMonth()).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');

return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
* Function to get the modal fields from the questions set in the module config
*/
Expand Down Expand Up @@ -170,7 +155,7 @@ const apply = new util.RootModule(
_id: new ObjectId().toHexString(),
user: submittedModal.member!.user.id,
responses: responses,
date: formatDate(new Date()),
date: util.formatDate(new Date()),
status: 'Pending',
};

Expand Down
23 changes: 4 additions & 19 deletions src/modules/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ async function deleteRecord(user: User): Promise<void> {
await records.deleteOne({user: user.id});
}

/**
* Formats the current date to YYYY-MM-DD HH:MM:SS
* @param date The current date
*/
function formatDate(date: Date): string {
const year = date.getFullYear();
const month = (1 + date.getMonth()).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');

return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/** The root note command group */
const notes = new util.RootModule('note', 'Add or delete notes from users', [
util.mongo,
Expand Down Expand Up @@ -105,7 +90,7 @@ notes.registerSubModule(
const note: Note = {
contents: noteContents,
addedBy: interaction.user.id,
date: formatDate(new Date()),
date: util.formatDate(new Date()),
};

const db: Db = util.mongo.fetchValue();
Expand Down Expand Up @@ -221,7 +206,7 @@ notes.registerSubModule(
files: [
{
attachment: file,
name: `notes_for_${user.id}_${formatDate(new Date())}.json`,
name: `notes_for_${user.id}_${util.formatDate(new Date())}.json`,
},
],
});
Expand Down Expand Up @@ -270,12 +255,12 @@ const whois = new util.RootModule(
// Addd info fields
fields.push({
name: 'Created at',
value: formatDate(member.user.createdAt),
value: util.formatDate(member.user.createdAt),
inline: true,
});
fields.push({
name: 'Joined at',
value: formatDate(member.joinedAt!),
value: util.formatDate(member.joinedAt!),
inline: true,
});
fields.push({
Expand Down
17 changes: 1 addition & 16 deletions src/modules/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ interface Warning {

const WARNING_COLLECTION_NAME = 'warnings';

/**
* Formats the current date to YYYY-MM-DD HH:MM:SS
* @param The date to format
*/
function formatDate(date: Date): string {
const year = date.getFullYear();
const month = (1 + date.getMonth()).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');

return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
* Function to get an array of all warns of a user by their id
*
Expand Down Expand Up @@ -89,7 +74,7 @@ export async function warnUser(
user: member.id,
author: author.id,
reason: reason,
date: formatDate(new Date()),
date: util.formatDate(new Date()),
});

const existingWarns: Warning[] = await getWarns(member.id);
Expand Down

0 comments on commit 4a1fe20

Please sign in to comment.