Skip to content

Commit

Permalink
Merge pull request #455 from chessebuilderman/Change-Code-Gen-to-Sqli…
Browse files Browse the repository at this point in the history
…te-Base

Change code gen to JSON DB
  • Loading branch information
dr4wn authored Jul 13, 2024
2 parents 22bde23 + cc0d4ac commit 7bab596
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 793 deletions.
787 changes: 34 additions & 753 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
"node": ">=16.0.0",
"npm": ">=8.0.0"
},
"scripts": {
"hook": "is-ci || husky install || true",
"lint": "npx eslint src",
"clean": "run-script-os",
"clean:windows": "if exist \".\\dist\" rmdir /s /q \".\\dist\"",
"clean:linux": "rm -rf ./dist",
"clean:default": "rm -rf ./dist",
"build": "npm run clean && tsc --build",
"title": "run-script-os",
"title:windows": "title Inertia Lighting Discord Bot",
"title:default": "",
"start": "npm run title && node --trace-warnings --enable-source-maps --require module-alias/register --require dotenv/config ."
},
"scripts": {
"hook": "is-ci || husky install || true",
"lint": "npx eslint src",
"clean": "run-script-os",
"clean:windows": "if exist \".\\dist\" rmdir /s /q \".\\dist\"",
"clean:linux": "rm -rf ./dist",
"clean:default": "rm -rf ./dist",
"build": "npm run clean && tsc --build",
"title": "run-script-os",
"title:windows": "title Inertia Lighting Discord Bot",
"title:default": "",
"start": "npm run title && node --trace-warnings --enable-source-maps --require module-alias/register --require dotenv/config ."
},
"devDependencies": {
"@types/string-similarity": "4.0.2",
"@types/uuid": "9.0.8",
Expand All @@ -46,6 +46,7 @@
"discord.js": "14.14.1",
"dotenv": "16.4.5",
"go-mongo-db": "github:MidSpike/go-mongo-db",
"lowdb": "^7.0.1",
"module-alias": "2.2.3",
"moment-timezone": "0.5.45",
"node-gyp": "10.0.1",
Expand Down
51 changes: 32 additions & 19 deletions src/common/handlers/code_generation/code_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@
// Copyright (c) Inertia Lighting, Some Rights Reserved //
//------------------------------------------------------------//

import * as events from 'events';

import { ButtonStyle, CommandInteraction, ComponentType, Message } from 'discord.js';
import { ButtonStyle, CommandInteraction, ComponentType } from 'discord.js';

import { CustomEmbed } from '../../message.js';

import { getMarkdownFriendlyTimestamp } from '@root/utilities';

import { RobloxUsersApiUser, event_map, getUserData, getUserUpdates } from './user_update.js';

import create_db_handler from './create_db_handler.js';

import { verification_code_data } from './types.js';

//------------------------------------------------------------//

interface verification_code_data {
interaction: CommandInteraction,
roblox_id: string,
code: string,
expiration: number
event: events.EventEmitter
message_object: Message;
}

//------------------------------------------------------------//

export const codes: Array<verification_code_data> = [];
export const codes: verification_code_data[] = [];

const db_database_name = `${process.env.MONGO_DATABASE_NAME ?? ''}`;
if (db_database_name.length < 1) throw new Error('Environment variable: MONGO_DATABASE_NAME; is not set correctly.');
Expand Down Expand Up @@ -56,7 +50,14 @@ const code_length = 10;

async function checkUser(user_id: string, interaction: CommandInteraction) {
const user_data = await getUserData(user_id);
codes.filter((data: verification_code_data) => data.roblox_id === user_id).forEach((data) => {

//------------------------------------------------------------//

const code_db = await create_db_handler();

//------------------------------------------------------------//

code_db.data.codes.filter((data: verification_code_data) => data.roblox_id === user_id).forEach((data) => {
interaction.editReply({
embeds: [
CustomEmbed.from({
Expand Down Expand Up @@ -85,6 +86,13 @@ async function checkUser(user_id: string, interaction: CommandInteraction) {

export async function generateVerificationCode(user_id: string, interaction: CommandInteraction): Promise<undefined> {
if (await checkUser(user_id, interaction) === false) return;

//------------------------------------------------------------//

const code_db = await create_db_handler();

//------------------------------------------------------------//

let code: string = '';
const random_places: Array<[number, string]> = [];
special_word_array.forEach((word) => {
Expand Down Expand Up @@ -117,15 +125,15 @@ export async function generateVerificationCode(user_id: string, interaction: Com
const user_data = await getUserData(user_id);
const event = await getUserUpdates(user_id);
const message_object = (await interaction.fetchReply());
const push_data = {
const push_data: verification_code_data = {
interaction: interaction,
roblox_id: user_id,
code: code,
expiration: Date.now() + 43200000,
event: event,
message_object: message_object,
};
codes.push(push_data);
code_db.data.codes.push(push_data);

const discord_friendly_timestamp = getMarkdownFriendlyTimestamp(push_data.expiration);

Expand Down Expand Up @@ -181,7 +189,7 @@ export async function generateVerificationCode(user_id: string, interaction: Com
],
});
event_map.delete(push_data.roblox_id);
codes.filter((data) => data.roblox_id === user_id).forEach((_Object, index) => codes.splice(index));
code_db.data.codes.filter((data) => data.roblox_id === user_id).forEach((_Object, index) => code_db.data.codes.splice(index));
console.log('The recovery code is part of the user\'s blurb.');
return;
} else {
Expand All @@ -198,9 +206,14 @@ export async function generateVerificationCode(user_id: string, interaction: Com

/* -------------------------------------------------------------------------- */

setInterval(() => {
setInterval(async () => {
//------------------------------------------------------------//

const code_db = await create_db_handler();

//------------------------------------------------------------//

codes.filter((element) => element.expiration <= Date.now()).forEach((data, index) => {
code_db.data.codes.filter((element) => element.expiration <= Date.now()).forEach((data, index) => {
data.interaction?.editReply({
embeds: [
CustomEmbed.from({
Expand All @@ -210,7 +223,7 @@ setInterval(() => {
}),
],
});
codes.splice(index);
code_db.data.codes.splice(index);

});
}, 60000);
11 changes: 11 additions & 0 deletions src/common/handlers/code_generation/create_db_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { UserUpdateEmitter } from './user_update';
import { user_db_type } from './types';


// eslint-disable-next-line func-names
export default async function() {
const { JSONFilePreset } = await import('lowdb/node');
const db_pre: user_db_type = { codes: [], events: new Map<string, UserUpdateEmitter>() };
const code_db = await JSONFilePreset('user_data.json', db_pre);
return code_db;
}
14 changes: 14 additions & 0 deletions src/common/handlers/code_generation/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserUpdateEmitter } from './user_update';
export interface verification_code_data {
interaction: CommandInteraction,
roblox_id: string,
code: string,
expiration: number
event: events.EventEmitter
message_object: Message;
}

export type user_db_type = {
codes: verification_code_data[];
events: Map<string, UserUpdateEmitter>
}
1 change: 1 addition & 0 deletions src/common/handlers/code_generation/user_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
24 changes: 19 additions & 5 deletions src/common/handlers/code_generation/user_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import EventEmitter from 'node:events';

import axios from 'axios';
import create_db_handler from './create_db_handler';

//------------------------------------------------------------//

class UserUpdateEmitter extends EventEmitter {}
export class UserUpdateEmitter extends EventEmitter {}

export const event_map = new Map<string, UserUpdateEmitter>();

Expand Down Expand Up @@ -36,11 +37,18 @@ export type RobloxUsersApiUser = {
* @returns {Promise<events.EventEmitter>}
*/
export async function getUserUpdates(roblox_id: string | number): Promise<UserUpdateEmitter> {
//------------------------------------------------------------//

const code_db = await create_db_handler();

//------------------------------------------------------------//
const returning_event = new UserUpdateEmitter();
event_map.set(roblox_id.toString(), returning_event);
code_db.data.events.set(roblox_id.toString(), returning_event);
return returning_event;
}

/* -------------------------------------------------------------------------- */

/**
* @async
*
Expand All @@ -49,16 +57,22 @@ export async function getUserUpdates(roblox_id: string | number): Promise<UserUp
* @returns {Promise<RobloxUsersApiUser>}
*/
export async function getUserData(roblox_id: string | number): Promise<RobloxUsersApiUser> {

const request = await users_api.get<RobloxUsersApiUser>(`/v1/users/${roblox_id}`);
const request_data = request.data;
return request_data;
}
/* -------------------------------------------------------------------------- */

setInterval(() => {
event_map.forEach(async (v, k) => {
setInterval(async () => {
//------------------------------------------------------------//

const code_db = await create_db_handler();

//------------------------------------------------------------//
code_db.data.events.forEach(async (v, k) => {
const request = await users_api.get<RobloxUsersApiUser>(`/v1/users/${k}`);
const request_data = request.data;
v.emit('Update', request_data);
});
}, 10000);
}, 30000);
10 changes: 7 additions & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ export interface DbUserProducts {
[product_code: string]: boolean;
}

export type DbUserProductsArray = string[];

export interface DbUserTicketBlacklist {
blacklisted: boolean;
reason: string;
}

export interface DbUserData {
_id: MongoDB.ObjectId;
identity: DbUserIdentity;
lumens: number;
products: DbUserProducts;
ticket_blacklist?: DbUserTicketBlacklist
}

export interface DbUserDataArray {
_id: MongoDB.ObjectId;
identity: DbUserIdentity;
lumens: number;
products: DbUserProductsArray;
products: string[];
ticket_blacklist?: DbUserTicketBlacklist
}

//------------------------------------------------------------//
Expand Down

0 comments on commit 7bab596

Please sign in to comment.