Skip to content

Commit

Permalink
Merge branch 'master' into Change-Code-Gen-to-Sqlite-Base
Browse files Browse the repository at this point in the history
  • Loading branch information
DaRealFrost authored Jul 5, 2024
2 parents 5edea3c + 22bde23 commit 7610fba
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ MONGO_QUICK_SUPPORT_TOPICS_COLLECTION_NAME='quick-support-topics'
MONGO_USER_NOTES_COLLECTION_NAME='user-notes'

# Api Server Configurations
API_BASE64_ENCODED_TOKEN_FOR_USER_VERIFICATION_ENDPOINTS=''
API_SERVER_IP=''
API_SERVER_PORT=''
API_BASE64_ENCODED_TOKEN_FOR_USER_VERIFICATION_ENDPOINTS=''
19 changes: 19 additions & 0 deletions .github/workflows/docker-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: "docker-cd"

on:
push:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

# - name: Build Docker Image
# run: |
# docker build . -t ci:0

# - name: Deploy Docker
# run: |
# docker compose pull
15 changes: 15 additions & 0 deletions .github/workflows/docker-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "docker-ci"

on:
push: {}
pull_request: {}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Build Docker Image
run: |
docker build . -t ci:0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ __*

# Whitelisted Files
!start_bot.bat
!update.bat
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm test
npm run lint
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Inertia Lighting Discord Bot

[![CodeQL](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/codeql-analysis.yml)

## About

This code was made for Inertia Lighting.

Any attempt to use it without explicit permission is prohibited.

Assume that all works contained in this repository are copyrighted and protected under law.

## Copyright

Copyright © Inertia Lighting, Some Rights Reserved
# Inertia Lighting Discord Bot

[![CodeQL](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/codeql-analysis.yml)
[![ESLint](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/eslint.yml/badge.svg)](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/eslint.yml)
[![docker-ci](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/docker-ci.yml/badge.svg)](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/docker-ci.yml)
[![docker-cd](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/docker-cd.yml/badge.svg)](https://github.com/Inertia-Lighting/discord-bot/actions/workflows/docker-cd.yml)

## About

This code was made for Inertia Lighting.

Any attempt to use it without explicit permission is prohibited.

Assume that all works contained in this repository are copyrighted and protected under law.

## Copyright

Copyright © Inertia Lighting, Some Rights Reserved
Empty file added compose.yaml
Empty file.
6 changes: 6 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

services:
discord-bot:
image: 'intertia-discord-bot'
env_file:
- ./.env
19 changes: 19 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:lts-alpine

# Create app directory
WORKDIR /usr/src/app

# Copy source code
COPY . /usr/src/app/

# Install dependencies
RUN npm ci --immutable

# Build the bot
RUN npm run build

# Create tmp directory
RUN mkdir -p /usr/src/app/temporary

# Start the bot
CMD ["npm", "start"]
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
"node": ">=16.0.0",
"npm": ">=8.0.0"
},
"scripts": {
"hook": "is-ci || husky",
"lint": "npx eslint .",
"clean": "run-script-os",
"clean:windows": "if exist \".\\dist\" rmdir /s /q \".\\dist\"",
"clean:linux": "rm -rf ./dist",
"build": "npm run clean && tsc --build",
"prepare": "npm run hook && npm run lint && npm run build",
"test": "npm run prepare",
"title": "run-script-os",
"title:windows": "title Inertia Lighting Discord Bot",
"title:default": "",
"start": "npm run prepare && 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 Down
1 change: 1 addition & 0 deletions src/common/handlers/user_data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '.';
13 changes: 13 additions & 0 deletions src/common/handlers/user_data/user_data_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DbUserData, DbUserDataArray } from '@root/types';

export async function dbUserArray(
db_user_data: DbUserData
): Promise<DbUserDataArray> {
const product_array: string[] = Object.keys(db_user_data.products).filter(product => db_user_data.products[product]);
return {
_id: db_user_data._id,
identity: db_user_data.identity,
lumens: db_user_data.lumens,
products: product_array,
};
}
32 changes: 9 additions & 23 deletions src/common/handlers/user_profile_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import axios from 'axios';

import * as Discord from 'discord.js';

import { DbBlacklistedUserRecord, DbProductData, DbUserData } from '@root/types';
import { DbBlacklistedUserRecord, DbProductData, DbUserData, DbUserDataArray } from '@root/types';

import { go_mongo_db } from '@root/common/mongo/mongo';

import { CustomEmbed } from '@root/common/message';
import { dbUserArray } from './user_data/user_data_handler';

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

Expand All @@ -28,25 +29,11 @@ if (db_blacklisted_users_collection_name.length < 1) throw new Error('Environmen

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

async function fetchUserProductCodes(
discord_user_id: string,
async function newFetchUserProductCodes(
db_user_data: DbUserDataArray
): Promise<string[]> {
const user_product_codes = await axios({
method: 'post',
url: 'https://api.inertia.lighting/v2/user/products/fetch',
headers: {
'Content-Type': 'application/json',
},
data: {
'discord_user_id': discord_user_id,
},
validateStatus: (status) => status === 200 || status === 404,
timeout: 10_000, // 10 seconds
}).then(
(response) => response.data as string[],
); // don't catch to propagate error to caller
return db_user_data.products;

return user_product_codes;
}

//------------------------------------------------------------//
Expand All @@ -65,9 +52,8 @@ export async function userProfileHandler(
},
});

const db_user_data = await db_user_data_find_cursor.next() as unknown as DbUserData | null;

if (!db_user_data) {
const original_db_user_data = await db_user_data_find_cursor.next() as unknown as DbUserData | null;
if (!original_db_user_data) {
await deferred_interaction.editReply({
embeds: [
CustomEmbed.from({
Expand All @@ -86,7 +72,7 @@ export async function userProfileHandler(

return;
}

const db_user_data = await dbUserArray(original_db_user_data);
const db_blacklisted_user_data_find_cursor = await go_mongo_db.find(db_database_name, db_blacklisted_users_collection_name, {
$or: [
{ 'identity.discord_user_id': db_user_data.identity.discord_user_id },
Expand All @@ -106,7 +92,7 @@ export async function userProfileHandler(

const db_viewable_roblox_products = await db_viewable_roblox_products_find_cursor.toArray() as unknown as DbProductData[];

const user_product_codes = await fetchUserProductCodes(db_user_data.identity.discord_user_id) ?? [];
const user_product_codes = await newFetchUserProductCodes(db_user_data) ?? [];
const user_products = db_viewable_roblox_products.filter(product => user_product_codes.includes(product.code));

const roblox_user_data: {
Expand Down
12 changes: 9 additions & 3 deletions src/custom_interactions/chat_input/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ if (db_users_collection_name.length < 1) throw new Error('Environment variable:
const user_verification_endpoints_base64_encoded_token = `${process.env.API_BASE64_ENCODED_TOKEN_FOR_USER_VERIFICATION_ENDPOINTS ?? ''}`;
if (user_verification_endpoints_base64_encoded_token.length < 1) throw new Error('Environment variable: API_BASE64_ENCODED_TOKEN_FOR_USER_VERIFICATION_ENDPOINTS; is not set correctly.');

const api_server_ip = `${process.env.API_SERVER_IP ?? ''}`;
if (api_server_ip.length < 1) throw new Error('Environment variable: API_SERVER_IP; is not set correctly.');

const api_server_port = `${process.env.API_SERVER_PORT ?? ''}`;
if (api_server_port.length < 1) throw new Error('Environment variable: API_SERVER_PORT; is not set correctly.');

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

async function userAlreadyVerifiedHandler(
Expand Down Expand Up @@ -76,7 +82,7 @@ async function verifyHandler(
try {
fetch_pending_verification_response = await axios({
method: 'post',
url: 'https://api.inertia.lighting/v2/user/verification/context/fetch',
url: `http://${api_server_ip}:${api_server_port}/v2/user/verification/context/fetch`,
headers: {
'Content-Type': 'application/json',
'Authorization': `InertiaAuthUserVerificationEndpoints ${user_verification_endpoints_base64_encoded_token}`,
Expand Down Expand Up @@ -143,7 +149,7 @@ async function verifyHandler(
try {
await axios({
method: 'post',
url: 'https://api.inertia.lighting/v2/user/verification/context/update',
url: `http://${api_server_ip}:${api_server_port}/v2/user/verification/context/update`,
headers: {
'Content-Type': 'application/json',
'Authorization': `InertiaAuthUserVerificationEndpoints ${user_verification_endpoints_base64_encoded_token}`,
Expand All @@ -157,7 +163,7 @@ async function verifyHandler(

await axios({
method: 'post',
url: 'https://api.inertia.lighting/v2/user/verification/context/submit',
url: `http://${api_server_ip}:${api_server_port}/v2/user/verification/context/submit`,
headers: {
'Content-Type': 'application/json',
'Authorization': `InertiaAuthUserVerificationEndpoints ${user_verification_endpoints_base64_encoded_token}`,
Expand Down
10 changes: 10 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ export interface DbUserProducts {
[product_code: string]: boolean;
}

export type DbUserProductsArray = string[];


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

export interface DbUserDataArray {
_id: MongoDB.ObjectId;
identity: DbUserIdentity;
lumens: number;
products: DbUserProductsArray;
}

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

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
17 changes: 0 additions & 17 deletions start_bot.bat

This file was deleted.

8 changes: 8 additions & 0 deletions update.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if [[ `git status --porcelain` ]]; then
call git pull
call docker build --tag inertia-dicord-bot .
call docker-compose doown
call docker-compose up -d
else
# No changes
fi

0 comments on commit 7610fba

Please sign in to comment.