diff --git a/package.json b/package.json index d651207..8d672b8 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "private": true, "scripts": { "tsc": "tsc", - "build": "", + "build": "yarn tsc -p tsconfig.json", "lint": "yarn eslint \"src/**/*\"", "lint-fix": "yarn lint --fix", "start": "ts-node src/index.ts", diff --git a/src/config.ts b/src/config.ts index a75dfb2..1597c53 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,36 @@ import dotenv from 'dotenv'; -dotenv.config(); +import { DataSourceOptions } from 'typeorm'; +import { BotServer } from './entity/BotServer'; +import { NewsDate } from './entity/NewsDate'; +import * as fs from 'fs'; + +const fileName = 'src/bot.env' +if (fs.existsSync(fileName)) + dotenv.config({path:fileName}); +else + dotenv.config(); + +export const dataSourceOption: DataSourceOptions = { + type: 'postgres', + host: process.env.dbHost || 'database host', + port: parseInt(process.env.dbPort)|| 5432, + username: process.env.dbUsername||'db user name', + database: process.env.dbName||'db name', + password: process.env.dbPWD ||'database password', + synchronize: false, + logging: false, + entities: [BotServer, NewsDate], + migrations: ['./migration/*.ts', './migration/*.js'], + subscribers: ['./subscriber/*.ts', './subscriber/*.js'], + ssl: false, + extra: { + insecureAuth: true, + ssl: { + rejectUnauthorized: false, + }, + }, +}; + export const CyphersApiKey = process.env.cyphersKey || 'Your Neople Open API Key'; diff --git a/src/data-sources.ts b/src/data-sources.ts index 447b9a2..ec28075 100644 --- a/src/data-sources.ts +++ b/src/data-sources.ts @@ -1,29 +1,7 @@ import 'reflect-metadata'; -import dotenv from 'dotenv'; import { DataSource, DataSourceOptions } from 'typeorm'; -import { BotServer } from './entity/BotServer'; -import { NewsDate } from './entity/NewsDate'; +import { dataSourceOption } from './config'; -dotenv.config(); -const dataSourceOption: DataSourceOptions = { - type: 'postgres', - host: process.env.dbHost || 'database host', - port: parseInt(process.env.dbPort)|| 5432, - username: process.env.dbUsername||'db user name', - database: process.env.dbName||'db name', - password: process.env.dbPWD ||'database password', - synchronize: false, - logging: false, - entities: [BotServer, NewsDate], - migrations: ['./migration/*.ts', './migration/*.js'], - subscribers: ['./subscriber/*.ts', './subscriber/*.js'], - ssl: false, - extra: { - insecureAuth: true, - ssl: { - rejectUnauthorized: false, - }, - }, -}; -const AppDataSource = new DataSource(dataSourceOption); + +const AppDataSource = new DataSource(dataSourceOption as DataSourceOptions); export default AppDataSource; diff --git a/src/utils/guild.ts b/src/utils/guild.ts index cfbc809..4fabcc9 100644 --- a/src/utils/guild.ts +++ b/src/utils/guild.ts @@ -11,20 +11,20 @@ export const deleteGuild = async (guild: Guild): Promise => { await botServerRepository.remove(server); }; -const registerGuildStr = async (guildID: string): Promise => { +const registerGuildStr = async (guildID: string): Promise => { try { const server = new BotServer(); server.serverId = guildID; await botServerRepository.insert(server); - return true; + return server; } catch { - return false; + return null; } }; export const registerGuild = async (guild: Guild): Promise => { const result = await registerGuildStr(guild.id.toString()); - if (result === false) { + if (result === null) { guild.systemChannel?.send('failed to register server'); guild.leave(); } @@ -34,13 +34,10 @@ export const getGuildInfoStr = async ( guildID: string ): Promise => { if (guildID === undefined || guildID === '') return null; - let server: BotServer | null = await botServerRepository.findOne({ + const server: BotServer | null = await botServerRepository.findOne({ where: { serverId: guildID }, }); - if (server === null) { - registerGuildStr(guildID); - server = new BotServer(); - } + if (server === null) return registerGuildStr(guildID); server.serverId = guildID; return server; };